blob: 61c6cac882150acaba23f032b2ef4bb14c0d4bca [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
Gabe Black3b734592012-10-25 16:31:08 +000048#ifdef CONFIG_OF_LIBFDT
49#include <fdt_support.h>
50#endif /* CONFIG_OF_LIBFDT */
51
wdenkbdccc4f2003-08-05 17:43:17 +000052#include <post.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000053#include <linux/ctype.h>
Heiko Schocher317d6c52012-01-16 21:13:35 +000054#include <menu.h>
wdenkbdccc4f2003-08-05 17:43:17 +000055
James Yang597f6c22008-05-05 10:22:53 -050056#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020057DECLARE_GLOBAL_DATA_PTR;
58#endif
59
Heiko Schocherfad63402007-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 Medve5e2c08c2009-05-12 13:48:32 -050064void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
Heiko Schocherfad63402007-07-13 09:54:17 +020065
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020066#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +000067int update_tftp (ulong addr);
Bartlomiej Sieka4bae9092008-10-01 15:26:31 +020068#endif /* CONFIG_UPDATE_TFTP */
wdenk8bde7f72003-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 Schmoller6475b9f2010-03-12 09:49:23 -060074char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
wdenkc6097192002-11-03 00:24:07 +000075
Stefan Roese3ca91222006-07-27 16:11:19 +020076static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
Mike Frysinger82359ae2010-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 Hobbsc8a20792011-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 Hobbsb41bc5a2011-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 Denk19973b62006-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 Denk19973b62006-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 Eibacha5aae0a2012-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 Roesef2302d42008-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 Korsgaardc3284b02008-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,
wdenk8bde7f72003-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 Korsgaardc3284b02008-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 Michlada4d402007-04-25 16:01:26 +0200207 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000208# endif
209
dzu8cb81432003-10-24 13:14:45 +0000210#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200211 if (abort)
212 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-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
wdenkc7de8292002-11-19 11:04:11 +0000220#ifdef CONFIG_MENUKEY
221static int menukey = 0;
222#endif
223
Jason Hobbsb41bc5a2011-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
wdenkc7de8292002-11-19 11:04:11 +0000231#ifdef CONFIG_MENUPROMPT
Stefan Roesef2302d42008-08-06 14:05:38 +0200232 printf(CONFIG_MENUPROMPT);
wdenkc7de8292002-11-19 11:04:11 +0000233#else
Joe Hershberger93d72122012-08-17 10:53:12 +0000234 if (bootdelay >= 0)
235 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000236#endif
wdenkc6097192002-11-03 00:24:07 +0000237
238#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-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 */
wdenk4b9206e2004-03-23 22:14:11 +0000246 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200247 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000248 }
wdenk8bde7f72003-06-27 21:31:46 +0000249 }
wdenkc6097192002-11-03 00:24:07 +0000250#endif
251
wdenkf72da342003-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 */
wdenkc7de8292002-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 */
wdenkc7de8292002-11-19 11:04:11 +0000265# endif
wdenkc6097192002-11-03 00:24:07 +0000266 break;
267 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200268 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000269 }
270
Ladislav Michlada4d402007-04-25 16:01:26 +0200271 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000272 }
273
Ladislav Michlada4d402007-04-25 16:01:26 +0200274 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000275
wdenkf72da342003-10-10 10:05:42 +0000276#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200277 if (abort)
278 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-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
Doug Anderson67e1ea22012-10-25 16:31:09 +0000286/*
287 * Runs the given boot command securely. Specifically:
288 * - Doesn't run the command with the shell (run_command or parse_string_outer),
289 * since that's a lot of code surface that an attacker might exploit.
290 * Because of this, we don't do any argument parsing--the secure boot command
291 * has to be a full-fledged u-boot command.
292 * - Doesn't check for keypresses before booting, since that could be a
293 * security hole; also disables Ctrl-C.
294 * - Doesn't allow the command to return.
295 *
296 * Upon any failures, this function will drop into an infinite loop after
297 * printing the error message to console.
298 */
299
300#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
301 defined(CONFIG_OF_CONTROL)
302static void secure_boot_cmd(char *cmd)
303{
304 cmd_tbl_t *cmdtp;
305 int rc;
306
307 if (!cmd) {
308 printf("## Error: Secure boot command not specified\n");
309 goto err;
310 }
311
312 /* Disable Ctrl-C just in case some command is used that checks it. */
313 disable_ctrlc(1);
314
315 /* Find the command directly. */
316 cmdtp = find_cmd(cmd);
317 if (!cmdtp) {
318 printf("## Error: \"%s\" not defined\n", cmd);
319 goto err;
320 }
321
322 /* Run the command, forcing no flags and faking argc and argv. */
323 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
324
325 /* Shouldn't ever return from boot command. */
326 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
327
328err:
329 /*
330 * Not a whole lot to do here. Rebooting won't help much, since we'll
331 * just end up right back here. Just loop.
332 */
333 hang();
334}
335
336#endif /* CONFIG_OF_CONTROL */
337
338
wdenkc6097192002-11-03 00:24:07 +0000339/****************************************************************************/
340
341void main_loop (void)
342{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200343#ifndef CONFIG_SYS_HUSH_PARSER
344 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
wdenkc6097192002-11-03 00:24:07 +0000345 int len;
346 int rc = 1;
347 int flag;
348#endif
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000349#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
350 defined(CONFIG_OF_CONTROL)
351 char *env;
352#endif
wdenkc6097192002-11-03 00:24:07 +0000353#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
354 char *s;
355 int bootdelay;
356#endif
357#ifdef CONFIG_PREBOOT
358 char *p;
359#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000360#ifdef CONFIG_BOOTCOUNT_LIMIT
361 unsigned long bootcount = 0;
362 unsigned long bootlimit = 0;
363 char *bcs;
364 char bcs_set[16];
365#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000366
wdenkbdccc4f2003-08-05 17:43:17 +0000367#ifdef CONFIG_BOOTCOUNT_LIMIT
368 bootcount = bootcount_load();
369 bootcount++;
370 bootcount_store (bootcount);
371 sprintf (bcs_set, "%lu", bootcount);
372 setenv ("bootcount", bcs_set);
373 bcs = getenv ("bootlimit");
374 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
375#endif /* CONFIG_BOOTCOUNT_LIMIT */
376
wdenkc6097192002-11-03 00:24:07 +0000377#ifdef CONFIG_MODEM_SUPPORT
378 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
379 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200380 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000381 setenv ("preboot", str); /* set or delete definition */
382 if (str != NULL)
383 free (str);
384 mdm_init(); /* wait for modem connection */
385 }
386#endif /* CONFIG_MODEM_SUPPORT */
387
stroese05875972003-04-04 15:44:49 +0000388#ifdef CONFIG_VERSION_VARIABLE
389 {
stroese155cb012003-07-11 08:00:33 +0000390 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000391 }
392#endif /* CONFIG_VERSION_VARIABLE */
393
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200394#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000395 u_boot_hush_start ();
396#endif
397
Heiko Schocher81473f62008-10-15 09:40:28 +0200398#if defined(CONFIG_HUSH_INIT_VAR)
399 hush_init_var ();
400#endif
401
wdenkc6097192002-11-03 00:24:07 +0000402#ifdef CONFIG_PREBOOT
403 if ((p = getenv ("preboot")) != NULL) {
404# ifdef CONFIG_AUTOBOOT_KEYED
405 int prev = disable_ctrlc(1); /* disable Control C checking */
406# endif
407
Simon Glass3a8a02b2012-03-30 21:30:56 +0000408 run_command_list(p, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000409
410# ifdef CONFIG_AUTOBOOT_KEYED
411 disable_ctrlc(prev); /* restore Control C checking */
412# endif
413 }
414#endif /* CONFIG_PREBOOT */
415
Wolfgang Denk143cd212010-03-11 23:56:03 +0100416#if defined(CONFIG_UPDATE_TFTP)
Andreas Pretzsch8d6b7322011-07-16 05:50:59 +0000417 update_tftp (0UL);
Wolfgang Denk143cd212010-03-11 23:56:03 +0100418#endif /* CONFIG_UPDATE_TFTP */
419
wdenkc6097192002-11-03 00:24:07 +0000420#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
421 s = getenv ("bootdelay");
422 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
423
wdenka6c7ad22002-12-03 21:28:10 +0000424 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000425
Heiko Schocher317d6c52012-01-16 21:13:35 +0000426#if defined(CONFIG_MENU_SHOW)
427 bootdelay = menu_show(bootdelay);
428#endif
wdenkc6097192002-11-03 00:24:07 +0000429# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000430 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000431# endif /* CONFIG_BOOT_RETRY_TIME */
432
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100433#ifdef CONFIG_POST
434 if (gd->flags & GD_FLG_POSTFAIL) {
435 s = getenv("failbootcmd");
436 }
437 else
438#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000439#ifdef CONFIG_BOOTCOUNT_LIMIT
440 if (bootlimit && (bootcount > bootlimit)) {
441 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
442 (unsigned)bootlimit);
443 s = getenv ("altbootcmd");
444 }
445 else
446#endif /* CONFIG_BOOTCOUNT_LIMIT */
447 s = getenv ("bootcmd");
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000448#ifdef CONFIG_OF_CONTROL
449 /* Allow the fdt to override the boot command */
450 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
451 if (env)
452 s = env;
Doug Anderson67e1ea22012-10-25 16:31:09 +0000453
454 /*
455 * If the bootsecure option was chosen, use secure_boot_cmd().
456 * Always use 'env' in this case, since bootsecure requres that the
457 * bootcmd was specified in the FDT too.
458 */
459 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
460 secure_boot_cmd(env);
461
Che-Liang Chiou224b72e2012-10-25 16:31:07 +0000462#endif /* CONFIG_OF_CONTROL */
wdenka6c7ad22002-12-03 21:28:10 +0000463
464 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
465
Joe Hershberger93d72122012-08-17 10:53:12 +0000466 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
wdenkc6097192002-11-03 00:24:07 +0000467# ifdef CONFIG_AUTOBOOT_KEYED
468 int prev = disable_ctrlc(1); /* disable Control C checking */
469# endif
470
Simon Glass3a8a02b2012-03-30 21:30:56 +0000471 run_command_list(s, -1, 0);
wdenkc6097192002-11-03 00:24:07 +0000472
473# ifdef CONFIG_AUTOBOOT_KEYED
474 disable_ctrlc(prev); /* restore Control C checking */
475# endif
476 }
wdenkc7de8292002-11-19 11:04:11 +0000477
478# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000479 if (menukey == CONFIG_MENUKEY) {
Jason Hobbs370d1e32011-06-23 08:27:30 +0000480 s = getenv("menucmd");
Jason Hobbsc8a20792011-08-31 05:37:24 +0000481 if (s)
Simon Glass3a8a02b2012-03-30 21:30:56 +0000482 run_command_list(s, -1, 0);
wdenkc7de8292002-11-19 11:04:11 +0000483 }
484#endif /* CONFIG_MENUKEY */
Wolfgang Denk953b7e62010-06-13 18:28:54 +0200485#endif /* CONFIG_BOOTDELAY */
wdenkc7de8292002-11-19 11:04:11 +0000486
Gabe Black3b734592012-10-25 16:31:08 +0000487#if defined CONFIG_OF_CONTROL
488 set_working_fdt_addr((void *)gd->fdt_blob);
489#endif /* CONFIG_OF_CONTROL */
490
wdenkc6097192002-11-03 00:24:07 +0000491 /*
492 * Main Loop for Monitor Command Processing
493 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200494#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +0000495 parse_file_outer();
496 /* This point is never reached */
497 for (;;);
498#else
499 for (;;) {
500#ifdef CONFIG_BOOT_RETRY_TIME
501 if (rc >= 0) {
502 /* Saw enough of a valid command to
503 * restart the timeout.
504 */
505 reset_cmd_timeout();
506 }
507#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200508 len = readline (CONFIG_SYS_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000509
510 flag = 0; /* assume no special flags for now */
511 if (len > 0)
512 strcpy (lastcommand, console_buffer);
513 else if (len == 0)
514 flag |= CMD_FLAG_REPEAT;
515#ifdef CONFIG_BOOT_RETRY_TIME
516 else if (len == -2) {
517 /* -2 means timed out, retry autoboot
518 */
wdenk4b9206e2004-03-23 22:14:11 +0000519 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000520# ifdef CONFIG_RESET_TO_RETRY
521 /* Reinit board to run initialization code again */
522 do_reset (NULL, 0, 0, NULL);
523# else
524 return; /* retry autoboot */
525# endif
526 }
527#endif
528
529 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000530 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000531 else
Simon Glass53071532012-02-14 19:59:21 +0000532 rc = run_command(lastcommand, flag);
wdenkc6097192002-11-03 00:24:07 +0000533
534 if (rc <= 0) {
535 /* invalid command or not repeatable, forget it */
536 lastcommand[0] = 0;
537 }
538 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200539#endif /*CONFIG_SYS_HUSH_PARSER*/
wdenkc6097192002-11-03 00:24:07 +0000540}
541
wdenk6dd652f2003-06-19 23:40:20 +0000542#ifdef CONFIG_BOOT_RETRY_TIME
543/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200544 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000545 */
546void init_cmd_timeout(void)
547{
548 char *s = getenv ("bootretry");
549
550 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000551 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000552 else
553 retry_time = CONFIG_BOOT_RETRY_TIME;
554
555 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
556 retry_time = CONFIG_BOOT_RETRY_MIN;
557}
558
wdenkc6097192002-11-03 00:24:07 +0000559/***************************************************************************
560 * reset command line timeout to retry_time seconds
561 */
wdenkc6097192002-11-03 00:24:07 +0000562void reset_cmd_timeout(void)
563{
564 endtime = endtick(retry_time);
565}
566#endif
567
Wolfgang Denk501090a2006-07-21 11:33:45 +0200568#ifdef CONFIG_CMDLINE_EDITING
569
570/*
571 * cmdline-editing related codes from vivi.
572 * Author: Janghoon Lyu <nandy@mizi.com>
573 */
574
Wolfgang Denk501090a2006-07-21 11:33:45 +0200575#define putnstr(str,n) do { \
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700576 printf ("%.*s", (int)n, str); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200577 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200578
579#define CTL_CH(c) ((c) - 'a' + 1)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200580#define CTL_BACKSPACE ('\b')
581#define DEL ((char)255)
582#define DEL7 ((char)127)
583#define CREAD_HIST_CHAR ('!')
584
585#define getcmd_putch(ch) putc(ch)
586#define getcmd_getch() getc()
587#define getcmd_cbeep() getcmd_putch('\a')
588
589#define HIST_MAX 20
Peter Tyser8804ae32010-09-29 13:30:56 -0500590#define HIST_SIZE CONFIG_SYS_CBSIZE
Wolfgang Denk501090a2006-07-21 11:33:45 +0200591
Kim Phillips199adb62012-10-29 13:34:32 +0000592static int hist_max;
593static int hist_add_idx;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200594static int hist_cur = -1;
Kim Phillips199adb62012-10-29 13:34:32 +0000595static unsigned hist_num;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200596
Kim Phillips199adb62012-10-29 13:34:32 +0000597static char *hist_list[HIST_MAX];
598static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200599
600#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
601
602static void hist_init(void)
603{
604 int i;
605
606 hist_max = 0;
607 hist_add_idx = 0;
608 hist_cur = -1;
609 hist_num = 0;
610
611 for (i = 0; i < HIST_MAX; i++) {
612 hist_list[i] = hist_lines[i];
613 hist_list[i][0] = '\0';
614 }
615}
616
617static void cread_add_to_hist(char *line)
618{
619 strcpy(hist_list[hist_add_idx], line);
620
621 if (++hist_add_idx >= HIST_MAX)
622 hist_add_idx = 0;
623
624 if (hist_add_idx > hist_max)
625 hist_max = hist_add_idx;
626
627 hist_num++;
628}
629
630static char* hist_prev(void)
631{
632 char *ret;
633 int old_cur;
634
635 if (hist_cur < 0)
636 return NULL;
637
638 old_cur = hist_cur;
639 if (--hist_cur < 0)
640 hist_cur = hist_max;
641
642 if (hist_cur == hist_add_idx) {
643 hist_cur = old_cur;
644 ret = NULL;
645 } else
646 ret = hist_list[hist_cur];
647
648 return (ret);
649}
650
651static char* hist_next(void)
652{
653 char *ret;
654
655 if (hist_cur < 0)
656 return NULL;
657
658 if (hist_cur == hist_add_idx)
659 return NULL;
660
661 if (++hist_cur > hist_max)
662 hist_cur = 0;
663
664 if (hist_cur == hist_add_idx) {
665 ret = "";
666 } else
667 ret = hist_list[hist_cur];
668
669 return (ret);
670}
671
Stefan Roese3ca91222006-07-27 16:11:19 +0200672#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200673static void cread_print_hist_list(void)
674{
675 int i;
676 unsigned long n;
677
678 n = hist_num - hist_max;
679
680 i = hist_add_idx + 1;
681 while (1) {
682 if (i > hist_max)
683 i = 0;
684 if (i == hist_add_idx)
685 break;
686 printf("%s\n", hist_list[i]);
687 n++;
688 i++;
689 }
690}
Stefan Roese3ca91222006-07-27 16:11:19 +0200691#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200692
693#define BEGINNING_OF_LINE() { \
694 while (num) { \
695 getcmd_putch(CTL_BACKSPACE); \
696 num--; \
697 } \
698}
699
700#define ERASE_TO_EOL() { \
701 if (num < eol_num) { \
Mike Frysinger8faba482010-07-23 05:28:15 -0400702 printf("%*s", (int)(eol_num - num), ""); \
703 do { \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200704 getcmd_putch(CTL_BACKSPACE); \
Mike Frysinger8faba482010-07-23 05:28:15 -0400705 } while (--eol_num > num); \
Wolfgang Denk501090a2006-07-21 11:33:45 +0200706 } \
707}
708
709#define REFRESH_TO_EOL() { \
710 if (num < eol_num) { \
711 wlen = eol_num - num; \
712 putnstr(buf + num, wlen); \
713 num = eol_num; \
714 } \
715}
716
717static void cread_add_char(char ichar, int insert, unsigned long *num,
718 unsigned long *eol_num, char *buf, unsigned long len)
719{
720 unsigned long wlen;
721
722 /* room ??? */
723 if (insert || *num == *eol_num) {
724 if (*eol_num > len - 1) {
725 getcmd_cbeep();
726 return;
727 }
728 (*eol_num)++;
729 }
730
731 if (insert) {
732 wlen = *eol_num - *num;
733 if (wlen > 1) {
734 memmove(&buf[*num+1], &buf[*num], wlen-1);
735 }
736
737 buf[*num] = ichar;
738 putnstr(buf + *num, wlen);
739 (*num)++;
740 while (--wlen) {
741 getcmd_putch(CTL_BACKSPACE);
742 }
743 } else {
744 /* echo the character */
745 wlen = 1;
746 buf[*num] = ichar;
747 putnstr(buf + *num, wlen);
748 (*num)++;
749 }
750}
751
752static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
753 unsigned long *eol_num, char *buf, unsigned long len)
754{
755 while (strsize--) {
756 cread_add_char(*str, insert, num, eol_num, buf, len);
757 str++;
758 }
759}
760
Heiko Schocher9c348312012-01-16 21:13:05 +0000761static int cread_line(const char *const prompt, char *buf, unsigned int *len,
762 int timeout)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200763{
764 unsigned long num = 0;
765 unsigned long eol_num = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200766 unsigned long wlen;
767 char ichar;
768 int insert = 1;
769 int esc_len = 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200770 char esc_save[8];
Peter Tyserecc55002009-10-25 15:12:54 -0500771 int init_len = strlen(buf);
Heiko Schocher9c348312012-01-16 21:13:05 +0000772 int first = 1;
Peter Tyserecc55002009-10-25 15:12:54 -0500773
774 if (init_len)
775 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200776
777 while (1) {
Andreas Engel00ac50e2008-01-09 17:10:56 +0100778#ifdef CONFIG_BOOT_RETRY_TIME
779 while (!tstc()) { /* while no incoming data */
780 if (retry_time >= 0 && get_ticks() > endtime)
781 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +0200782 WATCHDOG_RESET();
Andreas Engel00ac50e2008-01-09 17:10:56 +0100783 }
784#endif
Heiko Schocher9c348312012-01-16 21:13:05 +0000785 if (first && timeout) {
786 uint64_t etime = endtick(timeout);
787
788 while (!tstc()) { /* while no incoming data */
789 if (get_ticks() >= etime)
790 return -2; /* timed out */
791 WATCHDOG_RESET();
792 }
793 first = 0;
794 }
Andreas Engel00ac50e2008-01-09 17:10:56 +0100795
Wolfgang Denk501090a2006-07-21 11:33:45 +0200796 ichar = getcmd_getch();
797
798 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200799 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200800 break;
801 }
802
803 /*
804 * handle standard linux xterm esc sequences for arrow key, etc.
805 */
806 if (esc_len != 0) {
807 if (esc_len == 1) {
808 if (ichar == '[') {
809 esc_save[esc_len] = ichar;
810 esc_len = 2;
811 } else {
812 cread_add_str(esc_save, esc_len, insert,
813 &num, &eol_num, buf, *len);
814 esc_len = 0;
815 }
816 continue;
817 }
818
819 switch (ichar) {
820
821 case 'D': /* <- key */
822 ichar = CTL_CH('b');
823 esc_len = 0;
824 break;
825 case 'C': /* -> key */
826 ichar = CTL_CH('f');
827 esc_len = 0;
828 break; /* pass off to ^F handler */
829 case 'H': /* Home key */
830 ichar = CTL_CH('a');
831 esc_len = 0;
832 break; /* pass off to ^A handler */
833 case 'A': /* up arrow */
834 ichar = CTL_CH('p');
835 esc_len = 0;
836 break; /* pass off to ^P handler */
837 case 'B': /* down arrow */
838 ichar = CTL_CH('n');
839 esc_len = 0;
840 break; /* pass off to ^N handler */
841 default:
842 esc_save[esc_len++] = ichar;
843 cread_add_str(esc_save, esc_len, insert,
844 &num, &eol_num, buf, *len);
845 esc_len = 0;
846 continue;
847 }
848 }
849
850 switch (ichar) {
851 case 0x1b:
852 if (esc_len == 0) {
853 esc_save[esc_len] = ichar;
854 esc_len = 1;
855 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200856 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200857 esc_len = 0;
858 }
859 break;
860
861 case CTL_CH('a'):
862 BEGINNING_OF_LINE();
863 break;
864 case CTL_CH('c'): /* ^C - break */
865 *buf = '\0'; /* discard input */
866 return (-1);
867 case CTL_CH('f'):
868 if (num < eol_num) {
869 getcmd_putch(buf[num]);
870 num++;
871 }
872 break;
873 case CTL_CH('b'):
874 if (num) {
875 getcmd_putch(CTL_BACKSPACE);
876 num--;
877 }
878 break;
879 case CTL_CH('d'):
880 if (num < eol_num) {
881 wlen = eol_num - num - 1;
882 if (wlen) {
883 memmove(&buf[num], &buf[num+1], wlen);
884 putnstr(buf + num, wlen);
885 }
886
887 getcmd_putch(' ');
888 do {
889 getcmd_putch(CTL_BACKSPACE);
890 } while (wlen--);
891 eol_num--;
892 }
893 break;
894 case CTL_CH('k'):
895 ERASE_TO_EOL();
896 break;
897 case CTL_CH('e'):
898 REFRESH_TO_EOL();
899 break;
900 case CTL_CH('o'):
901 insert = !insert;
902 break;
903 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100904 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200905 BEGINNING_OF_LINE();
906 ERASE_TO_EOL();
907 break;
908 case DEL:
909 case DEL7:
910 case 8:
911 if (num) {
912 wlen = eol_num - num;
913 num--;
914 memmove(&buf[num], &buf[num+1], wlen);
915 getcmd_putch(CTL_BACKSPACE);
916 putnstr(buf + num, wlen);
917 getcmd_putch(' ');
918 do {
919 getcmd_putch(CTL_BACKSPACE);
920 } while (wlen--);
921 eol_num--;
922 }
923 break;
924 case CTL_CH('p'):
925 case CTL_CH('n'):
926 {
927 char * hline;
928
929 esc_len = 0;
930
931 if (ichar == CTL_CH('p'))
932 hline = hist_prev();
933 else
934 hline = hist_next();
935
936 if (!hline) {
937 getcmd_cbeep();
938 continue;
939 }
940
941 /* nuke the current line */
942 /* first, go home */
943 BEGINNING_OF_LINE();
944
945 /* erase to end of line */
946 ERASE_TO_EOL();
947
948 /* copy new line into place and display */
949 strcpy(buf, hline);
950 eol_num = strlen(buf);
951 REFRESH_TO_EOL();
952 continue;
953 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100954#ifdef CONFIG_AUTO_COMPLETE
955 case '\t': {
956 int num2, col;
957
958 /* do not autocomplete when in the middle */
959 if (num < eol_num) {
960 getcmd_cbeep();
961 break;
962 }
963
964 buf[num] = '\0';
965 col = strlen(prompt) + eol_num;
966 num2 = num;
967 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
968 col = num2 - num;
969 num += col;
970 eol_num += col;
971 }
972 break;
973 }
974#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200975 default:
976 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
977 break;
978 }
979 }
980 *len = eol_num;
981 buf[eol_num] = '\0'; /* lose the newline */
982
983 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
984 cread_add_to_hist(buf);
985 hist_cur = hist_add_idx;
986
Peter Tyserf9239432009-10-25 15:12:53 -0500987 return 0;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200988}
989
990#endif /* CONFIG_CMDLINE_EDITING */
991
wdenkc6097192002-11-03 00:24:07 +0000992/****************************************************************************/
993
994/*
995 * Prompt for input and read a line.
996 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
997 * time out when time goes past endtime (timebase time in ticks).
998 * Return: number of read characters
999 * -1 if break
1000 * -2 if timed out
1001 */
1002int readline (const char *const prompt)
1003{
Peter Tyserecc55002009-10-25 15:12:54 -05001004 /*
1005 * If console_buffer isn't 0-length the user will be prompted to modify
1006 * it instead of entering it from scratch as desired.
1007 */
1008 console_buffer[0] = '\0';
1009
Heiko Schocher9c348312012-01-16 21:13:05 +00001010 return readline_into_buffer(prompt, console_buffer, 0);
James Yang6636b622008-01-09 11:17:49 -06001011}
1012
1013
Heiko Schocher9c348312012-01-16 21:13:05 +00001014int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
James Yang6636b622008-01-09 11:17:49 -06001015{
1016 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001017#ifdef CONFIG_CMDLINE_EDITING
Peter Tyser8804ae32010-09-29 13:30:56 -05001018 unsigned int len = CONFIG_SYS_CBSIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +02001019 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +02001020 static int initted = 0;
1021
James Yang597f6c22008-05-05 10:22:53 -05001022 /*
1023 * History uses a global array which is not
1024 * writable until after relocation to RAM.
1025 * Revert to non-history version if still
1026 * running from flash.
1027 */
1028 if (gd->flags & GD_FLG_RELOC) {
1029 if (!initted) {
1030 hist_init();
1031 initted = 1;
1032 }
1033
Peter Tysere491a712009-10-25 15:12:52 -05001034 if (prompt)
1035 puts (prompt);
James Yang597f6c22008-05-05 10:22:53 -05001036
Heiko Schocher9c348312012-01-16 21:13:05 +00001037 rc = cread_line(prompt, p, &len, timeout);
James Yang597f6c22008-05-05 10:22:53 -05001038 return rc < 0 ? rc : len;
1039
1040 } else {
1041#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -06001042 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +00001043 int n = 0; /* buffer index */
1044 int plen = 0; /* prompt length */
1045 int col; /* output column cnt */
1046 char c;
1047
1048 /* print prompt */
1049 if (prompt) {
1050 plen = strlen (prompt);
1051 puts (prompt);
1052 }
1053 col = plen;
1054
1055 for (;;) {
1056#ifdef CONFIG_BOOT_RETRY_TIME
1057 while (!tstc()) { /* while no incoming data */
1058 if (retry_time >= 0 && get_ticks() > endtime)
1059 return (-2); /* timed out */
Jens Scharsig30dc1652010-04-09 19:02:38 +02001060 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001061 }
1062#endif
1063 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1064
1065#ifdef CONFIG_SHOW_ACTIVITY
1066 while (!tstc()) {
wdenkc6097192002-11-03 00:24:07 +00001067 show_activity(0);
Jens Scharsig30dc1652010-04-09 19:02:38 +02001068 WATCHDOG_RESET();
wdenkc6097192002-11-03 00:24:07 +00001069 }
1070#endif
1071 c = getc();
1072
1073 /*
1074 * Special character handling
1075 */
1076 switch (c) {
1077 case '\r': /* Enter */
1078 case '\n':
1079 *p = '\0';
1080 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -06001081 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001082
wdenk27aa8182004-03-23 22:37:33 +00001083 case '\0': /* nul */
1084 continue;
1085
wdenkc6097192002-11-03 00:24:07 +00001086 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001087 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001088 return (-1);
1089
1090 case 0x15: /* ^U - erase line */
1091 while (col > plen) {
1092 puts (erase_seq);
1093 --col;
1094 }
James Yang6636b622008-01-09 11:17:49 -06001095 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001096 n = 0;
1097 continue;
1098
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001099 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001100 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001101 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001102 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001103 }
1104 continue;
1105
1106 case 0x08: /* ^H - backspace */
1107 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001108 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001109 continue;
1110
1111 default:
1112 /*
1113 * Must be a normal character then
1114 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001115 if (n < CONFIG_SYS_CBSIZE-2) {
wdenkc6097192002-11-03 00:24:07 +00001116 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001117#ifdef CONFIG_AUTO_COMPLETE
1118 /* if auto completion triggered just continue */
1119 *p = '\0';
1120 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001121 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001122 continue;
1123 }
1124#endif
wdenkc6097192002-11-03 00:24:07 +00001125 puts (tab_seq+(col&07));
1126 col += 8 - (col&07);
1127 } else {
1128 ++col; /* echo input */
1129 putc (c);
1130 }
1131 *p++ = c;
1132 ++n;
1133 } else { /* Buffer full */
1134 putc ('\a');
1135 }
1136 }
1137 }
James Yang597f6c22008-05-05 10:22:53 -05001138#ifdef CONFIG_CMDLINE_EDITING
1139 }
1140#endif
wdenkc6097192002-11-03 00:24:07 +00001141}
1142
1143/****************************************************************************/
1144
1145static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1146{
1147 char *s;
1148
1149 if (*np == 0) {
1150 return (p);
1151 }
1152
1153 if (*(--p) == '\t') { /* will retype the whole line */
1154 while (*colp > plen) {
1155 puts (erase_seq);
1156 (*colp)--;
1157 }
1158 for (s=buffer; s<p; ++s) {
1159 if (*s == '\t') {
1160 puts (tab_seq+((*colp) & 07));
1161 *colp += 8 - ((*colp) & 07);
1162 } else {
1163 ++(*colp);
1164 putc (*s);
1165 }
1166 }
1167 } else {
1168 puts (erase_seq);
1169 (*colp)--;
1170 }
1171 (*np)--;
1172 return (p);
1173}
1174
1175/****************************************************************************/
1176
1177int parse_line (char *line, char *argv[])
1178{
1179 int nargs = 0;
1180
1181#ifdef DEBUG_PARSER
1182 printf ("parse_line: \"%s\"\n", line);
1183#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001184 while (nargs < CONFIG_SYS_MAXARGS) {
wdenkc6097192002-11-03 00:24:07 +00001185
1186 /* skip any white space */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001187 while (isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001188 ++line;
wdenkc6097192002-11-03 00:24:07 +00001189
1190 if (*line == '\0') { /* end of line, no more args */
1191 argv[nargs] = NULL;
1192#ifdef DEBUG_PARSER
1193 printf ("parse_line: nargs=%d\n", nargs);
1194#endif
1195 return (nargs);
1196 }
1197
1198 argv[nargs++] = line; /* begin of argument string */
1199
1200 /* find end of string */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +00001201 while (*line && !isblank(*line))
wdenkc6097192002-11-03 00:24:07 +00001202 ++line;
wdenkc6097192002-11-03 00:24:07 +00001203
1204 if (*line == '\0') { /* end of line, no more args */
1205 argv[nargs] = NULL;
1206#ifdef DEBUG_PARSER
1207 printf ("parse_line: nargs=%d\n", nargs);
1208#endif
1209 return (nargs);
1210 }
1211
1212 *line++ = '\0'; /* terminate current arg */
1213 }
1214
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001215 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
wdenkc6097192002-11-03 00:24:07 +00001216
1217#ifdef DEBUG_PARSER
1218 printf ("parse_line: nargs=%d\n", nargs);
1219#endif
1220 return (nargs);
1221}
1222
1223/****************************************************************************/
1224
Simon Glass7fed89e2012-02-14 19:59:22 +00001225#ifndef CONFIG_SYS_HUSH_PARSER
wdenkc6097192002-11-03 00:24:07 +00001226static void process_macros (const char *input, char *output)
1227{
1228 char c, prev;
1229 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001230 int inputcnt = strlen (input);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001231 int outputcnt = CONFIG_SYS_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001232 int state = 0; /* 0 = waiting for '$' */
1233
1234 /* 1 = waiting for '(' or '{' */
1235 /* 2 = waiting for ')' or '}' */
1236 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001237#ifdef DEBUG_PARSER
1238 char *output_start = output;
1239
Wolfgang Denk19973b62006-10-28 00:38:39 +02001240 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1241 input);
wdenkc6097192002-11-03 00:24:07 +00001242#endif
1243
Wolfgang Denk19973b62006-10-28 00:38:39 +02001244 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001245
1246 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001247 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001248 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001249
Wolfgang Denk19973b62006-10-28 00:38:39 +02001250 if (state != 3) {
1251 /* remove one level of escape characters */
1252 if ((c == '\\') && (prev != '\\')) {
1253 if (inputcnt-- == 0)
1254 break;
1255 prev = c;
1256 c = *input++;
1257 }
wdenka25f8622003-01-02 23:57:29 +00001258 }
wdenkc6097192002-11-03 00:24:07 +00001259
Wolfgang Denk19973b62006-10-28 00:38:39 +02001260 switch (state) {
1261 case 0: /* Waiting for (unescaped) $ */
1262 if ((c == '\'') && (prev != '\\')) {
1263 state = 3;
1264 break;
1265 }
1266 if ((c == '$') && (prev != '\\')) {
1267 state++;
1268 } else {
wdenkc6097192002-11-03 00:24:07 +00001269 *(output++) = c;
1270 outputcnt--;
1271 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001272 break;
1273 case 1: /* Waiting for ( */
1274 if (c == '(' || c == '{') {
1275 state++;
1276 varname_start = input;
1277 } else {
1278 state = 0;
1279 *(output++) = '$';
1280 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001281
Wolfgang Denk19973b62006-10-28 00:38:39 +02001282 if (outputcnt) {
1283 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001284 outputcnt--;
1285 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001286 }
1287 break;
1288 case 2: /* Waiting for ) */
1289 if (c == ')' || c == '}') {
1290 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001291 char envname[CONFIG_SYS_CBSIZE], *envval;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001292 int envcnt = input - varname_start - 1; /* Varname # of chars */
1293
1294 /* Get the varname */
1295 for (i = 0; i < envcnt; i++) {
1296 envname[i] = varname_start[i];
1297 }
1298 envname[i] = 0;
1299
1300 /* Get its value */
1301 envval = getenv (envname);
1302
1303 /* Copy into the line if it exists */
1304 if (envval != NULL)
1305 while ((*envval) && outputcnt) {
1306 *(output++) = *(envval++);
1307 outputcnt--;
1308 }
1309 /* Look for another '$' */
1310 state = 0;
1311 }
1312 break;
1313 case 3: /* Waiting for ' */
1314 if ((c == '\'') && (prev != '\\')) {
1315 state = 0;
1316 } else {
1317 *(output++) = c;
1318 outputcnt--;
1319 }
1320 break;
wdenkc6097192002-11-03 00:24:07 +00001321 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001322 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001323 }
1324
1325 if (outputcnt)
1326 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001327 else
1328 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001329
1330#ifdef DEBUG_PARSER
1331 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001332 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001333#endif
1334}
1335
1336/****************************************************************************
1337 * returns:
1338 * 1 - command executed, repeatable
1339 * 0 - command executed but not repeatable, interrupted commands are
1340 * always considered not repeatable
1341 * -1 - not executed (unrecognized, bootd recursion or too many args)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001342 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
wdenkc6097192002-11-03 00:24:07 +00001343 * considered unrecognized)
1344 *
1345 * WARNING:
1346 *
1347 * We must create a temporary copy of the command since the command we get
1348 * may be the result from getenv(), which returns a pointer directly to
1349 * the environment data, which may change magicly when the command we run
1350 * creates or modifies environment variables (like "bootp" does).
1351 */
Simon Glass53071532012-02-14 19:59:21 +00001352static int builtin_run_command(const char *cmd, int flag)
wdenkc6097192002-11-03 00:24:07 +00001353{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001354 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
wdenkc6097192002-11-03 00:24:07 +00001355 char *token; /* start of token in cmdbuf */
1356 char *sep; /* end of token (separator) in cmdbuf */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001357 char finaltoken[CONFIG_SYS_CBSIZE];
wdenkc6097192002-11-03 00:24:07 +00001358 char *str = cmdbuf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001359 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001360 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001361 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001362 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001363
1364#ifdef DEBUG_PARSER
1365 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1366 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1367 puts ("\"\n");
1368#endif
1369
1370 clear_ctrlc(); /* forget any previous Control C */
1371
1372 if (!cmd || !*cmd) {
1373 return -1; /* empty command */
1374 }
1375
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001376 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
wdenkc6097192002-11-03 00:24:07 +00001377 puts ("## Command too long!\n");
1378 return -1;
1379 }
1380
1381 strcpy (cmdbuf, cmd);
1382
1383 /* Process separators and check for invalid
1384 * repeatable commands
1385 */
1386
1387#ifdef DEBUG_PARSER
1388 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1389#endif
1390 while (*str) {
1391
1392 /*
1393 * Find separator, or string end
1394 * Allow simple escape of ';' by writing "\;"
1395 */
wdenka25f8622003-01-02 23:57:29 +00001396 for (inquotes = 0, sep = str; *sep; sep++) {
1397 if ((*sep=='\'') &&
1398 (*(sep-1) != '\\'))
1399 inquotes=!inquotes;
1400
1401 if (!inquotes &&
1402 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001403 ( sep != str) && /* past string start */
1404 (*(sep-1) != '\\')) /* and NOT escaped */
1405 break;
1406 }
1407
1408 /*
1409 * Limit the token to data between separators
1410 */
1411 token = str;
1412 if (*sep) {
1413 str = sep + 1; /* start of command for next pass */
1414 *sep = '\0';
1415 }
1416 else
1417 str = sep; /* no more commands for next pass */
1418#ifdef DEBUG_PARSER
1419 printf ("token: \"%s\"\n", token);
1420#endif
1421
1422 /* find macros in this token and replace them */
1423 process_macros (token, finaltoken);
1424
1425 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001426 if ((argc = parse_line (finaltoken, argv)) == 0) {
1427 rc = -1; /* no command at all */
1428 continue;
1429 }
wdenkc6097192002-11-03 00:24:07 +00001430
Timo Ketola030fca52012-04-22 23:57:27 +00001431 if (cmd_process(flag, argc, argv, &repeatable))
1432 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001433
1434 /* Did the user stop this? */
1435 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001436 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001437 }
1438
wdenkf07771c2003-05-28 08:06:31 +00001439 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001440}
Simon Glass7fed89e2012-02-14 19:59:22 +00001441#endif
wdenkc6097192002-11-03 00:24:07 +00001442
Simon Glass53071532012-02-14 19:59:21 +00001443/*
1444 * Run a command using the selected parser.
1445 *
1446 * @param cmd Command to run
1447 * @param flag Execution flags (CMD_FLAG_...)
1448 * @return 0 on success, or != 0 on error.
1449 */
1450int run_command(const char *cmd, int flag)
1451{
1452#ifndef CONFIG_SYS_HUSH_PARSER
1453 /*
1454 * builtin_run_command can return 0 or 1 for success, so clean up
1455 * its result.
1456 */
1457 if (builtin_run_command(cmd, flag) == -1)
1458 return 1;
1459
1460 return 0;
1461#else
1462 return parse_string_outer(cmd,
1463 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1464#endif
1465}
1466
Simon Glassd51004a2012-03-30 21:30:55 +00001467#ifndef CONFIG_SYS_HUSH_PARSER
1468/**
1469 * Execute a list of command separated by ; or \n using the built-in parser.
1470 *
1471 * This function cannot take a const char * for the command, since if it
1472 * finds newlines in the string, it replaces them with \0.
1473 *
1474 * @param cmd String containing list of commands
1475 * @param flag Execution flags (CMD_FLAG_...)
1476 * @return 0 on success, or != 0 on error.
1477 */
1478static int builtin_run_command_list(char *cmd, int flag)
1479{
1480 char *line, *next;
1481 int rcode = 0;
1482
1483 /*
1484 * Break into individual lines, and execute each line; terminate on
1485 * error.
1486 */
1487 line = next = cmd;
1488 while (*next) {
1489 if (*next == '\n') {
1490 *next = '\0';
1491 /* run only non-empty commands */
1492 if (*line) {
1493 debug("** exec: \"%s\"\n", line);
1494 if (builtin_run_command(line, 0) < 0) {
1495 rcode = 1;
1496 break;
1497 }
1498 }
1499 line = next + 1;
1500 }
1501 ++next;
1502 }
1503 if (rcode == 0 && *line)
1504 rcode = (builtin_run_command(line, 0) >= 0);
1505
1506 return rcode;
1507}
1508#endif
1509
1510int run_command_list(const char *cmd, int len, int flag)
1511{
1512 int need_buff = 1;
1513 char *buff = (char *)cmd; /* cast away const */
1514 int rcode = 0;
1515
1516 if (len == -1) {
1517 len = strlen(cmd);
1518#ifdef CONFIG_SYS_HUSH_PARSER
1519 /* hush will never change our string */
1520 need_buff = 0;
1521#else
1522 /* the built-in parser will change our string if it sees \n */
1523 need_buff = strchr(cmd, '\n') != NULL;
1524#endif
1525 }
1526 if (need_buff) {
1527 buff = malloc(len + 1);
1528 if (!buff)
1529 return 1;
1530 memcpy(buff, cmd, len);
1531 buff[len] = '\0';
1532 }
1533#ifdef CONFIG_SYS_HUSH_PARSER
1534 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1535#else
1536 /*
1537 * This function will overwrite any \n it sees with a \0, which
1538 * is why it can't work with a const char *. Here we are making
1539 * using of internal knowledge of this function, to avoid always
1540 * doing a malloc() which is actually required only in a case that
1541 * is pretty rare.
1542 */
1543 rcode = builtin_run_command_list(buff, flag);
1544 if (need_buff)
1545 free(buff);
1546#endif
1547
1548 return rcode;
1549}
1550
wdenkc6097192002-11-03 00:24:07 +00001551/****************************************************************************/
1552
Jon Loeligerc3517f92007-07-08 18:10:08 -05001553#if defined(CONFIG_CMD_RUN)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001554int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +00001555{
1556 int i;
wdenkc6097192002-11-03 00:24:07 +00001557
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001558 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001559 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +00001560
1561 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001562 char *arg;
1563
1564 if ((arg = getenv (argv[i])) == NULL) {
1565 printf ("## Error: \"%s\" not defined\n", argv[i]);
1566 return 1;
1567 }
Jason Hobbsc8a20792011-08-31 05:37:24 +00001568
Simon Glass009dde12012-02-14 19:59:20 +00001569 if (run_command(arg, flag) != 0)
wdenk3e386912003-04-05 00:53:31 +00001570 return 1;
wdenkc6097192002-11-03 00:24:07 +00001571 }
wdenk3e386912003-04-05 00:53:31 +00001572 return 0;
wdenkc6097192002-11-03 00:24:07 +00001573}
Jon Loeliger90253172007-07-10 11:02:44 -05001574#endif