blob: fd70928a724e233088e91b955be2596638359a0d [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 Denk4c9ca782006-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
wdenk9dd2b882002-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 Chiou0b5e5a32012-10-25 16:31:07 +000033#include <fdtdec.h>
Simon Glassa6642e02012-03-30 21:30:55 +000034#include <malloc.h>
Andreas Bießmann61d01952011-07-18 20:24:04 +020035#include <version.h>
Wolfgang Denkb0723792005-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-VILLARD03836942008-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 Chiou0b5e5a32012-10-25 16:31:07 +000044#ifdef CONFIG_OF_CONTROL
45#include <fdtdec.h>
46#endif
47
Gabe Black85892c12012-10-25 16:31:08 +000048#ifdef CONFIG_OF_LIBFDT
49#include <fdt_support.h>
50#endif /* CONFIG_OF_LIBFDT */
51
wdenk0a658552003-08-05 17:43:17 +000052#include <post.h>
Jason Hobbscafa1aa2011-08-23 11:06:54 +000053#include <linux/ctype.h>
Heiko Schocherb4c1df82012-01-16 21:13:35 +000054#include <menu.h>
wdenk0a658552003-08-05 17:43:17 +000055
James Yangb83983f2008-05-05 10:22:53 -050056#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denk6405a152006-03-31 18:32:53 +020057DECLARE_GLOBAL_DATA_PTR;
58#endif
59
Heiko Schocher8a8ec532007-07-13 09:54:17 +020060/*
61 * Board-specific Platform code can reimplement show_boot_progress () if needed
62 */
63void inline __show_boot_progress (int val) {}
Emil Medveace7cc82009-05-12 13:48:32 -050064void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocher8a8ec532007-07-13 09:54:17 +020065
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020066#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +000067int update_tftp (ulong addr);
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020068#endif /* CONFIG_UPDATE_TFTP */
wdenk57b2d802003-06-27 21:31:46 +000069
wdenkc6097192002-11-03 00:24:07 +000070#define MAX_DELAY_STOP_STR 32
71
wdenkc6097192002-11-03 00:24:07 +000072#undef DEBUG_PARSER
73
John Schmollerc01e7202010-03-12 09:49:23 -060074char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000075
Stefan Roese59ff5a52006-07-27 16:11:19 +020076static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysingerd5c319c2010-12-22 09:40:45 -050077static const char erase_seq[] = "\b \b"; /* erase sequence */
78static const char tab_seq[] = " "; /* used to expand TABs */
wdenkc6097192002-11-03 00:24:07 +000079
80#ifdef CONFIG_BOOT_RETRY_TIME
81static uint64_t endtime = 0; /* must be set, default is instant timeout */
82static int retry_time = -1; /* -1 so can call readline before main_loop */
83#endif
84
85#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
86
87#ifndef CONFIG_BOOT_RETRY_MIN
88#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
89#endif
90
91#ifdef CONFIG_MODEM_SUPPORT
92int do_mdm_init = 0;
93extern void mdm_init(void); /* defined in board.c */
94#endif
95
96/***************************************************************************
97 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
Jason Hobbsa56febb2011-08-31 05:37:24 +000098 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
wdenkc6097192002-11-03 00:24:07 +000099 */
100#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
101# if defined(CONFIG_AUTOBOOT_KEYED)
Jason Hobbs65febe62011-08-23 11:06:50 +0000102#ifndef CONFIG_MENU
103static inline
104#endif
105int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000106{
107 int abort = 0;
108 uint64_t etime = endtick(bootdelay);
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200109 struct {
wdenkc6097192002-11-03 00:24:07 +0000110 char* str;
111 u_int len;
112 int retry;
113 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200114 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000115 { str: getenv ("bootdelaykey"), retry: 1 },
116 { str: getenv ("bootdelaykey2"), retry: 1 },
117 { str: getenv ("bootstopkey"), retry: 0 },
118 { str: getenv ("bootstopkey2"), retry: 0 },
119 };
120
121 char presskey [MAX_DELAY_STOP_STR];
122 u_int presskey_len = 0;
123 u_int presskey_max = 0;
124 u_int i;
125
Dirk Eibachc82b4ea2012-04-26 01:49:33 +0000126#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
127 if (bootdelay == 0)
128 return 0;
129#endif
130
wdenkc6097192002-11-03 00:24:07 +0000131# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roese37628252008-08-06 14:05:38 +0200132 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000133# endif
134
135# ifdef CONFIG_AUTOBOOT_DELAY_STR
136 if (delaykey[0].str == NULL)
137 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
138# endif
139# ifdef CONFIG_AUTOBOOT_DELAY_STR2
140 if (delaykey[1].str == NULL)
141 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
142# endif
143# ifdef CONFIG_AUTOBOOT_STOP_STR
144 if (delaykey[2].str == NULL)
145 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
146# endif
147# ifdef CONFIG_AUTOBOOT_STOP_STR2
148 if (delaykey[3].str == NULL)
149 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
150# endif
151
152 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
153 delaykey[i].len = delaykey[i].str == NULL ?
154 0 : strlen (delaykey[i].str);
155 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
156 MAX_DELAY_STOP_STR : delaykey[i].len;
157
158 presskey_max = presskey_max > delaykey[i].len ?
159 presskey_max : delaykey[i].len;
160
161# if DEBUG_BOOTKEYS
162 printf("%s key:<%s>\n",
163 delaykey[i].retry ? "delay" : "stop",
164 delaykey[i].str ? delaykey[i].str : "NULL");
165# endif
166 }
167
168 /* In order to keep up with incoming data, check timeout only
169 * when catch up.
170 */
Peter Korsgaard6499fd62008-12-10 16:24:16 +0100171 do {
172 if (tstc()) {
173 if (presskey_len < presskey_max) {
174 presskey [presskey_len ++] = getc();
175 }
176 else {
177 for (i = 0; i < presskey_max - 1; i ++)
178 presskey [i] = presskey [i + 1];
179
180 presskey [i] = getc();
181 }
182 }
183
wdenkc6097192002-11-03 00:24:07 +0000184 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
185 if (delaykey[i].len > 0 &&
186 presskey_len >= delaykey[i].len &&
187 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk57b2d802003-06-27 21:31:46 +0000188 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000189 delaykey[i].len) == 0) {
190# if DEBUG_BOOTKEYS
191 printf("got %skey\n",
192 delaykey[i].retry ? "delay" : "stop");
193# endif
194
195# ifdef CONFIG_BOOT_RETRY_TIME
196 /* don't retry auto boot */
197 if (! delaykey[i].retry)
198 retry_time = -1;
199# endif
200 abort = 1;
201 }
202 }
Peter Korsgaard6499fd62008-12-10 16:24:16 +0100203 } while (!abort && get_ticks() <= etime);
wdenkc6097192002-11-03 00:24:07 +0000204
wdenkc6097192002-11-03 00:24:07 +0000205# if DEBUG_BOOTKEYS
206 if (!abort)
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200207 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000208# endif
209
dzu6605c842003-10-24 13:14:45 +0000210#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200211 if (abort)
212 gd->flags &= ~GD_FLG_SILENT;
dzu6605c842003-10-24 13:14:45 +0000213#endif
214
wdenkc6097192002-11-03 00:24:07 +0000215 return abort;
216}
217
218# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
219
wdenk452cfd62002-11-19 11:04:11 +0000220#ifdef CONFIG_MENUKEY
221static int menukey = 0;
222#endif
223
Jason Hobbs65febe62011-08-23 11:06:50 +0000224#ifndef CONFIG_MENU
225static inline
226#endif
227int abortboot(int bootdelay)
wdenkc6097192002-11-03 00:24:07 +0000228{
229 int abort = 0;
230
wdenk452cfd62002-11-19 11:04:11 +0000231#ifdef CONFIG_MENUPROMPT
Stefan Roese37628252008-08-06 14:05:38 +0200232 printf(CONFIG_MENUPROMPT);
wdenk452cfd62002-11-19 11:04:11 +0000233#else
Joe Hershberger96ccaf32012-08-17 10:53:12 +0000234 if (bootdelay >= 0)
235 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenk452cfd62002-11-19 11:04:11 +0000236#endif
wdenkc6097192002-11-03 00:24:07 +0000237
238#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk57b2d802003-06-27 21:31:46 +0000239 /*
240 * Check if key already pressed
241 * Don't check if bootdelay < 0
242 */
wdenkc6097192002-11-03 00:24:07 +0000243 if (bootdelay >= 0) {
244 if (tstc()) { /* we got a key press */
245 (void) getc(); /* consume input */
wdenk42c05472004-03-23 22:14:11 +0000246 puts ("\b\b\b 0");
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200247 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000248 }
wdenk57b2d802003-06-27 21:31:46 +0000249 }
wdenkc6097192002-11-03 00:24:07 +0000250#endif
251
wdenk808532a2003-10-10 10:05:42 +0000252 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000253 int i;
254
255 --bootdelay;
256 /* delay 100 * 10ms */
257 for (i=0; !abort && i<100; ++i) {
258 if (tstc()) { /* we got a key press */
259 abort = 1; /* don't auto boot */
260 bootdelay = 0; /* no more delay */
wdenk452cfd62002-11-19 11:04:11 +0000261# ifdef CONFIG_MENUKEY
262 menukey = getc();
263# else
wdenkc6097192002-11-03 00:24:07 +0000264 (void) getc(); /* consume input */
wdenk452cfd62002-11-19 11:04:11 +0000265# endif
wdenkc6097192002-11-03 00:24:07 +0000266 break;
267 }
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200268 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000269 }
270
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200271 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000272 }
273
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200274 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000275
wdenk808532a2003-10-10 10:05:42 +0000276#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200277 if (abort)
278 gd->flags &= ~GD_FLG_SILENT;
wdenk808532a2003-10-10 10:05:42 +0000279#endif
280
wdenkc6097192002-11-03 00:24:07 +0000281 return abort;
282}
283# endif /* CONFIG_AUTOBOOT_KEYED */
284#endif /* CONFIG_BOOTDELAY >= 0 */
285
286/****************************************************************************/
287
288void main_loop (void)
289{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200290#ifndef CONFIG_SYS_HUSH_PARSER
291 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
wdenkc6097192002-11-03 00:24:07 +0000292 int len;
293 int rc = 1;
294 int flag;
295#endif
Che-Liang Chiou0b5e5a32012-10-25 16:31:07 +0000296#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
297 defined(CONFIG_OF_CONTROL)
298 char *env;
299#endif
wdenkc6097192002-11-03 00:24:07 +0000300#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
301 char *s;
302 int bootdelay;
303#endif
304#ifdef CONFIG_PREBOOT
305 char *p;
306#endif
wdenk0a658552003-08-05 17:43:17 +0000307#ifdef CONFIG_BOOTCOUNT_LIMIT
308 unsigned long bootcount = 0;
309 unsigned long bootlimit = 0;
310 char *bcs;
311 char bcs_set[16];
312#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000313
wdenk0a658552003-08-05 17:43:17 +0000314#ifdef CONFIG_BOOTCOUNT_LIMIT
315 bootcount = bootcount_load();
316 bootcount++;
317 bootcount_store (bootcount);
318 sprintf (bcs_set, "%lu", bootcount);
319 setenv ("bootcount", bcs_set);
320 bcs = getenv ("bootlimit");
321 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
322#endif /* CONFIG_BOOTCOUNT_LIMIT */
323
wdenkc6097192002-11-03 00:24:07 +0000324#ifdef CONFIG_MODEM_SUPPORT
325 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
326 if (do_mdm_init) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200327 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000328 setenv ("preboot", str); /* set or delete definition */
329 if (str != NULL)
330 free (str);
331 mdm_init(); /* wait for modem connection */
332 }
333#endif /* CONFIG_MODEM_SUPPORT */
334
stroesef8105cf2003-04-04 15:44:49 +0000335#ifdef CONFIG_VERSION_VARIABLE
336 {
stroeseb9ab27c2003-07-11 08:00:33 +0000337 setenv ("ver", version_string); /* set version variable */
stroesef8105cf2003-04-04 15:44:49 +0000338 }
339#endif /* CONFIG_VERSION_VARIABLE */
340
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200341#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000342 u_boot_hush_start ();
343#endif
344
Heiko Schocher49ebcb52008-10-15 09:40:28 +0200345#if defined(CONFIG_HUSH_INIT_VAR)
346 hush_init_var ();
347#endif
348
wdenkc6097192002-11-03 00:24:07 +0000349#ifdef CONFIG_PREBOOT
350 if ((p = getenv ("preboot")) != NULL) {
351# ifdef CONFIG_AUTOBOOT_KEYED
352 int prev = disable_ctrlc(1); /* disable Control C checking */
353# endif
354
Simon Glassd0eb9402012-03-30 21:30:56 +0000355 run_command_list(p, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000356
357# ifdef CONFIG_AUTOBOOT_KEYED
358 disable_ctrlc(prev); /* restore Control C checking */
359# endif
360 }
361#endif /* CONFIG_PREBOOT */
362
Wolfgang Denkce2dd6e2010-03-11 23:56:03 +0100363#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsche3fd7c02011-07-16 05:50:59 +0000364 update_tftp (0UL);
Wolfgang Denkce2dd6e2010-03-11 23:56:03 +0100365#endif /* CONFIG_UPDATE_TFTP */
366
wdenkc6097192002-11-03 00:24:07 +0000367#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
368 s = getenv ("bootdelay");
369 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
370
wdenk9dd2b882002-12-03 21:28:10 +0000371 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000372
Heiko Schocherb4c1df82012-01-16 21:13:35 +0000373#if defined(CONFIG_MENU_SHOW)
374 bootdelay = menu_show(bootdelay);
375#endif
wdenkc6097192002-11-03 00:24:07 +0000376# ifdef CONFIG_BOOT_RETRY_TIME
wdenkb00ec162003-06-19 23:40:20 +0000377 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000378# endif /* CONFIG_BOOT_RETRY_TIME */
379
Yuri Tikhonovd773efb2008-02-04 14:11:03 +0100380#ifdef CONFIG_POST
381 if (gd->flags & GD_FLG_POSTFAIL) {
382 s = getenv("failbootcmd");
383 }
384 else
385#endif /* CONFIG_POST */
wdenk0a658552003-08-05 17:43:17 +0000386#ifdef CONFIG_BOOTCOUNT_LIMIT
387 if (bootlimit && (bootcount > bootlimit)) {
388 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
389 (unsigned)bootlimit);
390 s = getenv ("altbootcmd");
391 }
392 else
393#endif /* CONFIG_BOOTCOUNT_LIMIT */
394 s = getenv ("bootcmd");
Che-Liang Chiou0b5e5a32012-10-25 16:31:07 +0000395#ifdef CONFIG_OF_CONTROL
396 /* Allow the fdt to override the boot command */
397 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
398 if (env)
399 s = env;
400#endif /* CONFIG_OF_CONTROL */
wdenk9dd2b882002-12-03 21:28:10 +0000401
402 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
403
Joe Hershberger96ccaf32012-08-17 10:53:12 +0000404 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
wdenkc6097192002-11-03 00:24:07 +0000405# ifdef CONFIG_AUTOBOOT_KEYED
406 int prev = disable_ctrlc(1); /* disable Control C checking */
407# endif
408
Simon Glassd0eb9402012-03-30 21:30:56 +0000409 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000410
411# ifdef CONFIG_AUTOBOOT_KEYED
412 disable_ctrlc(prev); /* restore Control C checking */
413# endif
414 }
wdenk452cfd62002-11-19 11:04:11 +0000415
416# ifdef CONFIG_MENUKEY
wdenk9dd2b882002-12-03 21:28:10 +0000417 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs3b403b72011-06-23 08:27:30 +0000418 s = getenv("menucmd");
Jason Hobbsa56febb2011-08-31 05:37:24 +0000419 if (s)
Simon Glassd0eb9402012-03-30 21:30:56 +0000420 run_command_list(s, -1, 0);
wdenk452cfd62002-11-19 11:04:11 +0000421 }
422#endif /* CONFIG_MENUKEY */
Wolfgang Denkb0b104a2010-06-13 18:28:54 +0200423#endif /* CONFIG_BOOTDELAY */
wdenk452cfd62002-11-19 11:04:11 +0000424
Gabe Black85892c12012-10-25 16:31:08 +0000425#if defined CONFIG_OF_CONTROL
426 set_working_fdt_addr((void *)gd->fdt_blob);
427#endif /* CONFIG_OF_CONTROL */
428
wdenkc6097192002-11-03 00:24:07 +0000429 /*
430 * Main Loop for Monitor Command Processing
431 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200432#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000433 parse_file_outer();
434 /* This point is never reached */
435 for (;;);
436#else
437 for (;;) {
438#ifdef CONFIG_BOOT_RETRY_TIME
439 if (rc >= 0) {
440 /* Saw enough of a valid command to
441 * restart the timeout.
442 */
443 reset_cmd_timeout();
444 }
445#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200446 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000447
448 flag = 0; /* assume no special flags for now */
449 if (len > 0)
450 strcpy (lastcommand, console_buffer);
451 else if (len == 0)
452 flag |= CMD_FLAG_REPEAT;
453#ifdef CONFIG_BOOT_RETRY_TIME
454 else if (len == -2) {
455 /* -2 means timed out, retry autoboot
456 */
wdenk42c05472004-03-23 22:14:11 +0000457 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000458# ifdef CONFIG_RESET_TO_RETRY
459 /* Reinit board to run initialization code again */
460 do_reset (NULL, 0, 0, NULL);
461# else
462 return; /* retry autoboot */
463# endif
464 }
465#endif
466
467 if (len == -1)
wdenk42c05472004-03-23 22:14:11 +0000468 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000469 else
Simon Glassbf8c5b02012-02-14 19:59:21 +0000470 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000471
472 if (rc <= 0) {
473 /* invalid command or not repeatable, forget it */
474 lastcommand[0] = 0;
475 }
476 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200477#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000478}
479
wdenkb00ec162003-06-19 23:40:20 +0000480#ifdef CONFIG_BOOT_RETRY_TIME
481/***************************************************************************
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200482 * initialize command line timeout
wdenkb00ec162003-06-19 23:40:20 +0000483 */
484void init_cmd_timeout(void)
485{
486 char *s = getenv ("bootretry");
487
488 if (s != NULL)
wdenk444f22b2003-12-07 21:39:28 +0000489 retry_time = (int)simple_strtol(s, NULL, 10);
wdenkb00ec162003-06-19 23:40:20 +0000490 else
491 retry_time = CONFIG_BOOT_RETRY_TIME;
492
493 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
494 retry_time = CONFIG_BOOT_RETRY_MIN;
495}
496
wdenkc6097192002-11-03 00:24:07 +0000497/***************************************************************************
498 * reset command line timeout to retry_time seconds
499 */
wdenkc6097192002-11-03 00:24:07 +0000500void reset_cmd_timeout(void)
501{
502 endtime = endtick(retry_time);
503}
504#endif
505
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200506#ifdef CONFIG_CMDLINE_EDITING
507
508/*
509 * cmdline-editing related codes from vivi.
510 * Author: Janghoon Lyu <nandy@mizi.com>
511 */
512
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200513#define putnstr(str,n) do { \
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700514 printf ("%.*s", (int)n, str); \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200515 } while (0)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200516
517#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200518#define CTL_BACKSPACE ('\b')
519#define DEL ((char)255)
520#define DEL7 ((char)127)
521#define CREAD_HIST_CHAR ('!')
522
523#define getcmd_putch(ch) putc(ch)
524#define getcmd_getch() getc()
525#define getcmd_cbeep() getcmd_putch('\a')
526
527#define HIST_MAX 20
Peter Tyser7e0e5ca2010-09-29 13:30:56 -0500528#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200529
Kim Phillipsb052b602012-10-29 13:34:32 +0000530static int hist_max;
531static int hist_add_idx;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200532static int hist_cur = -1;
Kim Phillipsb052b602012-10-29 13:34:32 +0000533static unsigned hist_num;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200534
Kim Phillipsb052b602012-10-29 13:34:32 +0000535static char *hist_list[HIST_MAX];
536static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200537
538#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
539
540static void hist_init(void)
541{
542 int i;
543
544 hist_max = 0;
545 hist_add_idx = 0;
546 hist_cur = -1;
547 hist_num = 0;
548
549 for (i = 0; i < HIST_MAX; i++) {
550 hist_list[i] = hist_lines[i];
551 hist_list[i][0] = '\0';
552 }
553}
554
555static void cread_add_to_hist(char *line)
556{
557 strcpy(hist_list[hist_add_idx], line);
558
559 if (++hist_add_idx >= HIST_MAX)
560 hist_add_idx = 0;
561
562 if (hist_add_idx > hist_max)
563 hist_max = hist_add_idx;
564
565 hist_num++;
566}
567
568static char* hist_prev(void)
569{
570 char *ret;
571 int old_cur;
572
573 if (hist_cur < 0)
574 return NULL;
575
576 old_cur = hist_cur;
577 if (--hist_cur < 0)
578 hist_cur = hist_max;
579
580 if (hist_cur == hist_add_idx) {
581 hist_cur = old_cur;
582 ret = NULL;
583 } else
584 ret = hist_list[hist_cur];
585
586 return (ret);
587}
588
589static char* hist_next(void)
590{
591 char *ret;
592
593 if (hist_cur < 0)
594 return NULL;
595
596 if (hist_cur == hist_add_idx)
597 return NULL;
598
599 if (++hist_cur > hist_max)
600 hist_cur = 0;
601
602 if (hist_cur == hist_add_idx) {
603 ret = "";
604 } else
605 ret = hist_list[hist_cur];
606
607 return (ret);
608}
609
Stefan Roese59ff5a52006-07-27 16:11:19 +0200610#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200611static void cread_print_hist_list(void)
612{
613 int i;
614 unsigned long n;
615
616 n = hist_num - hist_max;
617
618 i = hist_add_idx + 1;
619 while (1) {
620 if (i > hist_max)
621 i = 0;
622 if (i == hist_add_idx)
623 break;
624 printf("%s\n", hist_list[i]);
625 n++;
626 i++;
627 }
628}
Stefan Roese59ff5a52006-07-27 16:11:19 +0200629#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200630
631#define BEGINNING_OF_LINE() { \
632 while (num) { \
633 getcmd_putch(CTL_BACKSPACE); \
634 num--; \
635 } \
636}
637
638#define ERASE_TO_EOL() { \
639 if (num < eol_num) { \
Mike Frysinger3952bea2010-07-23 05:28:15 -0400640 printf("%*s", (int)(eol_num - num), ""); \
641 do { \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200642 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger3952bea2010-07-23 05:28:15 -0400643 } while (--eol_num > num); \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200644 } \
645}
646
647#define REFRESH_TO_EOL() { \
648 if (num < eol_num) { \
649 wlen = eol_num - num; \
650 putnstr(buf + num, wlen); \
651 num = eol_num; \
652 } \
653}
654
655static void cread_add_char(char ichar, int insert, unsigned long *num,
656 unsigned long *eol_num, char *buf, unsigned long len)
657{
658 unsigned long wlen;
659
660 /* room ??? */
661 if (insert || *num == *eol_num) {
662 if (*eol_num > len - 1) {
663 getcmd_cbeep();
664 return;
665 }
666 (*eol_num)++;
667 }
668
669 if (insert) {
670 wlen = *eol_num - *num;
671 if (wlen > 1) {
672 memmove(&buf[*num+1], &buf[*num], wlen-1);
673 }
674
675 buf[*num] = ichar;
676 putnstr(buf + *num, wlen);
677 (*num)++;
678 while (--wlen) {
679 getcmd_putch(CTL_BACKSPACE);
680 }
681 } else {
682 /* echo the character */
683 wlen = 1;
684 buf[*num] = ichar;
685 putnstr(buf + *num, wlen);
686 (*num)++;
687 }
688}
689
690static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
691 unsigned long *eol_num, char *buf, unsigned long len)
692{
693 while (strsize--) {
694 cread_add_char(*str, insert, num, eol_num, buf, len);
695 str++;
696 }
697}
698
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000699static int cread_line(const char *const prompt, char *buf, unsigned int *len,
700 int timeout)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200701{
702 unsigned long num = 0;
703 unsigned long eol_num = 0;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200704 unsigned long wlen;
705 char ichar;
706 int insert = 1;
707 int esc_len = 0;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200708 char esc_save[8];
Peter Tyser8ad15a92009-10-25 15:12:54 -0500709 int init_len = strlen(buf);
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000710 int first = 1;
Peter Tyser8ad15a92009-10-25 15:12:54 -0500711
712 if (init_len)
713 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200714
715 while (1) {
Andreas Engel3a0ba102008-01-09 17:10:56 +0100716#ifdef CONFIG_BOOT_RETRY_TIME
717 while (!tstc()) { /* while no incoming data */
718 if (retry_time >= 0 && get_ticks() > endtime)
719 return (-2); /* timed out */
Jens Scharsig11247552010-04-09 19:02:38 +0200720 WATCHDOG_RESET();
Andreas Engel3a0ba102008-01-09 17:10:56 +0100721 }
722#endif
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000723 if (first && timeout) {
724 uint64_t etime = endtick(timeout);
725
726 while (!tstc()) { /* while no incoming data */
727 if (get_ticks() >= etime)
728 return -2; /* timed out */
729 WATCHDOG_RESET();
730 }
731 first = 0;
732 }
Andreas Engel3a0ba102008-01-09 17:10:56 +0100733
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200734 ichar = getcmd_getch();
735
736 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200737 putc('\n');
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200738 break;
739 }
740
741 /*
742 * handle standard linux xterm esc sequences for arrow key, etc.
743 */
744 if (esc_len != 0) {
745 if (esc_len == 1) {
746 if (ichar == '[') {
747 esc_save[esc_len] = ichar;
748 esc_len = 2;
749 } else {
750 cread_add_str(esc_save, esc_len, insert,
751 &num, &eol_num, buf, *len);
752 esc_len = 0;
753 }
754 continue;
755 }
756
757 switch (ichar) {
758
759 case 'D': /* <- key */
760 ichar = CTL_CH('b');
761 esc_len = 0;
762 break;
763 case 'C': /* -> key */
764 ichar = CTL_CH('f');
765 esc_len = 0;
766 break; /* pass off to ^F handler */
767 case 'H': /* Home key */
768 ichar = CTL_CH('a');
769 esc_len = 0;
770 break; /* pass off to ^A handler */
771 case 'A': /* up arrow */
772 ichar = CTL_CH('p');
773 esc_len = 0;
774 break; /* pass off to ^P handler */
775 case 'B': /* down arrow */
776 ichar = CTL_CH('n');
777 esc_len = 0;
778 break; /* pass off to ^N handler */
779 default:
780 esc_save[esc_len++] = ichar;
781 cread_add_str(esc_save, esc_len, insert,
782 &num, &eol_num, buf, *len);
783 esc_len = 0;
784 continue;
785 }
786 }
787
788 switch (ichar) {
789 case 0x1b:
790 if (esc_len == 0) {
791 esc_save[esc_len] = ichar;
792 esc_len = 1;
793 } else {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200794 puts("impossible condition #876\n");
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200795 esc_len = 0;
796 }
797 break;
798
799 case CTL_CH('a'):
800 BEGINNING_OF_LINE();
801 break;
802 case CTL_CH('c'): /* ^C - break */
803 *buf = '\0'; /* discard input */
804 return (-1);
805 case CTL_CH('f'):
806 if (num < eol_num) {
807 getcmd_putch(buf[num]);
808 num++;
809 }
810 break;
811 case CTL_CH('b'):
812 if (num) {
813 getcmd_putch(CTL_BACKSPACE);
814 num--;
815 }
816 break;
817 case CTL_CH('d'):
818 if (num < eol_num) {
819 wlen = eol_num - num - 1;
820 if (wlen) {
821 memmove(&buf[num], &buf[num+1], wlen);
822 putnstr(buf + num, wlen);
823 }
824
825 getcmd_putch(' ');
826 do {
827 getcmd_putch(CTL_BACKSPACE);
828 } while (wlen--);
829 eol_num--;
830 }
831 break;
832 case CTL_CH('k'):
833 ERASE_TO_EOL();
834 break;
835 case CTL_CH('e'):
836 REFRESH_TO_EOL();
837 break;
838 case CTL_CH('o'):
839 insert = !insert;
840 break;
841 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100842 case CTL_CH('u'):
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200843 BEGINNING_OF_LINE();
844 ERASE_TO_EOL();
845 break;
846 case DEL:
847 case DEL7:
848 case 8:
849 if (num) {
850 wlen = eol_num - num;
851 num--;
852 memmove(&buf[num], &buf[num+1], wlen);
853 getcmd_putch(CTL_BACKSPACE);
854 putnstr(buf + num, wlen);
855 getcmd_putch(' ');
856 do {
857 getcmd_putch(CTL_BACKSPACE);
858 } while (wlen--);
859 eol_num--;
860 }
861 break;
862 case CTL_CH('p'):
863 case CTL_CH('n'):
864 {
865 char * hline;
866
867 esc_len = 0;
868
869 if (ichar == CTL_CH('p'))
870 hline = hist_prev();
871 else
872 hline = hist_next();
873
874 if (!hline) {
875 getcmd_cbeep();
876 continue;
877 }
878
879 /* nuke the current line */
880 /* first, go home */
881 BEGINNING_OF_LINE();
882
883 /* erase to end of line */
884 ERASE_TO_EOL();
885
886 /* copy new line into place and display */
887 strcpy(buf, hline);
888 eol_num = strlen(buf);
889 REFRESH_TO_EOL();
890 continue;
891 }
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100892#ifdef CONFIG_AUTO_COMPLETE
893 case '\t': {
894 int num2, col;
895
896 /* do not autocomplete when in the middle */
897 if (num < eol_num) {
898 getcmd_cbeep();
899 break;
900 }
901
902 buf[num] = '\0';
903 col = strlen(prompt) + eol_num;
904 num2 = num;
905 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
906 col = num2 - num;
907 num += col;
908 eol_num += col;
909 }
910 break;
911 }
912#endif
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200913 default:
914 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
915 break;
916 }
917 }
918 *len = eol_num;
919 buf[eol_num] = '\0'; /* lose the newline */
920
921 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
922 cread_add_to_hist(buf);
923 hist_cur = hist_add_idx;
924
Peter Tyser03e46c42009-10-25 15:12:53 -0500925 return 0;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200926}
927
928#endif /* CONFIG_CMDLINE_EDITING */
929
wdenkc6097192002-11-03 00:24:07 +0000930/****************************************************************************/
931
932/*
933 * Prompt for input and read a line.
934 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
935 * time out when time goes past endtime (timebase time in ticks).
936 * Return: number of read characters
937 * -1 if break
938 * -2 if timed out
939 */
940int readline (const char *const prompt)
941{
Peter Tyser8ad15a92009-10-25 15:12:54 -0500942 /*
943 * If console_buffer isn't 0-length the user will be prompted to modify
944 * it instead of entering it from scratch as desired.
945 */
946 console_buffer[0] = '\0';
947
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000948 return readline_into_buffer(prompt, console_buffer, 0);
James Yang20ffa972008-01-09 11:17:49 -0600949}
950
951
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000952int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang20ffa972008-01-09 11:17:49 -0600953{
954 char *p = buffer;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200955#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser7e0e5ca2010-09-29 13:30:56 -0500956 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesedc5ff702006-08-07 15:08:44 +0200957 int rc;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200958 static int initted = 0;
959
James Yangb83983f2008-05-05 10:22:53 -0500960 /*
961 * History uses a global array which is not
962 * writable until after relocation to RAM.
963 * Revert to non-history version if still
964 * running from flash.
965 */
966 if (gd->flags & GD_FLG_RELOC) {
967 if (!initted) {
968 hist_init();
969 initted = 1;
970 }
971
Peter Tyserc144e012009-10-25 15:12:52 -0500972 if (prompt)
973 puts (prompt);
James Yangb83983f2008-05-05 10:22:53 -0500974
Heiko Schocher0eab4d52012-01-16 21:13:05 +0000975 rc = cread_line(prompt, p, &len, timeout);
James Yangb83983f2008-05-05 10:22:53 -0500976 return rc < 0 ? rc : len;
977
978 } else {
979#endif /* CONFIG_CMDLINE_EDITING */
Kumar Galae225b172008-01-10 02:22:05 -0600980 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000981 int n = 0; /* buffer index */
982 int plen = 0; /* prompt length */
983 int col; /* output column cnt */
984 char c;
985
986 /* print prompt */
987 if (prompt) {
988 plen = strlen (prompt);
989 puts (prompt);
990 }
991 col = plen;
992
993 for (;;) {
994#ifdef CONFIG_BOOT_RETRY_TIME
995 while (!tstc()) { /* while no incoming data */
996 if (retry_time >= 0 && get_ticks() > endtime)
997 return (-2); /* timed out */
Jens Scharsig11247552010-04-09 19:02:38 +0200998 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +0000999 }
1000#endif
1001 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1002
1003#ifdef CONFIG_SHOW_ACTIVITY
1004 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +00001005 show_activity(0);
Jens Scharsig11247552010-04-09 19:02:38 +02001006 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001007 }
1008#endif
1009 c = getc();
1010
1011 /*
1012 * Special character handling
1013 */
1014 switch (c) {
1015 case '\r': /* Enter */
1016 case '\n':
1017 *p = '\0';
1018 puts ("\r\n");
James Yang20ffa972008-01-09 11:17:49 -06001019 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001020
wdenk8d4d1f62004-03-23 22:37:33 +00001021 case '\0': /* nul */
1022 continue;
1023
wdenkc6097192002-11-03 00:24:07 +00001024 case 0x03: /* ^C - break */
James Yang20ffa972008-01-09 11:17:49 -06001025 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001026 return (-1);
1027
1028 case 0x15: /* ^U - erase line */
1029 while (col > plen) {
1030 puts (erase_seq);
1031 --col;
1032 }
James Yang20ffa972008-01-09 11:17:49 -06001033 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001034 n = 0;
1035 continue;
1036
Wolfgang Denk70df7bc2007-06-22 23:59:00 +02001037 case 0x17: /* ^W - erase word */
James Yang20ffa972008-01-09 11:17:49 -06001038 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001039 while ((n > 0) && (*p != ' ')) {
James Yang20ffa972008-01-09 11:17:49 -06001040 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001041 }
1042 continue;
1043
1044 case 0x08: /* ^H - backspace */
1045 case 0x7F: /* DEL - backspace */
James Yang20ffa972008-01-09 11:17:49 -06001046 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001047 continue;
1048
1049 default:
1050 /*
1051 * Must be a normal character then
1052 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001053 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001054 if (c == '\t') { /* expand TABs */
wdenk3902d702004-04-15 18:22:41 +00001055#ifdef CONFIG_AUTO_COMPLETE
1056 /* if auto completion triggered just continue */
1057 *p = '\0';
1058 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang20ffa972008-01-09 11:17:49 -06001059 p = p_buf + n; /* reset */
wdenk3902d702004-04-15 18:22:41 +00001060 continue;
1061 }
1062#endif
wdenkc6097192002-11-03 00:24:07 +00001063 puts (tab_seq+(col&07));
1064 col += 8 - (col&07);
1065 } else {
1066 ++col; /* echo input */
1067 putc (c);
1068 }
1069 *p++ = c;
1070 ++n;
1071 } else { /* Buffer full */
1072 putc ('\a');
1073 }
1074 }
1075 }
James Yangb83983f2008-05-05 10:22:53 -05001076#ifdef CONFIG_CMDLINE_EDITING
1077 }
1078#endif
wdenkc6097192002-11-03 00:24:07 +00001079}
1080
1081/****************************************************************************/
1082
1083static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1084{
1085 char *s;
1086
1087 if (*np == 0) {
1088 return (p);
1089 }
1090
1091 if (*(--p) == '\t') { /* will retype the whole line */
1092 while (*colp > plen) {
1093 puts (erase_seq);
1094 (*colp)--;
1095 }
1096 for (s=buffer; s<p; ++s) {
1097 if (*s == '\t') {
1098 puts (tab_seq+((*colp) & 07));
1099 *colp += 8 - ((*colp) & 07);
1100 } else {
1101 ++(*colp);
1102 putc (*s);
1103 }
1104 }
1105 } else {
1106 puts (erase_seq);
1107 (*colp)--;
1108 }
1109 (*np)--;
1110 return (p);
1111}
1112
1113/****************************************************************************/
1114
1115int parse_line (char *line, char *argv[])
1116{
1117 int nargs = 0;
1118
1119#ifdef DEBUG_PARSER
1120 printf ("parse_line: \"%s\"\n", line);
1121#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001122 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001123
1124 /* skip any white space */
Jason Hobbscafa1aa2011-08-23 11:06:54 +00001125 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001126 ++line;
wdenkc6097192002-11-03 00:24:07 +00001127
1128 if (*line == '\0') { /* end of line, no more args */
1129 argv[nargs] = NULL;
1130#ifdef DEBUG_PARSER
1131 printf ("parse_line: nargs=%d\n", nargs);
1132#endif
1133 return (nargs);
1134 }
1135
1136 argv[nargs++] = line; /* begin of argument string */
1137
1138 /* find end of string */
Jason Hobbscafa1aa2011-08-23 11:06:54 +00001139 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001140 ++line;
wdenkc6097192002-11-03 00:24:07 +00001141
1142 if (*line == '\0') { /* end of line, no more args */
1143 argv[nargs] = NULL;
1144#ifdef DEBUG_PARSER
1145 printf ("parse_line: nargs=%d\n", nargs);
1146#endif
1147 return (nargs);
1148 }
1149
1150 *line++ = '\0'; /* terminate current arg */
1151 }
1152
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001153 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001154
1155#ifdef DEBUG_PARSER
1156 printf ("parse_line: nargs=%d\n", nargs);
1157#endif
1158 return (nargs);
1159}
1160
1161/****************************************************************************/
1162
Simon Glass82c21322012-02-14 19:59:22 +00001163#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001164static void process_macros (const char *input, char *output)
1165{
1166 char c, prev;
1167 const char *varname_start = NULL;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001168 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001169 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001170 int state = 0; /* 0 = waiting for '$' */
1171
1172 /* 1 = waiting for '(' or '{' */
1173 /* 2 = waiting for ')' or '}' */
1174 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001175#ifdef DEBUG_PARSER
1176 char *output_start = output;
1177
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001178 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1179 input);
wdenkc6097192002-11-03 00:24:07 +00001180#endif
1181
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001182 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001183
1184 while (inputcnt && outputcnt) {
wdenk57b2d802003-06-27 21:31:46 +00001185 c = *input++;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001186 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001187
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001188 if (state != 3) {
1189 /* remove one level of escape characters */
1190 if ((c == '\\') && (prev != '\\')) {
1191 if (inputcnt-- == 0)
1192 break;
1193 prev = c;
1194 c = *input++;
1195 }
wdenkc6097192002-11-03 00:24:07 +00001196 }
wdenkc6097192002-11-03 00:24:07 +00001197
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001198 switch (state) {
1199 case 0: /* Waiting for (unescaped) $ */
1200 if ((c == '\'') && (prev != '\\')) {
1201 state = 3;
1202 break;
1203 }
1204 if ((c == '$') && (prev != '\\')) {
1205 state++;
1206 } else {
wdenkc6097192002-11-03 00:24:07 +00001207 *(output++) = c;
1208 outputcnt--;
1209 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001210 break;
1211 case 1: /* Waiting for ( */
1212 if (c == '(' || c == '{') {
1213 state++;
1214 varname_start = input;
1215 } else {
1216 state = 0;
1217 *(output++) = '$';
1218 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001219
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001220 if (outputcnt) {
1221 *(output++) = c;
1222 outputcnt--;
1223 }
wdenkc6097192002-11-03 00:24:07 +00001224 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001225 break;
1226 case 2: /* Waiting for ) */
1227 if (c == ')' || c == '}') {
1228 int i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001229 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001230 int envcnt = input - varname_start - 1; /* Varname # of chars */
wdenkc6097192002-11-03 00:24:07 +00001231
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001232 /* Get the varname */
1233 for (i = 0; i < envcnt; i++) {
1234 envname[i] = varname_start[i];
wdenkc6097192002-11-03 00:24:07 +00001235 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001236 envname[i] = 0;
1237
1238 /* Get its value */
1239 envval = getenv (envname);
1240
1241 /* Copy into the line if it exists */
1242 if (envval != NULL)
1243 while ((*envval) && outputcnt) {
1244 *(output++) = *(envval++);
1245 outputcnt--;
1246 }
1247 /* Look for another '$' */
1248 state = 0;
1249 }
1250 break;
1251 case 3: /* Waiting for ' */
1252 if ((c == '\'') && (prev != '\\')) {
1253 state = 0;
1254 } else {
1255 *(output++) = c;
1256 outputcnt--;
1257 }
1258 break;
wdenkc6097192002-11-03 00:24:07 +00001259 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001260 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001261 }
1262
1263 if (outputcnt)
1264 *output = 0;
Bartlomiej Siekacadc8e82007-05-27 17:04:18 +02001265 else
1266 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001267
1268#ifdef DEBUG_PARSER
1269 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001270 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001271#endif
1272}
1273
1274/****************************************************************************
1275 * returns:
1276 * 1 - command executed, repeatable
1277 * 0 - command executed but not repeatable, interrupted commands are
1278 * always considered not repeatable
1279 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001280 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001281 * considered unrecognized)
1282 *
1283 * WARNING:
1284 *
1285 * We must create a temporary copy of the command since the command we get
1286 * may be the result from getenv(), which returns a pointer directly to
1287 * the environment data, which may change magicly when the command we run
1288 * creates or modifies environment variables (like "bootp" does).
1289 */
Simon Glassbf8c5b02012-02-14 19:59:21 +00001290static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001291{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001292 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001293 char *token; /* start of token in cmdbuf */
1294 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001295 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001296 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001297 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf4688a22003-05-28 08:06:31 +00001298 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001299 int repeatable = 1;
wdenkf4688a22003-05-28 08:06:31 +00001300 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001301
1302#ifdef DEBUG_PARSER
1303 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1304 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1305 puts ("\"\n");
1306#endif
1307
1308 clear_ctrlc(); /* forget any previous Control C */
1309
1310 if (!cmd || !*cmd) {
1311 return -1; /* empty command */
1312 }
1313
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001314 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001315 puts ("## Command too long!\n");
1316 return -1;
1317 }
1318
1319 strcpy (cmdbuf, cmd);
1320
1321 /* Process separators and check for invalid
1322 * repeatable commands
1323 */
1324
1325#ifdef DEBUG_PARSER
1326 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1327#endif
1328 while (*str) {
1329
1330 /*
1331 * Find separator, or string end
1332 * Allow simple escape of ';' by writing "\;"
1333 */
wdenk654868c2003-01-02 23:57:29 +00001334 for (inquotes = 0, sep = str; *sep; sep++) {
1335 if ((*sep=='\'') &&
1336 (*(sep-1) != '\\'))
1337 inquotes=!inquotes;
1338
1339 if (!inquotes &&
1340 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001341 ( sep != str) && /* past string start */
1342 (*(sep-1) != '\\')) /* and NOT escaped */
1343 break;
1344 }
1345
1346 /*
1347 * Limit the token to data between separators
1348 */
1349 token = str;
1350 if (*sep) {
1351 str = sep + 1; /* start of command for next pass */
1352 *sep = '\0';
1353 }
1354 else
1355 str = sep; /* no more commands for next pass */
1356#ifdef DEBUG_PARSER
1357 printf ("token: \"%s\"\n", token);
1358#endif
1359
1360 /* find macros in this token and replace them */
1361 process_macros (token, finaltoken);
1362
1363 /* Extract arguments */
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001364 if ((argc = parse_line (finaltoken, argv)) == 0) {
1365 rc = -1; /* no command at all */
1366 continue;
1367 }
wdenkc6097192002-11-03 00:24:07 +00001368
Timo Ketola9ba54722012-04-22 23:57:27 +00001369 if (cmd_process(flag, argc, argv, &repeatable))
1370 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001371
1372 /* Did the user stop this? */
1373 if (had_ctrlc ())
Detlev Zundeleeab5a12007-05-23 18:47:48 +02001374 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001375 }
1376
wdenkf4688a22003-05-28 08:06:31 +00001377 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001378}
Simon Glass82c21322012-02-14 19:59:22 +00001379#endif
wdenkc6097192002-11-03 00:24:07 +00001380
Simon Glassbf8c5b02012-02-14 19:59:21 +00001381/*
1382 * Run a command using the selected parser.
1383 *
1384 * @param cmd Command to run
1385 * @param flag Execution flags (CMD_FLAG_...)
1386 * @return 0 on success, or != 0 on error.
1387 */
1388int run_command(const char *cmd, int flag)
1389{
1390#ifndef CONFIG_SYS_HUSH_PARSER
1391 /*
1392 * builtin_run_command can return 0 or 1 for success, so clean up
1393 * its result.
1394 */
1395 if (builtin_run_command(cmd, flag) == -1)
1396 return 1;
1397
1398 return 0;
1399#else
1400 return parse_string_outer(cmd,
1401 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1402#endif
1403}
1404
Simon Glassa6642e02012-03-30 21:30:55 +00001405#ifndef CONFIG_SYS_HUSH_PARSER
1406/**
1407 * Execute a list of command separated by ; or \n using the built-in parser.
1408 *
1409 * This function cannot take a const char * for the command, since if it
1410 * finds newlines in the string, it replaces them with \0.
1411 *
1412 * @param cmd String containing list of commands
1413 * @param flag Execution flags (CMD_FLAG_...)
1414 * @return 0 on success, or != 0 on error.
1415 */
1416static int builtin_run_command_list(char *cmd, int flag)
1417{
1418 char *line, *next;
1419 int rcode = 0;
1420
1421 /*
1422 * Break into individual lines, and execute each line; terminate on
1423 * error.
1424 */
1425 line = next = cmd;
1426 while (*next) {
1427 if (*next == '\n') {
1428 *next = '\0';
1429 /* run only non-empty commands */
1430 if (*line) {
1431 debug("** exec: \"%s\"\n", line);
1432 if (builtin_run_command(line, 0) < 0) {
1433 rcode = 1;
1434 break;
1435 }
1436 }
1437 line = next + 1;
1438 }
1439 ++next;
1440 }
1441 if (rcode == 0 && *line)
1442 rcode = (builtin_run_command(line, 0) >= 0);
1443
1444 return rcode;
1445}
1446#endif
1447
1448int run_command_list(const char *cmd, int len, int flag)
1449{
1450 int need_buff = 1;
1451 char *buff = (char *)cmd; /* cast away const */
1452 int rcode = 0;
1453
1454 if (len == -1) {
1455 len = strlen(cmd);
1456#ifdef CONFIG_SYS_HUSH_PARSER
1457 /* hush will never change our string */
1458 need_buff = 0;
1459#else
1460 /* the built-in parser will change our string if it sees \n */
1461 need_buff = strchr(cmd, '\n') != NULL;
1462#endif
1463 }
1464 if (need_buff) {
1465 buff = malloc(len + 1);
1466 if (!buff)
1467 return 1;
1468 memcpy(buff, cmd, len);
1469 buff[len] = '\0';
1470 }
1471#ifdef CONFIG_SYS_HUSH_PARSER
1472 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1473#else
1474 /*
1475 * This function will overwrite any \n it sees with a \0, which
1476 * is why it can't work with a const char *. Here we are making
1477 * using of internal knowledge of this function, to avoid always
1478 * doing a malloc() which is actually required only in a case that
1479 * is pretty rare.
1480 */
1481 rcode = builtin_run_command_list(buff, flag);
1482 if (need_buff)
1483 free(buff);
1484#endif
1485
1486 return rcode;
1487}
1488
wdenkc6097192002-11-03 00:24:07 +00001489/****************************************************************************/
1490
Jon Loeliger052fc842007-07-08 18:10:08 -05001491#if defined(CONFIG_CMD_RUN)
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001492int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001493{
1494 int i;
wdenkc6097192002-11-03 00:24:07 +00001495
Wolfgang Denk3b683112010-07-17 01:06:04 +02001496 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001497 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001498
1499 for (i=1; i<argc; ++i) {
wdenkb02744a2003-04-05 00:53:31 +00001500 char *arg;
1501
1502 if ((arg = getenv (argv[i])) == NULL) {
1503 printf ("## Error: \"%s\" not defined\n", argv[i]);
1504 return 1;
1505 }
Jason Hobbsa56febb2011-08-31 05:37:24 +00001506
Simon Glass8161d042012-02-14 19:59:20 +00001507 if (run_command(arg, flag) != 0)
wdenkb02744a2003-04-05 00:53:31 +00001508 return 1;
wdenkc6097192002-11-03 00:24:07 +00001509 }
wdenkb02744a2003-04-05 00:53:31 +00001510 return 0;
wdenkc6097192002-11-03 00:24:07 +00001511}
Jon Loeligerd704d912007-07-10 11:02:44 -05001512#endif