]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/main.c
main: Separate out the two abortboot() functions
[thirdparty/u-boot.git] / common / main.c
CommitLineData
c609719b
WD
1/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
501090aa
WD
5 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
c609719b
WD
9 * 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
a6c7ad2f
WD
28/* #define DEBUG */
29
c609719b
WD
30#include <common.h>
31#include <watchdog.h>
32#include <command.h>
224b72e6 33#include <fdtdec.h>
d51004a8 34#include <malloc.h>
09c2e90c 35#include <version.h>
d9631ecf
WD
36#ifdef CONFIG_MODEM_SUPPORT
37#include <malloc.h> /* for free() prototype */
38#endif
c609719b 39
6d0f6bcf 40#ifdef CONFIG_SYS_HUSH_PARSER
c609719b
WD
41#include <hush.h>
42#endif
43
224b72e6
CLC
44#ifdef CONFIG_OF_CONTROL
45#include <fdtdec.h>
46#endif
47
bdccc4fe 48#include <post.h>
4d91a6ec 49#include <linux/ctype.h>
317d6c57 50#include <menu.h>
bdccc4fe 51
d87080b7 52DECLARE_GLOBAL_DATA_PTR;
d87080b7 53
fad63407
HS
54/*
55 * Board-specific Platform code can reimplement show_boot_progress () if needed
56 */
57void inline __show_boot_progress (int val) {}
5e2c08c3 58void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
fad63407 59
c609719b
WD
60#define MAX_DELAY_STOP_STR 32
61
c609719b
WD
62#undef DEBUG_PARSER
63
6475b9f9 64char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
c609719b 65
3ca9122f 66static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
82359aec
MF
67static const char erase_seq[] = "\b \b"; /* erase sequence */
68static const char tab_seq[] = " "; /* used to expand TABs */
c609719b
WD
69
70#ifdef CONFIG_BOOT_RETRY_TIME
71static uint64_t endtime = 0; /* must be set, default is instant timeout */
72static int retry_time = -1; /* -1 so can call readline before main_loop */
73#endif
74
75#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
76
77#ifndef CONFIG_BOOT_RETRY_MIN
78#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
79#endif
80
81#ifdef CONFIG_MODEM_SUPPORT
82int do_mdm_init = 0;
83extern void mdm_init(void); /* defined in board.c */
84#endif
85
86/***************************************************************************
87 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
c8a2079e 88 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
c609719b 89 */
d514544f 90#if defined(CONFIG_BOOTDELAY)
c609719b 91# if defined(CONFIG_AUTOBOOT_KEYED)
063ae006 92static int abortboot_keyed(int bootdelay)
c609719b
WD
93{
94 int abort = 0;
95 uint64_t etime = endtick(bootdelay);
19973b6a 96 struct {
c609719b
WD
97 char* str;
98 u_int len;
99 int retry;
100 }
19973b6a 101 delaykey [] = {
c609719b
WD
102 { str: getenv ("bootdelaykey"), retry: 1 },
103 { str: getenv ("bootdelaykey2"), retry: 1 },
104 { str: getenv ("bootstopkey"), retry: 0 },
105 { str: getenv ("bootstopkey2"), retry: 0 },
106 };
107
108 char presskey [MAX_DELAY_STOP_STR];
109 u_int presskey_len = 0;
110 u_int presskey_max = 0;
111 u_int i;
112
a5aae0a1
DE
113#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
114 if (bootdelay == 0)
115 return 0;
116#endif
117
c609719b 118# ifdef CONFIG_AUTOBOOT_PROMPT
f2302d44 119 printf(CONFIG_AUTOBOOT_PROMPT);
c609719b
WD
120# endif
121
122# ifdef CONFIG_AUTOBOOT_DELAY_STR
123 if (delaykey[0].str == NULL)
124 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
125# endif
126# ifdef CONFIG_AUTOBOOT_DELAY_STR2
127 if (delaykey[1].str == NULL)
128 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
129# endif
130# ifdef CONFIG_AUTOBOOT_STOP_STR
131 if (delaykey[2].str == NULL)
132 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
133# endif
134# ifdef CONFIG_AUTOBOOT_STOP_STR2
135 if (delaykey[3].str == NULL)
136 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
137# endif
138
139 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
140 delaykey[i].len = delaykey[i].str == NULL ?
141 0 : strlen (delaykey[i].str);
142 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
143 MAX_DELAY_STOP_STR : delaykey[i].len;
144
145 presskey_max = presskey_max > delaykey[i].len ?
146 presskey_max : delaykey[i].len;
147
148# if DEBUG_BOOTKEYS
149 printf("%s key:<%s>\n",
150 delaykey[i].retry ? "delay" : "stop",
151 delaykey[i].str ? delaykey[i].str : "NULL");
152# endif
153 }
154
155 /* In order to keep up with incoming data, check timeout only
156 * when catch up.
157 */
c3284b03
PK
158 do {
159 if (tstc()) {
160 if (presskey_len < presskey_max) {
161 presskey [presskey_len ++] = getc();
162 }
163 else {
164 for (i = 0; i < presskey_max - 1; i ++)
165 presskey [i] = presskey [i + 1];
166
167 presskey [i] = getc();
168 }
169 }
170
c609719b
WD
171 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
172 if (delaykey[i].len > 0 &&
173 presskey_len >= delaykey[i].len &&
174 memcmp (presskey + presskey_len - delaykey[i].len,
8bde7f77 175 delaykey[i].str,
c609719b
WD
176 delaykey[i].len) == 0) {
177# if DEBUG_BOOTKEYS
178 printf("got %skey\n",
179 delaykey[i].retry ? "delay" : "stop");
180# endif
181
182# ifdef CONFIG_BOOT_RETRY_TIME
183 /* don't retry auto boot */
184 if (! delaykey[i].retry)
185 retry_time = -1;
186# endif
187 abort = 1;
188 }
189 }
c3284b03 190 } while (!abort && get_ticks() <= etime);
c609719b 191
c609719b
WD
192# if DEBUG_BOOTKEYS
193 if (!abort)
ada4d400 194 puts("key timeout\n");
c609719b
WD
195# endif
196
8cb8143e 197#ifdef CONFIG_SILENT_CONSOLE
4ec5bd55
LM
198 if (abort)
199 gd->flags &= ~GD_FLG_SILENT;
8cb8143e 200#endif
201
c609719b
WD
202 return abort;
203}
204
205# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
206
c7de829c
WD
207#ifdef CONFIG_MENUKEY
208static int menukey = 0;
209#endif
210
063ae006 211static int abortboot_normal(int bootdelay)
c609719b
WD
212{
213 int abort = 0;
b2f3e0ea 214 unsigned long ts;
c609719b 215
c7de829c 216#ifdef CONFIG_MENUPROMPT
f2302d44 217 printf(CONFIG_MENUPROMPT);
c7de829c 218#else
93d7212f
JH
219 if (bootdelay >= 0)
220 printf("Hit any key to stop autoboot: %2d ", bootdelay);
c7de829c 221#endif
c609719b
WD
222
223#if defined CONFIG_ZERO_BOOTDELAY_CHECK
8bde7f77
WD
224 /*
225 * Check if key already pressed
226 * Don't check if bootdelay < 0
227 */
c609719b
WD
228 if (bootdelay >= 0) {
229 if (tstc()) { /* we got a key press */
230 (void) getc(); /* consume input */
4b9206ed 231 puts ("\b\b\b 0");
4ec5bd55 232 abort = 1; /* don't auto boot */
c609719b 233 }
8bde7f77 234 }
c609719b
WD
235#endif
236
f72da340 237 while ((bootdelay > 0) && (!abort)) {
c609719b 238 --bootdelay;
b2f3e0ea
JL
239 /* delay 1000 ms */
240 ts = get_timer(0);
241 do {
c609719b
WD
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
c7de829c
WD
245# ifdef CONFIG_MENUKEY
246 menukey = getc();
247# else
c609719b 248 (void) getc(); /* consume input */
c7de829c 249# endif
c609719b
WD
250 break;
251 }
ada4d400 252 udelay(10000);
b2f3e0ea 253 } while (!abort && get_timer(ts) < 1000);
c609719b 254
ada4d400 255 printf("\b\b\b%2d ", bootdelay);
c609719b
WD
256 }
257
ada4d400 258 putc('\n');
c609719b 259
f72da340 260#ifdef CONFIG_SILENT_CONSOLE
4ec5bd55
LM
261 if (abort)
262 gd->flags &= ~GD_FLG_SILENT;
f72da340
WD
263#endif
264
c609719b
WD
265 return abort;
266}
267# endif /* CONFIG_AUTOBOOT_KEYED */
063ae006
SG
268
269static int abortboot(int bootdelay)
270{
271#ifdef CONFIG_AUTOBOOT_KEYED
272 return abortboot_keyed(bootdelay);
273#else
274 return abortboot_normal(bootdelay);
275#endif
276}
d514544f 277#endif /* CONFIG_BOOTDELAY */
c609719b 278
67e1ea26
DA
279/*
280 * Runs the given boot command securely. Specifically:
281 * - Doesn't run the command with the shell (run_command or parse_string_outer),
282 * since that's a lot of code surface that an attacker might exploit.
283 * Because of this, we don't do any argument parsing--the secure boot command
284 * has to be a full-fledged u-boot command.
285 * - Doesn't check for keypresses before booting, since that could be a
286 * security hole; also disables Ctrl-C.
287 * - Doesn't allow the command to return.
288 *
289 * Upon any failures, this function will drop into an infinite loop after
290 * printing the error message to console.
291 */
292
d514544f 293#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
67e1ea26
DA
294static void secure_boot_cmd(char *cmd)
295{
296 cmd_tbl_t *cmdtp;
297 int rc;
298
299 if (!cmd) {
300 printf("## Error: Secure boot command not specified\n");
301 goto err;
302 }
303
304 /* Disable Ctrl-C just in case some command is used that checks it. */
305 disable_ctrlc(1);
306
307 /* Find the command directly. */
308 cmdtp = find_cmd(cmd);
309 if (!cmdtp) {
310 printf("## Error: \"%s\" not defined\n", cmd);
311 goto err;
312 }
313
314 /* Run the command, forcing no flags and faking argc and argv. */
315 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
316
317 /* Shouldn't ever return from boot command. */
318 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
319
320err:
321 /*
322 * Not a whole lot to do here. Rebooting won't help much, since we'll
323 * just end up right back here. Just loop.
324 */
325 hang();
326}
327
fcabc24f
SG
328static void process_fdt_options(const void *blob)
329{
330 ulong addr;
331
332 /* Add an env variable to point to a kernel payload, if available */
333 addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
334 if (addr)
335 setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
336
337 /* Add an env variable to point to a root disk, if available */
338 addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
339 if (addr)
340 setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
341}
67e1ea26
DA
342#endif /* CONFIG_OF_CONTROL */
343
344
c609719b
WD
345/****************************************************************************/
346
347void main_loop (void)
348{
6d0f6bcf
JCPV
349#ifndef CONFIG_SYS_HUSH_PARSER
350 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
c609719b
WD
351 int len;
352 int rc = 1;
353 int flag;
354#endif
d514544f 355#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
224b72e6
CLC
356 char *env;
357#endif
d514544f 358#if defined(CONFIG_BOOTDELAY)
c609719b
WD
359 char *s;
360 int bootdelay;
361#endif
362#ifdef CONFIG_PREBOOT
363 char *p;
364#endif
bdccc4fe
WD
365#ifdef CONFIG_BOOTCOUNT_LIMIT
366 unsigned long bootcount = 0;
367 unsigned long bootlimit = 0;
368 char *bcs;
369 char bcs_set[16];
370#endif /* CONFIG_BOOTCOUNT_LIMIT */
c609719b 371
01433d60
SG
372 bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
373
bdccc4fe
WD
374#ifdef CONFIG_BOOTCOUNT_LIMIT
375 bootcount = bootcount_load();
376 bootcount++;
377 bootcount_store (bootcount);
378 sprintf (bcs_set, "%lu", bootcount);
379 setenv ("bootcount", bcs_set);
380 bcs = getenv ("bootlimit");
381 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
382#endif /* CONFIG_BOOTCOUNT_LIMIT */
383
c609719b
WD
384#ifdef CONFIG_MODEM_SUPPORT
385 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
386 if (do_mdm_init) {
77ddac94 387 char *str = strdup(getenv("mdm_cmd"));
c609719b
WD
388 setenv ("preboot", str); /* set or delete definition */
389 if (str != NULL)
390 free (str);
391 mdm_init(); /* wait for modem connection */
392 }
393#endif /* CONFIG_MODEM_SUPPORT */
394
0587597c
SR
395#ifdef CONFIG_VERSION_VARIABLE
396 {
155cb010 397 setenv ("ver", version_string); /* set version variable */
0587597c
SR
398 }
399#endif /* CONFIG_VERSION_VARIABLE */
400
6d0f6bcf 401#ifdef CONFIG_SYS_HUSH_PARSER
c609719b
WD
402 u_boot_hush_start ();
403#endif
404
81473f67
HS
405#if defined(CONFIG_HUSH_INIT_VAR)
406 hush_init_var ();
407#endif
408
c609719b
WD
409#ifdef CONFIG_PREBOOT
410 if ((p = getenv ("preboot")) != NULL) {
411# ifdef CONFIG_AUTOBOOT_KEYED
412 int prev = disable_ctrlc(1); /* disable Control C checking */
413# endif
414
3a8a02be 415 run_command_list(p, -1, 0);
c609719b
WD
416
417# ifdef CONFIG_AUTOBOOT_KEYED
418 disable_ctrlc(prev); /* restore Control C checking */
419# endif
420 }
421#endif /* CONFIG_PREBOOT */
422
143cd21f 423#if defined(CONFIG_UPDATE_TFTP)
8d6b7320 424 update_tftp (0UL);
143cd21f
WD
425#endif /* CONFIG_UPDATE_TFTP */
426
d514544f 427#if defined(CONFIG_BOOTDELAY)
c609719b
WD
428 s = getenv ("bootdelay");
429 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
430
a6c7ad2f 431 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
c609719b 432
317d6c57
HS
433#if defined(CONFIG_MENU_SHOW)
434 bootdelay = menu_show(bootdelay);
435#endif
c609719b 436# ifdef CONFIG_BOOT_RETRY_TIME
6dd652fa 437 init_cmd_timeout ();
c609719b
WD
438# endif /* CONFIG_BOOT_RETRY_TIME */
439
b428f6a8
YT
440#ifdef CONFIG_POST
441 if (gd->flags & GD_FLG_POSTFAIL) {
442 s = getenv("failbootcmd");
443 }
444 else
445#endif /* CONFIG_POST */
bdccc4fe
WD
446#ifdef CONFIG_BOOTCOUNT_LIMIT
447 if (bootlimit && (bootcount > bootlimit)) {
448 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
449 (unsigned)bootlimit);
450 s = getenv ("altbootcmd");
451 }
452 else
453#endif /* CONFIG_BOOTCOUNT_LIMIT */
454 s = getenv ("bootcmd");
224b72e6
CLC
455#ifdef CONFIG_OF_CONTROL
456 /* Allow the fdt to override the boot command */
457 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
458 if (env)
459 s = env;
67e1ea26 460
fcabc24f
SG
461 process_fdt_options(gd->fdt_blob);
462
67e1ea26
DA
463 /*
464 * If the bootsecure option was chosen, use secure_boot_cmd().
465 * Always use 'env' in this case, since bootsecure requres that the
466 * bootcmd was specified in the FDT too.
467 */
468 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
469 secure_boot_cmd(env);
470
224b72e6 471#endif /* CONFIG_OF_CONTROL */
a6c7ad2f
WD
472
473 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
474
93d7212f 475 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
c609719b
WD
476# ifdef CONFIG_AUTOBOOT_KEYED
477 int prev = disable_ctrlc(1); /* disable Control C checking */
478# endif
479
3a8a02be 480 run_command_list(s, -1, 0);
c609719b
WD
481
482# ifdef CONFIG_AUTOBOOT_KEYED
483 disable_ctrlc(prev); /* restore Control C checking */
484# endif
485 }
c7de829c
WD
486
487# ifdef CONFIG_MENUKEY
a6c7ad2f 488 if (menukey == CONFIG_MENUKEY) {
370d1e3e 489 s = getenv("menucmd");
c8a2079e 490 if (s)
3a8a02be 491 run_command_list(s, -1, 0);
c7de829c
WD
492 }
493#endif /* CONFIG_MENUKEY */
953b7e62 494#endif /* CONFIG_BOOTDELAY */
c7de829c 495
c609719b
WD
496 /*
497 * Main Loop for Monitor Command Processing
498 */
6d0f6bcf 499#ifdef CONFIG_SYS_HUSH_PARSER
c609719b
WD
500 parse_file_outer();
501 /* This point is never reached */
502 for (;;);
503#else
504 for (;;) {
505#ifdef CONFIG_BOOT_RETRY_TIME
506 if (rc >= 0) {
507 /* Saw enough of a valid command to
508 * restart the timeout.
509 */
510 reset_cmd_timeout();
511 }
512#endif
6d0f6bcf 513 len = readline (CONFIG_SYS_PROMPT);
c609719b
WD
514
515 flag = 0; /* assume no special flags for now */
516 if (len > 0)
517 strcpy (lastcommand, console_buffer);
518 else if (len == 0)
519 flag |= CMD_FLAG_REPEAT;
520#ifdef CONFIG_BOOT_RETRY_TIME
521 else if (len == -2) {
522 /* -2 means timed out, retry autoboot
523 */
4b9206ed 524 puts ("\nTimed out waiting for command\n");
c609719b
WD
525# ifdef CONFIG_RESET_TO_RETRY
526 /* Reinit board to run initialization code again */
527 do_reset (NULL, 0, 0, NULL);
528# else
529 return; /* retry autoboot */
530# endif
531 }
532#endif
533
534 if (len == -1)
4b9206ed 535 puts ("<INTERRUPT>\n");
c609719b 536 else
53071532 537 rc = run_command(lastcommand, flag);
c609719b
WD
538
539 if (rc <= 0) {
540 /* invalid command or not repeatable, forget it */
541 lastcommand[0] = 0;
542 }
543 }
6d0f6bcf 544#endif /*CONFIG_SYS_HUSH_PARSER*/
c609719b
WD
545}
546
6dd652fa
WD
547#ifdef CONFIG_BOOT_RETRY_TIME
548/***************************************************************************
19973b6a 549 * initialize command line timeout
6dd652fa
WD
550 */
551void init_cmd_timeout(void)
552{
553 char *s = getenv ("bootretry");
554
555 if (s != NULL)
b028f715 556 retry_time = (int)simple_strtol(s, NULL, 10);
6dd652fa
WD
557 else
558 retry_time = CONFIG_BOOT_RETRY_TIME;
559
560 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
561 retry_time = CONFIG_BOOT_RETRY_MIN;
562}
563
c609719b
WD
564/***************************************************************************
565 * reset command line timeout to retry_time seconds
566 */
c609719b
WD
567void reset_cmd_timeout(void)
568{
569 endtime = endtick(retry_time);
570}
571#endif
572
501090aa
WD
573#ifdef CONFIG_CMDLINE_EDITING
574
575/*
576 * cmdline-editing related codes from vivi.
577 * Author: Janghoon Lyu <nandy@mizi.com>
578 */
579
501090aa 580#define putnstr(str,n) do { \
dc4b0b38 581 printf ("%.*s", (int)n, str); \
501090aa 582 } while (0)
501090aa
WD
583
584#define CTL_CH(c) ((c) - 'a' + 1)
501090aa
WD
585#define CTL_BACKSPACE ('\b')
586#define DEL ((char)255)
587#define DEL7 ((char)127)
588#define CREAD_HIST_CHAR ('!')
589
590#define getcmd_putch(ch) putc(ch)
591#define getcmd_getch() getc()
592#define getcmd_cbeep() getcmd_putch('\a')
593
594#define HIST_MAX 20
8804ae3b 595#define HIST_SIZE CONFIG_SYS_CBSIZE
501090aa 596
199adb60
KP
597static int hist_max;
598static int hist_add_idx;
501090aa 599static int hist_cur = -1;
199adb60 600static unsigned hist_num;
501090aa 601
199adb60
KP
602static char *hist_list[HIST_MAX];
603static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
501090aa
WD
604
605#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
606
607static void hist_init(void)
608{
609 int i;
610
611 hist_max = 0;
612 hist_add_idx = 0;
613 hist_cur = -1;
614 hist_num = 0;
615
616 for (i = 0; i < HIST_MAX; i++) {
617 hist_list[i] = hist_lines[i];
618 hist_list[i][0] = '\0';
619 }
620}
621
622static void cread_add_to_hist(char *line)
623{
624 strcpy(hist_list[hist_add_idx], line);
625
626 if (++hist_add_idx >= HIST_MAX)
627 hist_add_idx = 0;
628
629 if (hist_add_idx > hist_max)
630 hist_max = hist_add_idx;
631
632 hist_num++;
633}
634
635static char* hist_prev(void)
636{
637 char *ret;
638 int old_cur;
639
640 if (hist_cur < 0)
641 return NULL;
642
643 old_cur = hist_cur;
644 if (--hist_cur < 0)
645 hist_cur = hist_max;
646
647 if (hist_cur == hist_add_idx) {
648 hist_cur = old_cur;
649 ret = NULL;
650 } else
651 ret = hist_list[hist_cur];
652
653 return (ret);
654}
655
656static char* hist_next(void)
657{
658 char *ret;
659
660 if (hist_cur < 0)
661 return NULL;
662
663 if (hist_cur == hist_add_idx)
664 return NULL;
665
666 if (++hist_cur > hist_max)
667 hist_cur = 0;
668
669 if (hist_cur == hist_add_idx) {
670 ret = "";
671 } else
672 ret = hist_list[hist_cur];
673
674 return (ret);
675}
676
3ca9122f 677#ifndef CONFIG_CMDLINE_EDITING
501090aa
WD
678static void cread_print_hist_list(void)
679{
680 int i;
681 unsigned long n;
682
683 n = hist_num - hist_max;
684
685 i = hist_add_idx + 1;
686 while (1) {
687 if (i > hist_max)
688 i = 0;
689 if (i == hist_add_idx)
690 break;
691 printf("%s\n", hist_list[i]);
692 n++;
693 i++;
694 }
695}
3ca9122f 696#endif /* CONFIG_CMDLINE_EDITING */
501090aa
WD
697
698#define BEGINNING_OF_LINE() { \
699 while (num) { \
700 getcmd_putch(CTL_BACKSPACE); \
701 num--; \
702 } \
703}
704
705#define ERASE_TO_EOL() { \
706 if (num < eol_num) { \
8faba489
MF
707 printf("%*s", (int)(eol_num - num), ""); \
708 do { \
501090aa 709 getcmd_putch(CTL_BACKSPACE); \
8faba489 710 } while (--eol_num > num); \
501090aa
WD
711 } \
712}
713
714#define REFRESH_TO_EOL() { \
715 if (num < eol_num) { \
716 wlen = eol_num - num; \
717 putnstr(buf + num, wlen); \
718 num = eol_num; \
719 } \
720}
721
722static void cread_add_char(char ichar, int insert, unsigned long *num,
723 unsigned long *eol_num, char *buf, unsigned long len)
724{
725 unsigned long wlen;
726
727 /* room ??? */
728 if (insert || *num == *eol_num) {
729 if (*eol_num > len - 1) {
730 getcmd_cbeep();
731 return;
732 }
733 (*eol_num)++;
734 }
735
736 if (insert) {
737 wlen = *eol_num - *num;
738 if (wlen > 1) {
739 memmove(&buf[*num+1], &buf[*num], wlen-1);
740 }
741
742 buf[*num] = ichar;
743 putnstr(buf + *num, wlen);
744 (*num)++;
745 while (--wlen) {
746 getcmd_putch(CTL_BACKSPACE);
747 }
748 } else {
749 /* echo the character */
750 wlen = 1;
751 buf[*num] = ichar;
752 putnstr(buf + *num, wlen);
753 (*num)++;
754 }
755}
756
757static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
758 unsigned long *eol_num, char *buf, unsigned long len)
759{
760 while (strsize--) {
761 cread_add_char(*str, insert, num, eol_num, buf, len);
762 str++;
763 }
764}
765
9c348311
HS
766static int cread_line(const char *const prompt, char *buf, unsigned int *len,
767 int timeout)
501090aa
WD
768{
769 unsigned long num = 0;
770 unsigned long eol_num = 0;
501090aa
WD
771 unsigned long wlen;
772 char ichar;
773 int insert = 1;
774 int esc_len = 0;
501090aa 775 char esc_save[8];
ecc5500e 776 int init_len = strlen(buf);
9c348311 777 int first = 1;
ecc5500e
PT
778
779 if (init_len)
780 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
501090aa
WD
781
782 while (1) {
00ac50e3
AE
783#ifdef CONFIG_BOOT_RETRY_TIME
784 while (!tstc()) { /* while no incoming data */
785 if (retry_time >= 0 && get_ticks() > endtime)
786 return (-2); /* timed out */
30dc165a 787 WATCHDOG_RESET();
00ac50e3
AE
788 }
789#endif
9c348311
HS
790 if (first && timeout) {
791 uint64_t etime = endtick(timeout);
792
793 while (!tstc()) { /* while no incoming data */
794 if (get_ticks() >= etime)
795 return -2; /* timed out */
796 WATCHDOG_RESET();
797 }
798 first = 0;
799 }
00ac50e3 800
501090aa
WD
801 ichar = getcmd_getch();
802
803 if ((ichar == '\n') || (ichar == '\r')) {
dd9f06f0 804 putc('\n');
501090aa
WD
805 break;
806 }
807
808 /*
809 * handle standard linux xterm esc sequences for arrow key, etc.
810 */
811 if (esc_len != 0) {
812 if (esc_len == 1) {
813 if (ichar == '[') {
814 esc_save[esc_len] = ichar;
815 esc_len = 2;
816 } else {
817 cread_add_str(esc_save, esc_len, insert,
818 &num, &eol_num, buf, *len);
819 esc_len = 0;
820 }
821 continue;
822 }
823
824 switch (ichar) {
825
826 case 'D': /* <- key */
827 ichar = CTL_CH('b');
828 esc_len = 0;
829 break;
830 case 'C': /* -> key */
831 ichar = CTL_CH('f');
832 esc_len = 0;
833 break; /* pass off to ^F handler */
834 case 'H': /* Home key */
835 ichar = CTL_CH('a');
836 esc_len = 0;
837 break; /* pass off to ^A handler */
838 case 'A': /* up arrow */
839 ichar = CTL_CH('p');
840 esc_len = 0;
841 break; /* pass off to ^P handler */
842 case 'B': /* down arrow */
843 ichar = CTL_CH('n');
844 esc_len = 0;
845 break; /* pass off to ^N handler */
846 default:
847 esc_save[esc_len++] = ichar;
848 cread_add_str(esc_save, esc_len, insert,
849 &num, &eol_num, buf, *len);
850 esc_len = 0;
851 continue;
852 }
853 }
854
855 switch (ichar) {
856 case 0x1b:
857 if (esc_len == 0) {
858 esc_save[esc_len] = ichar;
859 esc_len = 1;
860 } else {
dd9f06f0 861 puts("impossible condition #876\n");
501090aa
WD
862 esc_len = 0;
863 }
864 break;
865
866 case CTL_CH('a'):
867 BEGINNING_OF_LINE();
868 break;
869 case CTL_CH('c'): /* ^C - break */
870 *buf = '\0'; /* discard input */
871 return (-1);
872 case CTL_CH('f'):
873 if (num < eol_num) {
874 getcmd_putch(buf[num]);
875 num++;
876 }
877 break;
878 case CTL_CH('b'):
879 if (num) {
880 getcmd_putch(CTL_BACKSPACE);
881 num--;
882 }
883 break;
884 case CTL_CH('d'):
885 if (num < eol_num) {
886 wlen = eol_num - num - 1;
887 if (wlen) {
888 memmove(&buf[num], &buf[num+1], wlen);
889 putnstr(buf + num, wlen);
890 }
891
892 getcmd_putch(' ');
893 do {
894 getcmd_putch(CTL_BACKSPACE);
895 } while (wlen--);
896 eol_num--;
897 }
898 break;
899 case CTL_CH('k'):
900 ERASE_TO_EOL();
901 break;
902 case CTL_CH('e'):
903 REFRESH_TO_EOL();
904 break;
905 case CTL_CH('o'):
906 insert = !insert;
907 break;
908 case CTL_CH('x'):
23d0baf9 909 case CTL_CH('u'):
501090aa
WD
910 BEGINNING_OF_LINE();
911 ERASE_TO_EOL();
912 break;
913 case DEL:
914 case DEL7:
915 case 8:
916 if (num) {
917 wlen = eol_num - num;
918 num--;
919 memmove(&buf[num], &buf[num+1], wlen);
920 getcmd_putch(CTL_BACKSPACE);
921 putnstr(buf + num, wlen);
922 getcmd_putch(' ');
923 do {
924 getcmd_putch(CTL_BACKSPACE);
925 } while (wlen--);
926 eol_num--;
927 }
928 break;
929 case CTL_CH('p'):
930 case CTL_CH('n'):
931 {
932 char * hline;
933
934 esc_len = 0;
935
936 if (ichar == CTL_CH('p'))
937 hline = hist_prev();
938 else
939 hline = hist_next();
940
941 if (!hline) {
942 getcmd_cbeep();
943 continue;
944 }
945
946 /* nuke the current line */
947 /* first, go home */
948 BEGINNING_OF_LINE();
949
950 /* erase to end of line */
951 ERASE_TO_EOL();
952
953 /* copy new line into place and display */
954 strcpy(buf, hline);
955 eol_num = strlen(buf);
956 REFRESH_TO_EOL();
957 continue;
958 }
23d0baf9
JCPV
959#ifdef CONFIG_AUTO_COMPLETE
960 case '\t': {
961 int num2, col;
962
963 /* do not autocomplete when in the middle */
964 if (num < eol_num) {
965 getcmd_cbeep();
966 break;
967 }
968
969 buf[num] = '\0';
970 col = strlen(prompt) + eol_num;
971 num2 = num;
972 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
973 col = num2 - num;
974 num += col;
975 eol_num += col;
976 }
977 break;
978 }
979#endif
501090aa
WD
980 default:
981 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
982 break;
983 }
984 }
985 *len = eol_num;
986 buf[eol_num] = '\0'; /* lose the newline */
987
988 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
989 cread_add_to_hist(buf);
990 hist_cur = hist_add_idx;
991
f9239438 992 return 0;
501090aa
WD
993}
994
995#endif /* CONFIG_CMDLINE_EDITING */
996
c609719b
WD
997/****************************************************************************/
998
999/*
1000 * Prompt for input and read a line.
1001 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
1002 * time out when time goes past endtime (timebase time in ticks).
1003 * Return: number of read characters
1004 * -1 if break
1005 * -2 if timed out
1006 */
1007int readline (const char *const prompt)
1008{
ecc5500e
PT
1009 /*
1010 * If console_buffer isn't 0-length the user will be prompted to modify
1011 * it instead of entering it from scratch as desired.
1012 */
1013 console_buffer[0] = '\0';
1014
9c348311 1015 return readline_into_buffer(prompt, console_buffer, 0);
6636b62a
JY
1016}
1017
1018
9c348311 1019int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
6636b62a
JY
1020{
1021 char *p = buffer;
501090aa 1022#ifdef CONFIG_CMDLINE_EDITING
8804ae3b 1023 unsigned int len = CONFIG_SYS_CBSIZE;
d8f961bb 1024 int rc;
501090aa
WD
1025 static int initted = 0;
1026
597f6c26
JY
1027 /*
1028 * History uses a global array which is not
1029 * writable until after relocation to RAM.
1030 * Revert to non-history version if still
1031 * running from flash.
1032 */
1033 if (gd->flags & GD_FLG_RELOC) {
1034 if (!initted) {
1035 hist_init();
1036 initted = 1;
1037 }
1038
e491a71e
PT
1039 if (prompt)
1040 puts (prompt);
597f6c26 1041
9c348311 1042 rc = cread_line(prompt, p, &len, timeout);
597f6c26
JY
1043 return rc < 0 ? rc : len;
1044
1045 } else {
1046#endif /* CONFIG_CMDLINE_EDITING */
0ec59524 1047 char * p_buf = p;
c609719b
WD
1048 int n = 0; /* buffer index */
1049 int plen = 0; /* prompt length */
1050 int col; /* output column cnt */
1051 char c;
1052
1053 /* print prompt */
1054 if (prompt) {
1055 plen = strlen (prompt);
1056 puts (prompt);
1057 }
1058 col = plen;
1059
1060 for (;;) {
1061#ifdef CONFIG_BOOT_RETRY_TIME
1062 while (!tstc()) { /* while no incoming data */
1063 if (retry_time >= 0 && get_ticks() > endtime)
1064 return (-2); /* timed out */
30dc165a 1065 WATCHDOG_RESET();
c609719b
WD
1066 }
1067#endif
1068 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1069
1070#ifdef CONFIG_SHOW_ACTIVITY
1071 while (!tstc()) {
c609719b 1072 show_activity(0);
30dc165a 1073 WATCHDOG_RESET();
c609719b
WD
1074 }
1075#endif
1076 c = getc();
1077
1078 /*
1079 * Special character handling
1080 */
1081 switch (c) {
1082 case '\r': /* Enter */
1083 case '\n':
1084 *p = '\0';
1085 puts ("\r\n");
6636b62a 1086 return (p - p_buf);
c609719b 1087
27aa8186
WD
1088 case '\0': /* nul */
1089 continue;
1090
c609719b 1091 case 0x03: /* ^C - break */
6636b62a 1092 p_buf[0] = '\0'; /* discard input */
c609719b
WD
1093 return (-1);
1094
1095 case 0x15: /* ^U - erase line */
1096 while (col > plen) {
1097 puts (erase_seq);
1098 --col;
1099 }
6636b62a 1100 p = p_buf;
c609719b
WD
1101 n = 0;
1102 continue;
1103
1636d1c8 1104 case 0x17: /* ^W - erase word */
6636b62a 1105 p=delete_char(p_buf, p, &col, &n, plen);
c609719b 1106 while ((n > 0) && (*p != ' ')) {
6636b62a 1107 p=delete_char(p_buf, p, &col, &n, plen);
c609719b
WD
1108 }
1109 continue;
1110
1111 case 0x08: /* ^H - backspace */
1112 case 0x7F: /* DEL - backspace */
6636b62a 1113 p=delete_char(p_buf, p, &col, &n, plen);
c609719b
WD
1114 continue;
1115
1116 default:
1117 /*
1118 * Must be a normal character then
1119 */
6d0f6bcf 1120 if (n < CONFIG_SYS_CBSIZE-2) {
c609719b 1121 if (c == '\t') { /* expand TABs */
04a85b3b
WD
1122#ifdef CONFIG_AUTO_COMPLETE
1123 /* if auto completion triggered just continue */
1124 *p = '\0';
1125 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
6636b62a 1126 p = p_buf + n; /* reset */
04a85b3b
WD
1127 continue;
1128 }
1129#endif
c609719b
WD
1130 puts (tab_seq+(col&07));
1131 col += 8 - (col&07);
1132 } else {
9a8efc46
SG
1133 char buf[2];
1134
1135 /*
1136 * Echo input using puts() to force am
1137 * LCD flush if we are using an LCD
1138 */
1139 ++col;
1140 buf[0] = c;
1141 buf[1] = '\0';
1142 puts(buf);
c609719b
WD
1143 }
1144 *p++ = c;
1145 ++n;
1146 } else { /* Buffer full */
1147 putc ('\a');
1148 }
1149 }
1150 }
597f6c26
JY
1151#ifdef CONFIG_CMDLINE_EDITING
1152 }
1153#endif
c609719b
WD
1154}
1155
1156/****************************************************************************/
1157
1158static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1159{
1160 char *s;
1161
1162 if (*np == 0) {
1163 return (p);
1164 }
1165
1166 if (*(--p) == '\t') { /* will retype the whole line */
1167 while (*colp > plen) {
1168 puts (erase_seq);
1169 (*colp)--;
1170 }
1171 for (s=buffer; s<p; ++s) {
1172 if (*s == '\t') {
1173 puts (tab_seq+((*colp) & 07));
1174 *colp += 8 - ((*colp) & 07);
1175 } else {
1176 ++(*colp);
1177 putc (*s);
1178 }
1179 }
1180 } else {
1181 puts (erase_seq);
1182 (*colp)--;
1183 }
1184 (*np)--;
1185 return (p);
1186}
1187
1188/****************************************************************************/
1189
1190int parse_line (char *line, char *argv[])
1191{
1192 int nargs = 0;
1193
1194#ifdef DEBUG_PARSER
1195 printf ("parse_line: \"%s\"\n", line);
1196#endif
6d0f6bcf 1197 while (nargs < CONFIG_SYS_MAXARGS) {
c609719b
WD
1198
1199 /* skip any white space */
4d91a6ec 1200 while (isblank(*line))
c609719b 1201 ++line;
c609719b
WD
1202
1203 if (*line == '\0') { /* end of line, no more args */
1204 argv[nargs] = NULL;
1205#ifdef DEBUG_PARSER
1206 printf ("parse_line: nargs=%d\n", nargs);
1207#endif
1208 return (nargs);
1209 }
1210
1211 argv[nargs++] = line; /* begin of argument string */
1212
1213 /* find end of string */
4d91a6ec 1214 while (*line && !isblank(*line))
c609719b 1215 ++line;
c609719b
WD
1216
1217 if (*line == '\0') { /* end of line, no more args */
1218 argv[nargs] = NULL;
1219#ifdef DEBUG_PARSER
1220 printf ("parse_line: nargs=%d\n", nargs);
1221#endif
1222 return (nargs);
1223 }
1224
1225 *line++ = '\0'; /* terminate current arg */
1226 }
1227
6d0f6bcf 1228 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
c609719b
WD
1229
1230#ifdef DEBUG_PARSER
1231 printf ("parse_line: nargs=%d\n", nargs);
1232#endif
1233 return (nargs);
1234}
1235
1236/****************************************************************************/
1237
7fed89e0 1238#ifndef CONFIG_SYS_HUSH_PARSER
c609719b
WD
1239static void process_macros (const char *input, char *output)
1240{
1241 char c, prev;
1242 const char *varname_start = NULL;
19973b6a 1243 int inputcnt = strlen (input);
6d0f6bcf 1244 int outputcnt = CONFIG_SYS_CBSIZE;
19973b6a
WD
1245 int state = 0; /* 0 = waiting for '$' */
1246
1247 /* 1 = waiting for '(' or '{' */
1248 /* 2 = waiting for ')' or '}' */
1249 /* 3 = waiting for ''' */
c609719b
WD
1250#ifdef DEBUG_PARSER
1251 char *output_start = output;
1252
19973b6a
WD
1253 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1254 input);
c609719b
WD
1255#endif
1256
19973b6a 1257 prev = '\0'; /* previous character */
c609719b
WD
1258
1259 while (inputcnt && outputcnt) {
8bde7f77 1260 c = *input++;
19973b6a
WD
1261 inputcnt--;
1262
1263 if (state != 3) {
1264 /* remove one level of escape characters */
1265 if ((c == '\\') && (prev != '\\')) {
1266 if (inputcnt-- == 0)
1267 break;
1268 prev = c;
1269 c = *input++;
1270 }
c609719b 1271 }
19973b6a
WD
1272
1273 switch (state) {
1274 case 0: /* Waiting for (unescaped) $ */
1275 if ((c == '\'') && (prev != '\\')) {
1276 state = 3;
1277 break;
1278 }
1279 if ((c == '$') && (prev != '\\')) {
1280 state++;
1281 } else {
c609719b
WD
1282 *(output++) = c;
1283 outputcnt--;
1284 }
19973b6a
WD
1285 break;
1286 case 1: /* Waiting for ( */
1287 if (c == '(' || c == '{') {
1288 state++;
1289 varname_start = input;
1290 } else {
1291 state = 0;
1292 *(output++) = '$';
1293 outputcnt--;
c609719b 1294
19973b6a
WD
1295 if (outputcnt) {
1296 *(output++) = c;
c609719b
WD
1297 outputcnt--;
1298 }
19973b6a
WD
1299 }
1300 break;
1301 case 2: /* Waiting for ) */
1302 if (c == ')' || c == '}') {
1303 int i;
6d0f6bcf 1304 char envname[CONFIG_SYS_CBSIZE], *envval;
19973b6a
WD
1305 int envcnt = input - varname_start - 1; /* Varname # of chars */
1306
1307 /* Get the varname */
1308 for (i = 0; i < envcnt; i++) {
1309 envname[i] = varname_start[i];
1310 }
1311 envname[i] = 0;
1312
1313 /* Get its value */
1314 envval = getenv (envname);
1315
1316 /* Copy into the line if it exists */
1317 if (envval != NULL)
1318 while ((*envval) && outputcnt) {
1319 *(output++) = *(envval++);
1320 outputcnt--;
1321 }
1322 /* Look for another '$' */
1323 state = 0;
1324 }
1325 break;
1326 case 3: /* Waiting for ' */
1327 if ((c == '\'') && (prev != '\\')) {
1328 state = 0;
1329 } else {
1330 *(output++) = c;
1331 outputcnt--;
1332 }
1333 break;
a25f862b 1334 }
19973b6a 1335 prev = c;
c609719b
WD
1336 }
1337
1338 if (outputcnt)
1339 *output = 0;
9160b96f
BS
1340 else
1341 *(output - 1) = 0;
c609719b
WD
1342
1343#ifdef DEBUG_PARSER
1344 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
19973b6a 1345 strlen (output_start), output_start);
c609719b
WD
1346#endif
1347}
1348
1349/****************************************************************************
1350 * returns:
1351 * 1 - command executed, repeatable
1352 * 0 - command executed but not repeatable, interrupted commands are
1353 * always considered not repeatable
1354 * -1 - not executed (unrecognized, bootd recursion or too many args)
6d0f6bcf 1355 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
c609719b
WD
1356 * considered unrecognized)
1357 *
1358 * WARNING:
1359 *
1360 * We must create a temporary copy of the command since the command we get
1361 * may be the result from getenv(), which returns a pointer directly to
1362 * the environment data, which may change magicly when the command we run
1363 * creates or modifies environment variables (like "bootp" does).
1364 */
53071532 1365static int builtin_run_command(const char *cmd, int flag)
c609719b 1366{
6d0f6bcf 1367 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
c609719b
WD
1368 char *token; /* start of token in cmdbuf */
1369 char *sep; /* end of token (separator) in cmdbuf */
6d0f6bcf 1370 char finaltoken[CONFIG_SYS_CBSIZE];
c609719b 1371 char *str = cmdbuf;
6d0f6bcf 1372 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
f07771cc 1373 int argc, inquotes;
c609719b 1374 int repeatable = 1;
f07771cc 1375 int rc = 0;
c609719b
WD
1376
1377#ifdef DEBUG_PARSER
1378 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1379 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1380 puts ("\"\n");
1381#endif
1382
1383 clear_ctrlc(); /* forget any previous Control C */
1384
1385 if (!cmd || !*cmd) {
1386 return -1; /* empty command */
1387 }
1388
6d0f6bcf 1389 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
c609719b
WD
1390 puts ("## Command too long!\n");
1391 return -1;
1392 }
1393
1394 strcpy (cmdbuf, cmd);
1395
1396 /* Process separators and check for invalid
1397 * repeatable commands
1398 */
1399
1400#ifdef DEBUG_PARSER
1401 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1402#endif
1403 while (*str) {
1404
1405 /*
1406 * Find separator, or string end
1407 * Allow simple escape of ';' by writing "\;"
1408 */
a25f862b
WD
1409 for (inquotes = 0, sep = str; *sep; sep++) {
1410 if ((*sep=='\'') &&
1411 (*(sep-1) != '\\'))
1412 inquotes=!inquotes;
1413
1414 if (!inquotes &&
1415 (*sep == ';') && /* separator */
c609719b
WD
1416 ( sep != str) && /* past string start */
1417 (*(sep-1) != '\\')) /* and NOT escaped */
1418 break;
1419 }
1420
1421 /*
1422 * Limit the token to data between separators
1423 */
1424 token = str;
1425 if (*sep) {
1426 str = sep + 1; /* start of command for next pass */
1427 *sep = '\0';
1428 }
1429 else
1430 str = sep; /* no more commands for next pass */
1431#ifdef DEBUG_PARSER
1432 printf ("token: \"%s\"\n", token);
1433#endif
1434
1435 /* find macros in this token and replace them */
1436 process_macros (token, finaltoken);
1437
1438 /* Extract arguments */
1264b405
WD
1439 if ((argc = parse_line (finaltoken, argv)) == 0) {
1440 rc = -1; /* no command at all */
1441 continue;
1442 }
c609719b 1443
34765e88 1444 if (cmd_process(flag, argc, argv, &repeatable, NULL))
030fca52 1445 rc = -1;
c609719b
WD
1446
1447 /* Did the user stop this? */
1448 if (had_ctrlc ())
5afb2020 1449 return -1; /* if stopped then not repeatable */
c609719b
WD
1450 }
1451
f07771cc 1452 return rc ? rc : repeatable;
c609719b 1453}
7fed89e0 1454#endif
c609719b 1455
53071532
SG
1456/*
1457 * Run a command using the selected parser.
1458 *
1459 * @param cmd Command to run
1460 * @param flag Execution flags (CMD_FLAG_...)
1461 * @return 0 on success, or != 0 on error.
1462 */
1463int run_command(const char *cmd, int flag)
1464{
1465#ifndef CONFIG_SYS_HUSH_PARSER
1466 /*
1467 * builtin_run_command can return 0 or 1 for success, so clean up
1468 * its result.
1469 */
1470 if (builtin_run_command(cmd, flag) == -1)
1471 return 1;
1472
1473 return 0;
1474#else
1475 return parse_string_outer(cmd,
1476 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1477#endif
1478}
1479
d51004a8
SG
1480#ifndef CONFIG_SYS_HUSH_PARSER
1481/**
1482 * Execute a list of command separated by ; or \n using the built-in parser.
1483 *
1484 * This function cannot take a const char * for the command, since if it
1485 * finds newlines in the string, it replaces them with \0.
1486 *
1487 * @param cmd String containing list of commands
1488 * @param flag Execution flags (CMD_FLAG_...)
1489 * @return 0 on success, or != 0 on error.
1490 */
1491static int builtin_run_command_list(char *cmd, int flag)
1492{
1493 char *line, *next;
1494 int rcode = 0;
1495
1496 /*
1497 * Break into individual lines, and execute each line; terminate on
1498 * error.
1499 */
1500 line = next = cmd;
1501 while (*next) {
1502 if (*next == '\n') {
1503 *next = '\0';
1504 /* run only non-empty commands */
1505 if (*line) {
1506 debug("** exec: \"%s\"\n", line);
1507 if (builtin_run_command(line, 0) < 0) {
1508 rcode = 1;
1509 break;
1510 }
1511 }
1512 line = next + 1;
1513 }
1514 ++next;
1515 }
1516 if (rcode == 0 && *line)
1517 rcode = (builtin_run_command(line, 0) >= 0);
1518
1519 return rcode;
1520}
1521#endif
1522
1523int run_command_list(const char *cmd, int len, int flag)
1524{
1525 int need_buff = 1;
1526 char *buff = (char *)cmd; /* cast away const */
1527 int rcode = 0;
1528
1529 if (len == -1) {
1530 len = strlen(cmd);
1531#ifdef CONFIG_SYS_HUSH_PARSER
1532 /* hush will never change our string */
1533 need_buff = 0;
1534#else
1535 /* the built-in parser will change our string if it sees \n */
1536 need_buff = strchr(cmd, '\n') != NULL;
1537#endif
1538 }
1539 if (need_buff) {
1540 buff = malloc(len + 1);
1541 if (!buff)
1542 return 1;
1543 memcpy(buff, cmd, len);
1544 buff[len] = '\0';
1545 }
1546#ifdef CONFIG_SYS_HUSH_PARSER
1547 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1548#else
1549 /*
1550 * This function will overwrite any \n it sees with a \0, which
1551 * is why it can't work with a const char *. Here we are making
1552 * using of internal knowledge of this function, to avoid always
1553 * doing a malloc() which is actually required only in a case that
1554 * is pretty rare.
1555 */
1556 rcode = builtin_run_command_list(buff, flag);
1557 if (need_buff)
1558 free(buff);
1559#endif
1560
1561 return rcode;
1562}
1563
c609719b
WD
1564/****************************************************************************/
1565
c3517f91 1566#if defined(CONFIG_CMD_RUN)
54841ab5 1567int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
c609719b
WD
1568{
1569 int i;
c609719b 1570
47e26b1b 1571 if (argc < 2)
4c12eeb8 1572 return CMD_RET_USAGE;
c609719b
WD
1573
1574 for (i=1; i<argc; ++i) {
3e38691e
WD
1575 char *arg;
1576
1577 if ((arg = getenv (argv[i])) == NULL) {
1578 printf ("## Error: \"%s\" not defined\n", argv[i]);
1579 return 1;
1580 }
c8a2079e 1581
009dde19 1582 if (run_command(arg, flag) != 0)
3e38691e 1583 return 1;
c609719b 1584 }
3e38691e 1585 return 0;
c609719b 1586}
90253178 1587#endif