Blame view

common/main.c 34.1 KB
c609719b8   wdenk   Initial revision
1
2
3
4
  /*
   * (C) Copyright 2000
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
501090aaa   Wolfgang Denk   Add readline cmdl...
5
6
7
8
   * Add to readline cmdline-editing by
   * (C) Copyright 2005
   * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
   *
c609719b8   wdenk   Initial revision
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   * See file CREDITS for list of people who contributed to this
   * project.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2 of
   * the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   * MA 02111-1307 USA
   */
a6c7ad2f6   wdenk   * Fix startup pro...
27
  /* #define	DEBUG	*/
c609719b8   wdenk   Initial revision
28
29
30
  #include <common.h>
  #include <watchdog.h>
  #include <command.h>
224b72e63   Che-Liang Chiou   fdt: Load boot co...
31
  #include <fdtdec.h>
d51004a83   Simon Glass   Add run_command_l...
32
  #include <malloc.h>
09c2e90c1   Andreas Bießmann   unify version_string
33
  #include <version.h>
d9631ecf5   Wolfgang Denk   Fix compiler warning
34
35
36
  #ifdef CONFIG_MODEM_SUPPORT
  #include <malloc.h>		/* for free() prototype */
  #endif
c609719b8   wdenk   Initial revision
37

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
38
  #ifdef CONFIG_SYS_HUSH_PARSER
c609719b8   wdenk   Initial revision
39
40
  #include <hush.h>
  #endif
224b72e63   Che-Liang Chiou   fdt: Load boot co...
41
42
43
  #ifdef CONFIG_OF_CONTROL
  #include <fdtdec.h>
  #endif
bdccc4fed   wdenk   * Map ISP1362 USB...
44
  #include <post.h>
4d91a6eca   Jason Hobbs   Replace space and...
45
  #include <linux/ctype.h>
317d6c57d   Heiko Schocher   common, menu: sho...
46
  #include <menu.h>
bdccc4fed   wdenk   * Map ISP1362 USB...
47

d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
48
  DECLARE_GLOBAL_DATA_PTR;
d87080b72   Wolfgang Denk   GCC-4.x fixes: cl...
49

fad634071   Heiko Schocher   make show_boot_pr...
50
51
52
53
  /*
   * Board-specific Platform code can reimplement show_boot_progress () if needed
   */
  void inline __show_boot_progress (int val) {}
5e2c08c3a   Emil Medve   Remove inline qua...
54
  void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
fad634071   Heiko Schocher   make show_boot_pr...
55

4bae90904   Bartlomiej Sieka   Automatic softwar...
56
  #if defined(CONFIG_UPDATE_TFTP)
8d6b73202   Andreas Pretzsch   automatic update ...
57
  int update_tftp (ulong addr);
4bae90904   Bartlomiej Sieka   Automatic softwar...
58
  #endif /* CONFIG_UPDATE_TFTP */
8bde7f776   wdenk   * Code cleanup:
59

c609719b8   wdenk   Initial revision
60
  #define MAX_DELAY_STOP_STR 32
c609719b8   wdenk   Initial revision
61
  #undef DEBUG_PARSER
6475b9f91   John Schmoller   console: Fix cons...
62
  char        console_buffer[CONFIG_SYS_CBSIZE + 1];	/* console I/O buffer	*/
c609719b8   wdenk   Initial revision
63

3ca9122fe   Stefan Roese   Fix CONFIG_CMDLIN...
64
  static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
82359aec4   Mike Frysinger   cmd editing: mark...
65
66
  static const char erase_seq[] = "\b \b";		/* erase sequence	*/
  static const char   tab_seq[] = "        ";		/* used to expand TABs	*/
c609719b8   wdenk   Initial revision
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  
  #ifdef CONFIG_BOOT_RETRY_TIME
  static uint64_t endtime = 0;  /* must be set, default is instant timeout */
  static int      retry_time = -1; /* -1 so can call readline before main_loop */
  #endif
  
  #define	endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  
  #ifndef CONFIG_BOOT_RETRY_MIN
  #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  #endif
  
  #ifdef CONFIG_MODEM_SUPPORT
  int do_mdm_init = 0;
  extern void mdm_init(void); /* defined in board.c */
  #endif
  
  /***************************************************************************
   * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
c8a2079e4   Jason Hobbs   common: add run_c...
86
   * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
c609719b8   wdenk   Initial revision
87
   */
d514544fe   Joe Hershberger   CONFIG_BOOTDELAY ...
88
  #if defined(CONFIG_BOOTDELAY)
c609719b8   wdenk   Initial revision
89
  # if defined(CONFIG_AUTOBOOT_KEYED)
b41bc5a82   Jason Hobbs   common, menu: use...
90
91
92
93
  #ifndef CONFIG_MENU
  static inline
  #endif
  int abortboot(int bootdelay)
c609719b8   wdenk   Initial revision
94
95
96
  {
  	int abort = 0;
  	uint64_t etime = endtick(bootdelay);
19973b6ad   Wolfgang Denk   Minor code cleanup.
97
  	struct {
c609719b8   wdenk   Initial revision
98
99
100
101
  		char* str;
  		u_int len;
  		int retry;
  	}
19973b6ad   Wolfgang Denk   Minor code cleanup.
102
  	delaykey [] = {
c609719b8   wdenk   Initial revision
103
104
105
106
107
108
109
110
111
112
  		{ str: getenv ("bootdelaykey"),  retry: 1 },
  		{ str: getenv ("bootdelaykey2"), retry: 1 },
  		{ str: getenv ("bootstopkey"),   retry: 0 },
  		{ str: getenv ("bootstopkey2"),  retry: 0 },
  	};
  
  	char presskey [MAX_DELAY_STOP_STR];
  	u_int presskey_len = 0;
  	u_int presskey_max = 0;
  	u_int i;
a5aae0a1a   Dirk Eibach   Consider CONFIG_Z...
113
114
115
116
  #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
  	if (bootdelay == 0)
  		return 0;
  #endif
c609719b8   wdenk   Initial revision
117
  #  ifdef CONFIG_AUTOBOOT_PROMPT
f2302d443   Stefan Roese   Fix merge problems
118
  	printf(CONFIG_AUTOBOOT_PROMPT);
c609719b8   wdenk   Initial revision
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  #  endif
  
  #  ifdef CONFIG_AUTOBOOT_DELAY_STR
  	if (delaykey[0].str == NULL)
  		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  #  endif
  #  ifdef CONFIG_AUTOBOOT_DELAY_STR2
  	if (delaykey[1].str == NULL)
  		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
  #  endif
  #  ifdef CONFIG_AUTOBOOT_STOP_STR
  	if (delaykey[2].str == NULL)
  		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
  #  endif
  #  ifdef CONFIG_AUTOBOOT_STOP_STR2
  	if (delaykey[3].str == NULL)
  		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
  #  endif
  
  	for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  		delaykey[i].len = delaykey[i].str == NULL ?
  				    0 : strlen (delaykey[i].str);
  		delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  				    MAX_DELAY_STOP_STR : delaykey[i].len;
  
  		presskey_max = presskey_max > delaykey[i].len ?
  				    presskey_max : delaykey[i].len;
  
  #  if DEBUG_BOOTKEYS
  		printf("%s key:<%s>
  ",
  		       delaykey[i].retry ? "delay" : "stop",
  		       delaykey[i].str ? delaykey[i].str : "NULL");
  #  endif
  	}
  
  	/* In order to keep up with incoming data, check timeout only
  	 * when catch up.
  	 */
c3284b030   Peter Korsgaard   common/main: supp...
158
159
160
161
162
163
164
165
166
167
168
169
  	do {
  		if (tstc()) {
  			if (presskey_len < presskey_max) {
  				presskey [presskey_len ++] = getc();
  			}
  			else {
  				for (i = 0; i < presskey_max - 1; i ++)
  					presskey [i] = presskey [i + 1];
  
  				presskey [i] = getc();
  			}
  		}
c609719b8   wdenk   Initial revision
170
171
172
173
  		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  			if (delaykey[i].len > 0 &&
  			    presskey_len >= delaykey[i].len &&
  			    memcmp (presskey + presskey_len - delaykey[i].len,
8bde7f776   wdenk   * Code cleanup:
174
  				    delaykey[i].str,
c609719b8   wdenk   Initial revision
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  				    delaykey[i].len) == 0) {
  #  if DEBUG_BOOTKEYS
  				printf("got %skey
  ",
  				       delaykey[i].retry ? "delay" : "stop");
  #  endif
  
  #  ifdef CONFIG_BOOT_RETRY_TIME
  				/* don't retry auto boot */
  				if (! delaykey[i].retry)
  					retry_time = -1;
  #  endif
  				abort = 1;
  			}
  		}
c3284b030   Peter Korsgaard   common/main: supp...
190
  	} while (!abort && get_ticks() <= etime);
c609719b8   wdenk   Initial revision
191

c609719b8   wdenk   Initial revision
192
193
  #  if DEBUG_BOOTKEYS
  	if (!abort)
ada4d4009   Ladislav Michl   [PATCH] simplify ...
194
195
  		puts("key timeout
  ");
c609719b8   wdenk   Initial revision
196
  #  endif
8cb8143ef   dzu   * Correct header ...
197
  #ifdef CONFIG_SILENT_CONSOLE
4ec5bd55e   Ladislav Michl   [PATCH] simplify ...
198
199
  	if (abort)
  		gd->flags &= ~GD_FLG_SILENT;
8cb8143ef   dzu   * Correct header ...
200
  #endif
c609719b8   wdenk   Initial revision
201
202
203
204
  	return abort;
  }
  
  # else	/* !defined(CONFIG_AUTOBOOT_KEYED) */
c7de829c7   wdenk   * Patch by Thomas...
205
206
207
  #ifdef CONFIG_MENUKEY
  static int menukey = 0;
  #endif
b41bc5a82   Jason Hobbs   common, menu: use...
208
209
210
211
  #ifndef CONFIG_MENU
  static inline
  #endif
  int abortboot(int bootdelay)
c609719b8   wdenk   Initial revision
212
213
  {
  	int abort = 0;
b2f3e0ea3   Jim Lin   console: USB: KBD...
214
  	unsigned long ts;
c609719b8   wdenk   Initial revision
215

c7de829c7   wdenk   * Patch by Thomas...
216
  #ifdef CONFIG_MENUPROMPT
f2302d443   Stefan Roese   Fix merge problems
217
  	printf(CONFIG_MENUPROMPT);
c7de829c7   wdenk   * Patch by Thomas...
218
  #else
93d7212fa   Joe Hershberger   Allow runtime con...
219
220
  	if (bootdelay >= 0)
  		printf("Hit any key to stop autoboot: %2d ", bootdelay);
c7de829c7   wdenk   * Patch by Thomas...
221
  #endif
c609719b8   wdenk   Initial revision
222
223
  
  #if defined CONFIG_ZERO_BOOTDELAY_CHECK
8bde7f776   wdenk   * Code cleanup:
224
225
226
227
  	/*
  	 * Check if key already pressed
  	 * Don't check if bootdelay < 0
  	 */
c609719b8   wdenk   Initial revision
228
229
230
  	if (bootdelay >= 0) {
  		if (tstc()) {	/* we got a key press	*/
  			(void) getc();  /* consume input	*/
4b9206ed5   wdenk   * Patches by Thom...
231
  			puts ("\b\b\b 0");
4ec5bd55e   Ladislav Michl   [PATCH] simplify ...
232
  			abort = 1;	/* don't auto boot	*/
c609719b8   wdenk   Initial revision
233
  		}
8bde7f776   wdenk   * Code cleanup:
234
  	}
c609719b8   wdenk   Initial revision
235
  #endif
f72da3406   wdenk   Added config opti...
236
  	while ((bootdelay > 0) && (!abort)) {
c609719b8   wdenk   Initial revision
237
  		--bootdelay;
b2f3e0ea3   Jim Lin   console: USB: KBD...
238
239
240
  		/* delay 1000 ms */
  		ts = get_timer(0);
  		do {
c609719b8   wdenk   Initial revision
241
242
243
  			if (tstc()) {	/* we got a key press	*/
  				abort  = 1;	/* don't auto boot	*/
  				bootdelay = 0;	/* no more delay	*/
c7de829c7   wdenk   * Patch by Thomas...
244
245
246
  # ifdef CONFIG_MENUKEY
  				menukey = getc();
  # else
c609719b8   wdenk   Initial revision
247
  				(void) getc();  /* consume input	*/
c7de829c7   wdenk   * Patch by Thomas...
248
  # endif
c609719b8   wdenk   Initial revision
249
250
  				break;
  			}
ada4d4009   Ladislav Michl   [PATCH] simplify ...
251
  			udelay(10000);
b2f3e0ea3   Jim Lin   console: USB: KBD...
252
  		} while (!abort && get_timer(ts) < 1000);
c609719b8   wdenk   Initial revision
253

ada4d4009   Ladislav Michl   [PATCH] simplify ...
254
  		printf("\b\b\b%2d ", bootdelay);
c609719b8   wdenk   Initial revision
255
  	}
ada4d4009   Ladislav Michl   [PATCH] simplify ...
256
257
  	putc('
  ');
c609719b8   wdenk   Initial revision
258

f72da3406   wdenk   Added config opti...
259
  #ifdef CONFIG_SILENT_CONSOLE
4ec5bd55e   Ladislav Michl   [PATCH] simplify ...
260
261
  	if (abort)
  		gd->flags &= ~GD_FLG_SILENT;
f72da3406   wdenk   Added config opti...
262
  #endif
c609719b8   wdenk   Initial revision
263
264
265
  	return abort;
  }
  # endif	/* CONFIG_AUTOBOOT_KEYED */
d514544fe   Joe Hershberger   CONFIG_BOOTDELAY ...
266
  #endif	/* CONFIG_BOOTDELAY */
c609719b8   wdenk   Initial revision
267

67e1ea26e   Doug Anderson   fdt: Allow device...
268
269
270
271
272
273
274
275
276
277
278
279
280
  /*
   * Runs the given boot command securely.  Specifically:
   * - Doesn't run the command with the shell (run_command or parse_string_outer),
   *   since that's a lot of code surface that an attacker might exploit.
   *   Because of this, we don't do any argument parsing--the secure boot command
   *   has to be a full-fledged u-boot command.
   * - Doesn't check for keypresses before booting, since that could be a
   *   security hole; also disables Ctrl-C.
   * - Doesn't allow the command to return.
   *
   * Upon any failures, this function will drop into an infinite loop after
   * printing the error message to console.
   */
d514544fe   Joe Hershberger   CONFIG_BOOTDELAY ...
281
  #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
67e1ea26e   Doug Anderson   fdt: Allow device...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  static void secure_boot_cmd(char *cmd)
  {
  	cmd_tbl_t *cmdtp;
  	int rc;
  
  	if (!cmd) {
  		printf("## Error: Secure boot command not specified
  ");
  		goto err;
  	}
  
  	/* Disable Ctrl-C just in case some command is used that checks it. */
  	disable_ctrlc(1);
  
  	/* Find the command directly. */
  	cmdtp = find_cmd(cmd);
  	if (!cmdtp) {
  		printf("## Error: \"%s\" not defined
  ", cmd);
  		goto err;
  	}
  
  	/* Run the command, forcing no flags and faking argc and argv. */
  	rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
  
  	/* Shouldn't ever return from boot command. */
  	printf("## Error: \"%s\" returned (code %d)
  ", cmd, rc);
  
  err:
  	/*
  	 * Not a whole lot to do here.  Rebooting won't help much, since we'll
  	 * just end up right back here.  Just loop.
  	 */
  	hang();
  }
fcabc24f4   Simon Glass   fdt: Set kernaddr...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  static void process_fdt_options(const void *blob)
  {
  	ulong addr;
  
  	/* Add an env variable to point to a kernel payload, if available */
  	addr = fdtdec_get_config_int(gd->fdt_blob, "kernel-offset", 0);
  	if (addr)
  		setenv_addr("kernaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  
  	/* Add an env variable to point to a root disk, if available */
  	addr = fdtdec_get_config_int(gd->fdt_blob, "rootdisk-offset", 0);
  	if (addr)
  		setenv_addr("rootaddr", (void *)(CONFIG_SYS_TEXT_BASE + addr));
  }
67e1ea26e   Doug Anderson   fdt: Allow device...
332
  #endif /* CONFIG_OF_CONTROL */
c609719b8   wdenk   Initial revision
333
334
335
336
  /****************************************************************************/
  
  void main_loop (void)
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
337
338
  #ifndef CONFIG_SYS_HUSH_PARSER
  	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
c609719b8   wdenk   Initial revision
339
340
341
342
  	int len;
  	int rc = 1;
  	int flag;
  #endif
d514544fe   Joe Hershberger   CONFIG_BOOTDELAY ...
343
  #if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL)
224b72e63   Che-Liang Chiou   fdt: Load boot co...
344
345
  	char *env;
  #endif
d514544fe   Joe Hershberger   CONFIG_BOOTDELAY ...
346
  #if defined(CONFIG_BOOTDELAY)
c609719b8   wdenk   Initial revision
347
348
349
350
351
352
  	char *s;
  	int bootdelay;
  #endif
  #ifdef CONFIG_PREBOOT
  	char *p;
  #endif
bdccc4fed   wdenk   * Map ISP1362 USB...
353
354
355
356
357
358
  #ifdef CONFIG_BOOTCOUNT_LIMIT
  	unsigned long bootcount = 0;
  	unsigned long bootlimit = 0;
  	char *bcs;
  	char bcs_set[16];
  #endif /* CONFIG_BOOTCOUNT_LIMIT */
c609719b8   wdenk   Initial revision
359

01433d600   Simon Glass   Add new bootstage...
360
  	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
bdccc4fed   wdenk   * Map ISP1362 USB...
361
362
363
364
365
366
367
368
369
  #ifdef CONFIG_BOOTCOUNT_LIMIT
  	bootcount = bootcount_load();
  	bootcount++;
  	bootcount_store (bootcount);
  	sprintf (bcs_set, "%lu", bootcount);
  	setenv ("bootcount", bcs_set);
  	bcs = getenv ("bootlimit");
  	bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  #endif /* CONFIG_BOOTCOUNT_LIMIT */
c609719b8   wdenk   Initial revision
370
371
372
373
  #ifdef CONFIG_MODEM_SUPPORT
  	debug ("DEBUG: main_loop:   do_mdm_init=%d
  ", do_mdm_init);
  	if (do_mdm_init) {
77ddac948   Wolfgang Denk   Cleanup for GCC-4.x
374
  		char *str = strdup(getenv("mdm_cmd"));
c609719b8   wdenk   Initial revision
375
376
377
378
379
380
  		setenv ("preboot", str);  /* set or delete definition */
  		if (str != NULL)
  			free (str);
  		mdm_init(); /* wait for modem connection */
  	}
  #endif  /* CONFIG_MODEM_SUPPORT */
0587597ca   stroese   U-Boot version en...
381
382
  #ifdef CONFIG_VERSION_VARIABLE
  	{
155cb0104   stroese   - Fix bug in CONF...
383
  		setenv ("ver", version_string);  /* set version variable */
0587597ca   stroese   U-Boot version en...
384
385
  	}
  #endif /* CONFIG_VERSION_VARIABLE */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
386
  #ifdef CONFIG_SYS_HUSH_PARSER
c609719b8   wdenk   Initial revision
387
388
  	u_boot_hush_start ();
  #endif
81473f678   Heiko Schocher   hush: add showvar...
389
390
391
  #if defined(CONFIG_HUSH_INIT_VAR)
  	hush_init_var ();
  #endif
c609719b8   wdenk   Initial revision
392
393
394
395
396
  #ifdef CONFIG_PREBOOT
  	if ((p = getenv ("preboot")) != NULL) {
  # ifdef CONFIG_AUTOBOOT_KEYED
  		int prev = disable_ctrlc(1);	/* disable Control C checking */
  # endif
3a8a02beb   Simon Glass   Allow newlines wi...
397
  		run_command_list(p, -1, 0);
c609719b8   wdenk   Initial revision
398
399
400
401
402
403
  
  # ifdef CONFIG_AUTOBOOT_KEYED
  		disable_ctrlc(prev);	/* restore Control C checking */
  # endif
  	}
  #endif /* CONFIG_PREBOOT */
143cd21fe   Wolfgang Denk   Move CONFIG_UPDAT...
404
  #if defined(CONFIG_UPDATE_TFTP)
8d6b73202   Andreas Pretzsch   automatic update ...
405
  	update_tftp (0UL);
143cd21fe   Wolfgang Denk   Move CONFIG_UPDAT...
406
  #endif /* CONFIG_UPDATE_TFTP */
d514544fe   Joe Hershberger   CONFIG_BOOTDELAY ...
407
  #if defined(CONFIG_BOOTDELAY)
c609719b8   wdenk   Initial revision
408
409
  	s = getenv ("bootdelay");
  	bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
a6c7ad2f6   wdenk   * Fix startup pro...
410
411
412
  	debug ("### main_loop entered: bootdelay=%d
  
  ", bootdelay);
c609719b8   wdenk   Initial revision
413

317d6c57d   Heiko Schocher   common, menu: sho...
414
415
416
  #if defined(CONFIG_MENU_SHOW)
  	bootdelay = menu_show(bootdelay);
  #endif
c609719b8   wdenk   Initial revision
417
  # ifdef CONFIG_BOOT_RETRY_TIME
6dd652fa4   wdenk   Patches by Murray...
418
  	init_cmd_timeout ();
c609719b8   wdenk   Initial revision
419
  # endif	/* CONFIG_BOOT_RETRY_TIME */
b428f6a8c   Yuri Tikhonov   The patch introdu...
420
421
422
423
424
425
  #ifdef CONFIG_POST
  	if (gd->flags & GD_FLG_POSTFAIL) {
  		s = getenv("failbootcmd");
  	}
  	else
  #endif /* CONFIG_POST */
bdccc4fed   wdenk   * Map ISP1362 USB...
426
427
428
429
430
431
432
433
434
435
  #ifdef CONFIG_BOOTCOUNT_LIMIT
  	if (bootlimit && (bootcount > bootlimit)) {
  		printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.
  ",
  		        (unsigned)bootlimit);
  		s = getenv ("altbootcmd");
  	}
  	else
  #endif /* CONFIG_BOOTCOUNT_LIMIT */
  		s = getenv ("bootcmd");
224b72e63   Che-Liang Chiou   fdt: Load boot co...
436
437
438
439
440
  #ifdef CONFIG_OF_CONTROL
  	/* Allow the fdt to override the boot command */
  	env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
  	if (env)
  		s = env;
67e1ea26e   Doug Anderson   fdt: Allow device...
441

fcabc24f4   Simon Glass   fdt: Set kernaddr...
442
  	process_fdt_options(gd->fdt_blob);
67e1ea26e   Doug Anderson   fdt: Allow device...
443
444
445
446
447
448
449
  	/*
  	 * If the bootsecure option was chosen, use secure_boot_cmd().
  	 * Always use 'env' in this case, since bootsecure requres that the
  	 * bootcmd was specified in the FDT too.
  	 */
  	if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
  		secure_boot_cmd(env);
224b72e63   Che-Liang Chiou   fdt: Load boot co...
450
  #endif /* CONFIG_OF_CONTROL */
a6c7ad2f6   wdenk   * Fix startup pro...
451
452
453
  
  	debug ("### main_loop: bootcmd=\"%s\"
  ", s ? s : "<UNDEFINED>");
93d7212fa   Joe Hershberger   Allow runtime con...
454
  	if (bootdelay != -1 && s && !abortboot(bootdelay)) {
c609719b8   wdenk   Initial revision
455
456
457
  # ifdef CONFIG_AUTOBOOT_KEYED
  		int prev = disable_ctrlc(1);	/* disable Control C checking */
  # endif
3a8a02beb   Simon Glass   Allow newlines wi...
458
  		run_command_list(s, -1, 0);
c609719b8   wdenk   Initial revision
459
460
461
462
463
  
  # ifdef CONFIG_AUTOBOOT_KEYED
  		disable_ctrlc(prev);	/* restore Control C checking */
  # endif
  	}
c7de829c7   wdenk   * Patch by Thomas...
464
465
  
  # ifdef CONFIG_MENUKEY
a6c7ad2f6   wdenk   * Fix startup pro...
466
  	if (menukey == CONFIG_MENUKEY) {
370d1e3e5   Jason Hobbs   cosmetic, main: c...
467
  		s = getenv("menucmd");
c8a2079e4   Jason Hobbs   common: add run_c...
468
  		if (s)
3a8a02beb   Simon Glass   Allow newlines wi...
469
  			run_command_list(s, -1, 0);
c7de829c7   wdenk   * Patch by Thomas...
470
471
  	}
  #endif /* CONFIG_MENUKEY */
953b7e629   Wolfgang Denk   Remove AmigaOneG3...
472
  #endif /* CONFIG_BOOTDELAY */
c7de829c7   wdenk   * Patch by Thomas...
473

c609719b8   wdenk   Initial revision
474
475
476
  	/*
  	 * Main Loop for Monitor Command Processing
  	 */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
477
  #ifdef CONFIG_SYS_HUSH_PARSER
c609719b8   wdenk   Initial revision
478
479
480
481
482
483
484
485
486
487
488
489
490
  	parse_file_outer();
  	/* This point is never reached */
  	for (;;);
  #else
  	for (;;) {
  #ifdef CONFIG_BOOT_RETRY_TIME
  		if (rc >= 0) {
  			/* Saw enough of a valid command to
  			 * restart the timeout.
  			 */
  			reset_cmd_timeout();
  		}
  #endif
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
491
  		len = readline (CONFIG_SYS_PROMPT);
c609719b8   wdenk   Initial revision
492
493
494
495
496
497
498
499
500
501
  
  		flag = 0;	/* assume no special flags for now */
  		if (len > 0)
  			strcpy (lastcommand, console_buffer);
  		else if (len == 0)
  			flag |= CMD_FLAG_REPEAT;
  #ifdef CONFIG_BOOT_RETRY_TIME
  		else if (len == -2) {
  			/* -2 means timed out, retry autoboot
  			 */
4b9206ed5   wdenk   * Patches by Thom...
502
503
504
  			puts ("
  Timed out waiting for command
  ");
c609719b8   wdenk   Initial revision
505
506
507
508
509
510
511
512
513
514
  # ifdef CONFIG_RESET_TO_RETRY
  			/* Reinit board to run initialization code again */
  			do_reset (NULL, 0, 0, NULL);
  # else
  			return;		/* retry autoboot */
  # endif
  		}
  #endif
  
  		if (len == -1)
4b9206ed5   wdenk   * Patches by Thom...
515
516
  			puts ("<INTERRUPT>
  ");
c609719b8   wdenk   Initial revision
517
  		else
530715323   Simon Glass   Stop using builti...
518
  			rc = run_command(lastcommand, flag);
c609719b8   wdenk   Initial revision
519
520
521
522
523
524
  
  		if (rc <= 0) {
  			/* invalid command or not repeatable, forget it */
  			lastcommand[0] = 0;
  		}
  	}
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
525
  #endif /*CONFIG_SYS_HUSH_PARSER*/
c609719b8   wdenk   Initial revision
526
  }
6dd652fa4   wdenk   Patches by Murray...
527
528
  #ifdef CONFIG_BOOT_RETRY_TIME
  /***************************************************************************
19973b6ad   Wolfgang Denk   Minor code cleanup.
529
   * initialize command line timeout
6dd652fa4   wdenk   Patches by Murray...
530
531
532
533
534
535
   */
  void init_cmd_timeout(void)
  {
  	char *s = getenv ("bootretry");
  
  	if (s != NULL)
b028f7151   wdenk   * Patch by Yuli B...
536
  		retry_time = (int)simple_strtol(s, NULL, 10);
6dd652fa4   wdenk   Patches by Murray...
537
538
539
540
541
542
  	else
  		retry_time =  CONFIG_BOOT_RETRY_TIME;
  
  	if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  		retry_time = CONFIG_BOOT_RETRY_MIN;
  }
c609719b8   wdenk   Initial revision
543
544
545
  /***************************************************************************
   * reset command line timeout to retry_time seconds
   */
c609719b8   wdenk   Initial revision
546
547
548
549
550
  void reset_cmd_timeout(void)
  {
  	endtime = endtick(retry_time);
  }
  #endif
501090aaa   Wolfgang Denk   Add readline cmdl...
551
552
553
554
555
556
  #ifdef CONFIG_CMDLINE_EDITING
  
  /*
   * cmdline-editing related codes from vivi.
   * Author: Janghoon Lyu <nandy@mizi.com>
   */
501090aaa   Wolfgang Denk   Add readline cmdl...
557
  #define putnstr(str,n)	do {			\
dc4b0b38d   Andrew Klossner   Fix printf errors.
558
  		printf ("%.*s", (int)n, str);	\
501090aaa   Wolfgang Denk   Add readline cmdl...
559
  	} while (0)
501090aaa   Wolfgang Denk   Add readline cmdl...
560
561
  
  #define CTL_CH(c)		((c) - 'a' + 1)
501090aaa   Wolfgang Denk   Add readline cmdl...
562
563
564
565
566
567
568
569
570
571
  #define CTL_BACKSPACE		('\b')
  #define DEL			((char)255)
  #define DEL7			((char)127)
  #define CREAD_HIST_CHAR		('!')
  
  #define getcmd_putch(ch)	putc(ch)
  #define getcmd_getch()		getc()
  #define getcmd_cbeep()		getcmd_putch('\a')
  
  #define HIST_MAX		20
8804ae3b7   Peter Tyser   Replace MAX_CMDBU...
572
  #define HIST_SIZE		CONFIG_SYS_CBSIZE
501090aaa   Wolfgang Denk   Add readline cmdl...
573

199adb601   Kim Phillips   common/misc: spar...
574
575
  static int hist_max;
  static int hist_add_idx;
501090aaa   Wolfgang Denk   Add readline cmdl...
576
  static int hist_cur = -1;
199adb601   Kim Phillips   common/misc: spar...
577
  static unsigned hist_num;
501090aaa   Wolfgang Denk   Add readline cmdl...
578

199adb601   Kim Phillips   common/misc: spar...
579
580
  static char *hist_list[HIST_MAX];
  static char hist_lines[HIST_MAX][HIST_SIZE + 1];	/* Save room for NULL */
501090aaa   Wolfgang Denk   Add readline cmdl...
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  
  #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  
  static void hist_init(void)
  {
  	int i;
  
  	hist_max = 0;
  	hist_add_idx = 0;
  	hist_cur = -1;
  	hist_num = 0;
  
  	for (i = 0; i < HIST_MAX; i++) {
  		hist_list[i] = hist_lines[i];
  		hist_list[i][0] = '\0';
  	}
  }
  
  static void cread_add_to_hist(char *line)
  {
  	strcpy(hist_list[hist_add_idx], line);
  
  	if (++hist_add_idx >= HIST_MAX)
  		hist_add_idx = 0;
  
  	if (hist_add_idx > hist_max)
  		hist_max = hist_add_idx;
  
  	hist_num++;
  }
  
  static char* hist_prev(void)
  {
  	char *ret;
  	int old_cur;
  
  	if (hist_cur < 0)
  		return NULL;
  
  	old_cur = hist_cur;
  	if (--hist_cur < 0)
  		hist_cur = hist_max;
  
  	if (hist_cur == hist_add_idx) {
  		hist_cur = old_cur;
  		ret = NULL;
  	} else
  		ret = hist_list[hist_cur];
  
  	return (ret);
  }
  
  static char* hist_next(void)
  {
  	char *ret;
  
  	if (hist_cur < 0)
  		return NULL;
  
  	if (hist_cur == hist_add_idx)
  		return NULL;
  
  	if (++hist_cur > hist_max)
  		hist_cur = 0;
  
  	if (hist_cur == hist_add_idx) {
  		ret = "";
  	} else
  		ret = hist_list[hist_cur];
  
  	return (ret);
  }
3ca9122fe   Stefan Roese   Fix CONFIG_CMDLIN...
653
  #ifndef CONFIG_CMDLINE_EDITING
501090aaa   Wolfgang Denk   Add readline cmdl...
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  static void cread_print_hist_list(void)
  {
  	int i;
  	unsigned long n;
  
  	n = hist_num - hist_max;
  
  	i = hist_add_idx + 1;
  	while (1) {
  		if (i > hist_max)
  			i = 0;
  		if (i == hist_add_idx)
  			break;
  		printf("%s
  ", hist_list[i]);
  		n++;
  		i++;
  	}
  }
3ca9122fe   Stefan Roese   Fix CONFIG_CMDLIN...
673
  #endif /* CONFIG_CMDLINE_EDITING */
501090aaa   Wolfgang Denk   Add readline cmdl...
674
675
676
677
678
679
680
681
682
683
  
  #define BEGINNING_OF_LINE() {			\
  	while (num) {				\
  		getcmd_putch(CTL_BACKSPACE);	\
  		num--;				\
  	}					\
  }
  
  #define ERASE_TO_EOL() {				\
  	if (num < eol_num) {				\
8faba4894   Mike Frysinger   cmd editing: opti...
684
685
  		printf("%*s", (int)(eol_num - num), ""); \
  		do {					\
501090aaa   Wolfgang Denk   Add readline cmdl...
686
  			getcmd_putch(CTL_BACKSPACE);	\
8faba4894   Mike Frysinger   cmd editing: opti...
687
  		} while (--eol_num > num);		\
501090aaa   Wolfgang Denk   Add readline cmdl...
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
  	}						\
  }
  
  #define REFRESH_TO_EOL() {			\
  	if (num < eol_num) {			\
  		wlen = eol_num - num;		\
  		putnstr(buf + num, wlen);	\
  		num = eol_num;			\
  	}					\
  }
  
  static void cread_add_char(char ichar, int insert, unsigned long *num,
  	       unsigned long *eol_num, char *buf, unsigned long len)
  {
  	unsigned long wlen;
  
  	/* room ??? */
  	if (insert || *num == *eol_num) {
  		if (*eol_num > len - 1) {
  			getcmd_cbeep();
  			return;
  		}
  		(*eol_num)++;
  	}
  
  	if (insert) {
  		wlen = *eol_num - *num;
  		if (wlen > 1) {
  			memmove(&buf[*num+1], &buf[*num], wlen-1);
  		}
  
  		buf[*num] = ichar;
  		putnstr(buf + *num, wlen);
  		(*num)++;
  		while (--wlen) {
  			getcmd_putch(CTL_BACKSPACE);
  		}
  	} else {
  		/* echo the character */
  		wlen = 1;
  		buf[*num] = ichar;
  		putnstr(buf + *num, wlen);
  		(*num)++;
  	}
  }
  
  static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  	      unsigned long *eol_num, char *buf, unsigned long len)
  {
  	while (strsize--) {
  		cread_add_char(*str, insert, num, eol_num, buf, len);
  		str++;
  	}
  }
9c3483113   Heiko Schocher   common: add possi...
742
743
  static int cread_line(const char *const prompt, char *buf, unsigned int *len,
  		int timeout)
501090aaa   Wolfgang Denk   Add readline cmdl...
744
745
746
  {
  	unsigned long num = 0;
  	unsigned long eol_num = 0;
501090aaa   Wolfgang Denk   Add readline cmdl...
747
748
749
750
  	unsigned long wlen;
  	char ichar;
  	int insert = 1;
  	int esc_len = 0;
501090aaa   Wolfgang Denk   Add readline cmdl...
751
  	char esc_save[8];
ecc5500ee   Peter Tyser   readline(): Add a...
752
  	int init_len = strlen(buf);
9c3483113   Heiko Schocher   common: add possi...
753
  	int first = 1;
ecc5500ee   Peter Tyser   readline(): Add a...
754
755
756
  
  	if (init_len)
  		cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
501090aaa   Wolfgang Denk   Add readline cmdl...
757
758
  
  	while (1) {
00ac50e34   Andreas Engel   Make bootretry wo...
759
760
761
762
  #ifdef CONFIG_BOOT_RETRY_TIME
  		while (!tstc()) {	/* while no incoming data */
  			if (retry_time >= 0 && get_ticks() > endtime)
  				return (-2);	/* timed out */
30dc165a7   Jens Scharsig   FIX: watchdog tim...
763
  			WATCHDOG_RESET();
00ac50e34   Andreas Engel   Make bootretry wo...
764
765
  		}
  #endif
9c3483113   Heiko Schocher   common: add possi...
766
767
768
769
770
771
772
773
774
775
  		if (first && timeout) {
  			uint64_t etime = endtick(timeout);
  
  			while (!tstc()) {	/* while no incoming data */
  				if (get_ticks() >= etime)
  					return -2;	/* timed out */
  				WATCHDOG_RESET();
  			}
  			first = 0;
  		}
00ac50e34   Andreas Engel   Make bootretry wo...
776

501090aaa   Wolfgang Denk   Add readline cmdl...
777
778
779
780
  		ichar = getcmd_getch();
  
  		if ((ichar == '
  ') || (ichar == '\r')) {
dd9f06f0d   Wolfgang Denk   Minor code cleanup.
781
782
  			putc('
  ');
501090aaa   Wolfgang Denk   Add readline cmdl...
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
  			break;
  		}
  
  		/*
  		 * handle standard linux xterm esc sequences for arrow key, etc.
  		 */
  		if (esc_len != 0) {
  			if (esc_len == 1) {
  				if (ichar == '[') {
  					esc_save[esc_len] = ichar;
  					esc_len = 2;
  				} else {
  					cread_add_str(esc_save, esc_len, insert,
  						      &num, &eol_num, buf, *len);
  					esc_len = 0;
  				}
  				continue;
  			}
  
  			switch (ichar) {
  
  			case 'D':	/* <- key */
  				ichar = CTL_CH('b');
  				esc_len = 0;
  				break;
  			case 'C':	/* -> key */
  				ichar = CTL_CH('f');
  				esc_len = 0;
  				break;	/* pass off to ^F handler */
  			case 'H':	/* Home key */
  				ichar = CTL_CH('a');
  				esc_len = 0;
  				break;	/* pass off to ^A handler */
  			case 'A':	/* up arrow */
  				ichar = CTL_CH('p');
  				esc_len = 0;
  				break;	/* pass off to ^P handler */
  			case 'B':	/* down arrow */
  				ichar = CTL_CH('n');
  				esc_len = 0;
  				break;	/* pass off to ^N handler */
  			default:
  				esc_save[esc_len++] = ichar;
  				cread_add_str(esc_save, esc_len, insert,
  					      &num, &eol_num, buf, *len);
  				esc_len = 0;
  				continue;
  			}
  		}
  
  		switch (ichar) {
  		case 0x1b:
  			if (esc_len == 0) {
  				esc_save[esc_len] = ichar;
  				esc_len = 1;
  			} else {
dd9f06f0d   Wolfgang Denk   Minor code cleanup.
839
840
  				puts("impossible condition #876
  ");
501090aaa   Wolfgang Denk   Add readline cmdl...
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
  				esc_len = 0;
  			}
  			break;
  
  		case CTL_CH('a'):
  			BEGINNING_OF_LINE();
  			break;
  		case CTL_CH('c'):	/* ^C - break */
  			*buf = '\0';	/* discard input */
  			return (-1);
  		case CTL_CH('f'):
  			if (num < eol_num) {
  				getcmd_putch(buf[num]);
  				num++;
  			}
  			break;
  		case CTL_CH('b'):
  			if (num) {
  				getcmd_putch(CTL_BACKSPACE);
  				num--;
  			}
  			break;
  		case CTL_CH('d'):
  			if (num < eol_num) {
  				wlen = eol_num - num - 1;
  				if (wlen) {
  					memmove(&buf[num], &buf[num+1], wlen);
  					putnstr(buf + num, wlen);
  				}
  
  				getcmd_putch(' ');
  				do {
  					getcmd_putch(CTL_BACKSPACE);
  				} while (wlen--);
  				eol_num--;
  			}
  			break;
  		case CTL_CH('k'):
  			ERASE_TO_EOL();
  			break;
  		case CTL_CH('e'):
  			REFRESH_TO_EOL();
  			break;
  		case CTL_CH('o'):
  			insert = !insert;
  			break;
  		case CTL_CH('x'):
23d0baf96   Jean-Christophe PLAGNIOL-VILLARD   Allow CONFIG_AUTO...
888
  		case CTL_CH('u'):
501090aaa   Wolfgang Denk   Add readline cmdl...
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
  			BEGINNING_OF_LINE();
  			ERASE_TO_EOL();
  			break;
  		case DEL:
  		case DEL7:
  		case 8:
  			if (num) {
  				wlen = eol_num - num;
  				num--;
  				memmove(&buf[num], &buf[num+1], wlen);
  				getcmd_putch(CTL_BACKSPACE);
  				putnstr(buf + num, wlen);
  				getcmd_putch(' ');
  				do {
  					getcmd_putch(CTL_BACKSPACE);
  				} while (wlen--);
  				eol_num--;
  			}
  			break;
  		case CTL_CH('p'):
  		case CTL_CH('n'):
  		{
  			char * hline;
  
  			esc_len = 0;
  
  			if (ichar == CTL_CH('p'))
  				hline = hist_prev();
  			else
  				hline = hist_next();
  
  			if (!hline) {
  				getcmd_cbeep();
  				continue;
  			}
  
  			/* nuke the current line */
  			/* first, go home */
  			BEGINNING_OF_LINE();
  
  			/* erase to end of line */
  			ERASE_TO_EOL();
  
  			/* copy new line into place and display */
  			strcpy(buf, hline);
  			eol_num = strlen(buf);
  			REFRESH_TO_EOL();
  			continue;
  		}
23d0baf96   Jean-Christophe PLAGNIOL-VILLARD   Allow CONFIG_AUTO...
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
  #ifdef CONFIG_AUTO_COMPLETE
  		case '\t': {
  			int num2, col;
  
  			/* do not autocomplete when in the middle */
  			if (num < eol_num) {
  				getcmd_cbeep();
  				break;
  			}
  
  			buf[num] = '\0';
  			col = strlen(prompt) + eol_num;
  			num2 = num;
  			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  				col = num2 - num;
  				num += col;
  				eol_num += col;
  			}
  			break;
  		}
  #endif
501090aaa   Wolfgang Denk   Add readline cmdl...
959
960
961
962
963
964
965
966
967
968
969
  		default:
  			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  			break;
  		}
  	}
  	*len = eol_num;
  	buf[eol_num] = '\0';	/* lose the newline */
  
  	if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  		cread_add_to_hist(buf);
  	hist_cur = hist_add_idx;
f92394384   Peter Tyser   cread_line(): Rem...
970
  	return 0;
501090aaa   Wolfgang Denk   Add readline cmdl...
971
972
973
  }
  
  #endif /* CONFIG_CMDLINE_EDITING */
c609719b8   wdenk   Initial revision
974
975
976
977
978
979
980
981
982
983
984
985
  /****************************************************************************/
  
  /*
   * Prompt for input and read a line.
   * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
   * time out when time goes past endtime (timebase time in ticks).
   * Return:	number of read characters
   *		-1 if break
   *		-2 if timed out
   */
  int readline (const char *const prompt)
  {
ecc5500ee   Peter Tyser   readline(): Add a...
986
987
988
989
990
  	/*
  	 * If console_buffer isn't 0-length the user will be prompted to modify
  	 * it instead of entering it from scratch as desired.
  	 */
  	console_buffer[0] = '\0';
9c3483113   Heiko Schocher   common: add possi...
991
  	return readline_into_buffer(prompt, console_buffer, 0);
6636b62a6   James Yang   Expose parse_line...
992
  }
9c3483113   Heiko Schocher   common: add possi...
993
  int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
6636b62a6   James Yang   Expose parse_line...
994
995
  {
  	char *p = buffer;
501090aaa   Wolfgang Denk   Add readline cmdl...
996
  #ifdef CONFIG_CMDLINE_EDITING
8804ae3b7   Peter Tyser   Replace MAX_CMDBU...
997
  	unsigned int len = CONFIG_SYS_CBSIZE;
d8f961bbd   Stefan Roese   Fix control-c han...
998
  	int rc;
501090aaa   Wolfgang Denk   Add readline cmdl...
999
  	static int initted = 0;
597f6c26a   James Yang   Fix readline_into...
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
  	/*
  	 * History uses a global array which is not
  	 * writable until after relocation to RAM.
  	 * Revert to non-history version if still
  	 * running from flash.
  	 */
  	if (gd->flags & GD_FLG_RELOC) {
  		if (!initted) {
  			hist_init();
  			initted = 1;
  		}
e491a71e5   Peter Tyser   Check for NULL pr...
1011
1012
  		if (prompt)
  			puts (prompt);
597f6c26a   James Yang   Fix readline_into...
1013

9c3483113   Heiko Schocher   common: add possi...
1014
  		rc = cread_line(prompt, p, &len, timeout);
597f6c26a   James Yang   Fix readline_into...
1015
1016
1017
1018
  		return rc < 0 ? rc : len;
  
  	} else {
  #endif	/* CONFIG_CMDLINE_EDITING */
0ec595243   Kumar Gala   Fix compiler warning
1019
  	char * p_buf = p;
c609719b8   wdenk   Initial revision
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  	int	n = 0;				/* buffer index		*/
  	int	plen = 0;			/* prompt length	*/
  	int	col;				/* output column cnt	*/
  	char	c;
  
  	/* print prompt */
  	if (prompt) {
  		plen = strlen (prompt);
  		puts (prompt);
  	}
  	col = plen;
  
  	for (;;) {
  #ifdef CONFIG_BOOT_RETRY_TIME
  		while (!tstc()) {	/* while no incoming data */
  			if (retry_time >= 0 && get_ticks() > endtime)
  				return (-2);	/* timed out */
30dc165a7   Jens Scharsig   FIX: watchdog tim...
1037
  			WATCHDOG_RESET();
c609719b8   wdenk   Initial revision
1038
1039
1040
1041
1042
1043
  		}
  #endif
  		WATCHDOG_RESET();		/* Trigger watchdog, if needed */
  
  #ifdef CONFIG_SHOW_ACTIVITY
  		while (!tstc()) {
c609719b8   wdenk   Initial revision
1044
  			show_activity(0);
30dc165a7   Jens Scharsig   FIX: watchdog tim...
1045
  			WATCHDOG_RESET();
c609719b8   wdenk   Initial revision
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
  		}
  #endif
  		c = getc();
  
  		/*
  		 * Special character handling
  		 */
  		switch (c) {
  		case '\r':				/* Enter		*/
  		case '
  ':
  			*p = '\0';
  			puts ("\r
  ");
6636b62a6   James Yang   Expose parse_line...
1060
  			return (p - p_buf);
c609719b8   wdenk   Initial revision
1061

27aa81867   wdenk   * Patch by Carl R...
1062
1063
  		case '\0':				/* nul			*/
  			continue;
c609719b8   wdenk   Initial revision
1064
  		case 0x03:				/* ^C - break		*/
6636b62a6   James Yang   Expose parse_line...
1065
  			p_buf[0] = '\0';	/* discard input */
c609719b8   wdenk   Initial revision
1066
1067
1068
1069
1070
1071
1072
  			return (-1);
  
  		case 0x15:				/* ^U - erase line	*/
  			while (col > plen) {
  				puts (erase_seq);
  				--col;
  			}
6636b62a6   James Yang   Expose parse_line...
1073
  			p = p_buf;
c609719b8   wdenk   Initial revision
1074
1075
  			n = 0;
  			continue;
1636d1c85   Wolfgang Denk   Coding stylke cle...
1076
  		case 0x17:				/* ^W - erase word	*/
6636b62a6   James Yang   Expose parse_line...
1077
  			p=delete_char(p_buf, p, &col, &n, plen);
c609719b8   wdenk   Initial revision
1078
  			while ((n > 0) && (*p != ' ')) {
6636b62a6   James Yang   Expose parse_line...
1079
  				p=delete_char(p_buf, p, &col, &n, plen);
c609719b8   wdenk   Initial revision
1080
1081
1082
1083
1084
  			}
  			continue;
  
  		case 0x08:				/* ^H  - backspace	*/
  		case 0x7F:				/* DEL - backspace	*/
6636b62a6   James Yang   Expose parse_line...
1085
  			p=delete_char(p_buf, p, &col, &n, plen);
c609719b8   wdenk   Initial revision
1086
1087
1088
1089
1090
1091
  			continue;
  
  		default:
  			/*
  			 * Must be a normal character then
  			 */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1092
  			if (n < CONFIG_SYS_CBSIZE-2) {
c609719b8   wdenk   Initial revision
1093
  				if (c == '\t') {	/* expand TABs		*/
04a85b3b3   wdenk   * Patches by Pant...
1094
1095
1096
1097
  #ifdef CONFIG_AUTO_COMPLETE
  					/* if auto completion triggered just continue */
  					*p = '\0';
  					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
6636b62a6   James Yang   Expose parse_line...
1098
  						p = p_buf + n;	/* reset */
04a85b3b3   wdenk   * Patches by Pant...
1099
1100
1101
  						continue;
  					}
  #endif
c609719b8   wdenk   Initial revision
1102
1103
1104
  					puts (tab_seq+(col&07));
  					col += 8 - (col&07);
  				} else {
9a8efc460   Simon Glass   lcd: Add support ...
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
  					char buf[2];
  
  					/*
  					 * Echo input using puts() to force am
  					 * LCD flush if we are using an LCD
  					 */
  					++col;
  					buf[0] = c;
  					buf[1] = '\0';
  					puts(buf);
c609719b8   wdenk   Initial revision
1115
1116
1117
1118
1119
1120
1121
1122
  				}
  				*p++ = c;
  				++n;
  			} else {			/* Buffer full		*/
  				putc ('\a');
  			}
  		}
  	}
597f6c26a   James Yang   Fix readline_into...
1123
1124
1125
  #ifdef CONFIG_CMDLINE_EDITING
  	}
  #endif
c609719b8   wdenk   Initial revision
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  }
  
  /****************************************************************************/
  
  static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  {
  	char *s;
  
  	if (*np == 0) {
  		return (p);
  	}
  
  	if (*(--p) == '\t') {			/* will retype the whole line	*/
  		while (*colp > plen) {
  			puts (erase_seq);
  			(*colp)--;
  		}
  		for (s=buffer; s<p; ++s) {
  			if (*s == '\t') {
  				puts (tab_seq+((*colp) & 07));
  				*colp += 8 - ((*colp) & 07);
  			} else {
  				++(*colp);
  				putc (*s);
  			}
  		}
  	} else {
  		puts (erase_seq);
  		(*colp)--;
  	}
  	(*np)--;
  	return (p);
  }
  
  /****************************************************************************/
  
  int parse_line (char *line, char *argv[])
  {
  	int nargs = 0;
  
  #ifdef DEBUG_PARSER
  	printf ("parse_line: \"%s\"
  ", line);
  #endif
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1170
  	while (nargs < CONFIG_SYS_MAXARGS) {
c609719b8   wdenk   Initial revision
1171
1172
  
  		/* skip any white space */
4d91a6eca   Jason Hobbs   Replace space and...
1173
  		while (isblank(*line))
c609719b8   wdenk   Initial revision
1174
  			++line;
c609719b8   wdenk   Initial revision
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
  
  		if (*line == '\0') {	/* end of line, no more args	*/
  			argv[nargs] = NULL;
  #ifdef DEBUG_PARSER
  		printf ("parse_line: nargs=%d
  ", nargs);
  #endif
  			return (nargs);
  		}
  
  		argv[nargs++] = line;	/* begin of argument string	*/
  
  		/* find end of string */
4d91a6eca   Jason Hobbs   Replace space and...
1188
  		while (*line && !isblank(*line))
c609719b8   wdenk   Initial revision
1189
  			++line;
c609719b8   wdenk   Initial revision
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  
  		if (*line == '\0') {	/* end of line, no more args	*/
  			argv[nargs] = NULL;
  #ifdef DEBUG_PARSER
  		printf ("parse_line: nargs=%d
  ", nargs);
  #endif
  			return (nargs);
  		}
  
  		*line++ = '\0';		/* terminate current arg	 */
  	}
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1202
1203
  	printf ("** Too many args (max. %d) **
  ", CONFIG_SYS_MAXARGS);
c609719b8   wdenk   Initial revision
1204
1205
1206
1207
1208
1209
1210
1211
1212
  
  #ifdef DEBUG_PARSER
  	printf ("parse_line: nargs=%d
  ", nargs);
  #endif
  	return (nargs);
  }
  
  /****************************************************************************/
7fed89e00   Simon Glass   Don't include sta...
1213
  #ifndef CONFIG_SYS_HUSH_PARSER
c609719b8   wdenk   Initial revision
1214
1215
1216
1217
  static void process_macros (const char *input, char *output)
  {
  	char c, prev;
  	const char *varname_start = NULL;
19973b6ad   Wolfgang Denk   Minor code cleanup.
1218
  	int inputcnt = strlen (input);
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1219
  	int outputcnt = CONFIG_SYS_CBSIZE;
19973b6ad   Wolfgang Denk   Minor code cleanup.
1220
1221
1222
1223
1224
  	int state = 0;		/* 0 = waiting for '$'  */
  
  	/* 1 = waiting for '(' or '{' */
  	/* 2 = waiting for ')' or '}' */
  	/* 3 = waiting for '''  */
c609719b8   wdenk   Initial revision
1225
1226
  #ifdef DEBUG_PARSER
  	char *output_start = output;
19973b6ad   Wolfgang Denk   Minor code cleanup.
1227
1228
1229
  	printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"
  ", strlen (input),
  		input);
c609719b8   wdenk   Initial revision
1230
  #endif
19973b6ad   Wolfgang Denk   Minor code cleanup.
1231
  	prev = '\0';		/* previous character   */
c609719b8   wdenk   Initial revision
1232
1233
  
  	while (inputcnt && outputcnt) {
8bde7f776   wdenk   * Code cleanup:
1234
  		c = *input++;
19973b6ad   Wolfgang Denk   Minor code cleanup.
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
  		inputcnt--;
  
  		if (state != 3) {
  			/* remove one level of escape characters */
  			if ((c == '\\') && (prev != '\\')) {
  				if (inputcnt-- == 0)
  					break;
  				prev = c;
  				c = *input++;
  			}
c609719b8   wdenk   Initial revision
1245
  		}
19973b6ad   Wolfgang Denk   Minor code cleanup.
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
  
  		switch (state) {
  		case 0:	/* Waiting for (unescaped) $    */
  			if ((c == '\'') && (prev != '\\')) {
  				state = 3;
  				break;
  			}
  			if ((c == '$') && (prev != '\\')) {
  				state++;
  			} else {
c609719b8   wdenk   Initial revision
1256
1257
1258
  				*(output++) = c;
  				outputcnt--;
  			}
19973b6ad   Wolfgang Denk   Minor code cleanup.
1259
1260
1261
1262
1263
1264
1265
1266
1267
  			break;
  		case 1:	/* Waiting for (        */
  			if (c == '(' || c == '{') {
  				state++;
  				varname_start = input;
  			} else {
  				state = 0;
  				*(output++) = '$';
  				outputcnt--;
c609719b8   wdenk   Initial revision
1268

19973b6ad   Wolfgang Denk   Minor code cleanup.
1269
1270
  				if (outputcnt) {
  					*(output++) = c;
c609719b8   wdenk   Initial revision
1271
1272
  					outputcnt--;
  				}
19973b6ad   Wolfgang Denk   Minor code cleanup.
1273
1274
1275
1276
1277
  			}
  			break;
  		case 2:	/* Waiting for )        */
  			if (c == ')' || c == '}') {
  				int i;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1278
  				char envname[CONFIG_SYS_CBSIZE], *envval;
19973b6ad   Wolfgang Denk   Minor code cleanup.
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
  				int envcnt = input - varname_start - 1;	/* Varname # of chars */
  
  				/* Get the varname */
  				for (i = 0; i < envcnt; i++) {
  					envname[i] = varname_start[i];
  				}
  				envname[i] = 0;
  
  				/* Get its value */
  				envval = getenv (envname);
  
  				/* Copy into the line if it exists */
  				if (envval != NULL)
  					while ((*envval) && outputcnt) {
  						*(output++) = *(envval++);
  						outputcnt--;
  					}
  				/* Look for another '$' */
  				state = 0;
  			}
  			break;
  		case 3:	/* Waiting for '        */
  			if ((c == '\'') && (prev != '\\')) {
  				state = 0;
  			} else {
  				*(output++) = c;
  				outputcnt--;
  			}
  			break;
a25f862ba   wdenk   Patch by Detlev Z...
1308
  		}
19973b6ad   Wolfgang Denk   Minor code cleanup.
1309
  		prev = c;
c609719b8   wdenk   Initial revision
1310
1311
1312
1313
  	}
  
  	if (outputcnt)
  		*output = 0;
9160b96f7   Bartlomiej Sieka   Fix: Add missing ...
1314
1315
  	else
  		*(output - 1) = 0;
c609719b8   wdenk   Initial revision
1316
1317
1318
1319
  
  #ifdef DEBUG_PARSER
  	printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"
  ",
19973b6ad   Wolfgang Denk   Minor code cleanup.
1320
  		strlen (output_start), output_start);
c609719b8   wdenk   Initial revision
1321
1322
1323
1324
1325
1326
1327
1328
1329
  #endif
  }
  
  /****************************************************************************
   * returns:
   *	1  - command executed, repeatable
   *	0  - command executed but not repeatable, interrupted commands are
   *	     always considered not repeatable
   *	-1 - not executed (unrecognized, bootd recursion or too many args)
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1330
   *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
c609719b8   wdenk   Initial revision
1331
1332
1333
1334
1335
1336
1337
1338
1339
   *           considered unrecognized)
   *
   * WARNING:
   *
   * We must create a temporary copy of the command since the command we get
   * may be the result from getenv(), which returns a pointer directly to
   * the environment data, which may change magicly when the command we run
   * creates or modifies environment variables (like "bootp" does).
   */
530715323   Simon Glass   Stop using builti...
1340
  static int builtin_run_command(const char *cmd, int flag)
c609719b8   wdenk   Initial revision
1341
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1342
  	char cmdbuf[CONFIG_SYS_CBSIZE];	/* working copy of cmd		*/
c609719b8   wdenk   Initial revision
1343
1344
  	char *token;			/* start of token in cmdbuf	*/
  	char *sep;			/* end of token (separator) in cmdbuf */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1345
  	char finaltoken[CONFIG_SYS_CBSIZE];
c609719b8   wdenk   Initial revision
1346
  	char *str = cmdbuf;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1347
  	char *argv[CONFIG_SYS_MAXARGS + 1];	/* NULL terminated	*/
f07771cc2   wdenk   * Fix data abort ...
1348
  	int argc, inquotes;
c609719b8   wdenk   Initial revision
1349
  	int repeatable = 1;
f07771cc2   wdenk   * Fix data abort ...
1350
  	int rc = 0;
c609719b8   wdenk   Initial revision
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
  
  #ifdef DEBUG_PARSER
  	printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  	puts (cmd ? cmd : "NULL");	/* use puts - string may be loooong */
  	puts ("\"
  ");
  #endif
  
  	clear_ctrlc();		/* forget any previous Control C */
  
  	if (!cmd || !*cmd) {
  		return -1;	/* empty command */
  	}
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1364
  	if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
c609719b8   wdenk   Initial revision
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
  		puts ("## Command too long!
  ");
  		return -1;
  	}
  
  	strcpy (cmdbuf, cmd);
  
  	/* Process separators and check for invalid
  	 * repeatable commands
  	 */
  
  #ifdef DEBUG_PARSER
  	printf ("[PROCESS_SEPARATORS] %s
  ", cmd);
  #endif
  	while (*str) {
  
  		/*
  		 * Find separator, or string end
  		 * Allow simple escape of ';' by writing "\;"
  		 */
a25f862ba   wdenk   Patch by Detlev Z...
1386
1387
1388
1389
1390
1391
1392
  		for (inquotes = 0, sep = str; *sep; sep++) {
  			if ((*sep=='\'') &&
  			    (*(sep-1) != '\\'))
  				inquotes=!inquotes;
  
  			if (!inquotes &&
  			    (*sep == ';') &&	/* separator		*/
c609719b8   wdenk   Initial revision
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
  			    ( sep != str) &&	/* past string start	*/
  			    (*(sep-1) != '\\'))	/* and NOT escaped	*/
  				break;
  		}
  
  		/*
  		 * Limit the token to data between separators
  		 */
  		token = str;
  		if (*sep) {
  			str = sep + 1;	/* start of command for next pass */
  			*sep = '\0';
  		}
  		else
  			str = sep;	/* no more commands for next pass */
  #ifdef DEBUG_PARSER
  		printf ("token: \"%s\"
  ", token);
  #endif
  
  		/* find macros in this token and replace them */
  		process_macros (token, finaltoken);
  
  		/* Extract arguments */
1264b4050   Wolfgang Denk   Avoid dereferenci...
1417
1418
1419
1420
  		if ((argc = parse_line (finaltoken, argv)) == 0) {
  			rc = -1;	/* no command at all */
  			continue;
  		}
c609719b8   wdenk   Initial revision
1421

34765e885   Richard Genoud   cmd_time: merge r...
1422
  		if (cmd_process(flag, argc, argv, &repeatable, NULL))
030fca522   Timo Ketola   Fix the behaviour...
1423
  			rc = -1;
c609719b8   wdenk   Initial revision
1424
1425
1426
  
  		/* Did the user stop this? */
  		if (had_ctrlc ())
5afb20209   Detlev Zundel   Fix 'run' not to ...
1427
  			return -1;	/* if stopped then not repeatable */
c609719b8   wdenk   Initial revision
1428
  	}
f07771cc2   wdenk   * Fix data abort ...
1429
  	return rc ? rc : repeatable;
c609719b8   wdenk   Initial revision
1430
  }
7fed89e00   Simon Glass   Don't include sta...
1431
  #endif
c609719b8   wdenk   Initial revision
1432

530715323   Simon Glass   Stop using builti...
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
  /*
   * Run a command using the selected parser.
   *
   * @param cmd	Command to run
   * @param flag	Execution flags (CMD_FLAG_...)
   * @return 0 on success, or != 0 on error.
   */
  int run_command(const char *cmd, int flag)
  {
  #ifndef CONFIG_SYS_HUSH_PARSER
  	/*
  	 * builtin_run_command can return 0 or 1 for success, so clean up
  	 * its result.
  	 */
  	if (builtin_run_command(cmd, flag) == -1)
  		return 1;
  
  	return 0;
  #else
  	return parse_string_outer(cmd,
  			FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
  #endif
  }
d51004a83   Simon Glass   Add run_command_l...
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
  #ifndef CONFIG_SYS_HUSH_PARSER
  /**
   * Execute a list of command separated by ; or 
   using the built-in parser.
   *
   * This function cannot take a const char * for the command, since if it
   * finds newlines in the string, it replaces them with \0.
   *
   * @param cmd	String containing list of commands
   * @param flag	Execution flags (CMD_FLAG_...)
   * @return 0 on success, or != 0 on error.
   */
  static int builtin_run_command_list(char *cmd, int flag)
  {
  	char *line, *next;
  	int rcode = 0;
  
  	/*
  	 * Break into individual lines, and execute each line; terminate on
  	 * error.
  	 */
  	line = next = cmd;
  	while (*next) {
  		if (*next == '
  ') {
  			*next = '\0';
  			/* run only non-empty commands */
  			if (*line) {
  				debug("** exec: \"%s\"
  ", line);
  				if (builtin_run_command(line, 0) < 0) {
  					rcode = 1;
  					break;
  				}
  			}
  			line = next + 1;
  		}
  		++next;
  	}
  	if (rcode == 0 && *line)
  		rcode = (builtin_run_command(line, 0) >= 0);
  
  	return rcode;
  }
  #endif
  
  int run_command_list(const char *cmd, int len, int flag)
  {
  	int need_buff = 1;
  	char *buff = (char *)cmd;	/* cast away const */
  	int rcode = 0;
  
  	if (len == -1) {
  		len = strlen(cmd);
  #ifdef CONFIG_SYS_HUSH_PARSER
  		/* hush will never change our string */
  		need_buff = 0;
  #else
  		/* the built-in parser will change our string if it sees 
   */
  		need_buff = strchr(cmd, '
  ') != NULL;
  #endif
  	}
  	if (need_buff) {
  		buff = malloc(len + 1);
  		if (!buff)
  			return 1;
  		memcpy(buff, cmd, len);
  		buff[len] = '\0';
  	}
  #ifdef CONFIG_SYS_HUSH_PARSER
  	rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
  #else
  	/*
  	 * This function will overwrite any 
   it sees with a \0, which
  	 * is why it can't work with a const char *. Here we are making
  	 * using of internal knowledge of this function, to avoid always
  	 * doing a malloc() which is actually required only in a case that
  	 * is pretty rare.
  	 */
  	rcode = builtin_run_command_list(buff, flag);
  	if (need_buff)
  		free(buff);
  #endif
  
  	return rcode;
  }
c609719b8   wdenk   Initial revision
1545
  /****************************************************************************/
c3517f919   Jon Loeliger   common/* non-cmd*...
1546
  #if defined(CONFIG_CMD_RUN)
54841ab50   Wolfgang Denk   Make sure that ar...
1547
  int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
c609719b8   wdenk   Initial revision
1548
1549
  {
  	int i;
c609719b8   wdenk   Initial revision
1550

47e26b1bf   Wolfgang Denk   cmd_usage(): simp...
1551
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
1552
  		return CMD_RET_USAGE;
c609719b8   wdenk   Initial revision
1553
1554
  
  	for (i=1; i<argc; ++i) {
3e38691e8   wdenk   * Patch by Arun D...
1555
1556
1557
1558
1559
1560
1561
  		char *arg;
  
  		if ((arg = getenv (argv[i])) == NULL) {
  			printf ("## Error: \"%s\" not defined
  ", argv[i]);
  			return 1;
  		}
c8a2079e4   Jason Hobbs   common: add run_c...
1562

009dde195   Simon Glass   Rename run_comman...
1563
  		if (run_command(arg, flag) != 0)
3e38691e8   wdenk   * Patch by Arun D...
1564
  			return 1;
c609719b8   wdenk   Initial revision
1565
  	}
3e38691e8   wdenk   * Patch by Arun D...
1566
  	return 0;
c609719b8   wdenk   Initial revision
1567
  }
902531788   Jon Loeliger   common/: Remove l...
1568
  #endif