Blame view
common/main.c
33.3 KB
c609719b8 Initial revision |
1 2 3 4 |
/* * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * |
501090aaa 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> * |
1a4596601 Add GPL-2.0+ SPDX... |
9 |
* SPDX-License-Identifier: GPL-2.0+ |
c609719b8 Initial revision |
10 |
*/ |
a6c7ad2f6 * Fix startup pro... |
11 |
/* #define DEBUG */ |
c609719b8 Initial revision |
12 |
#include <common.h> |
c609719b8 Initial revision |
13 |
#include <command.h> |
224b72e63 fdt: Load boot co... |
14 |
#include <fdtdec.h> |
c609719b8 Initial revision |
15 |
#include <hush.h> |
fbcdf32af main: Correct hea... |
16 17 |
#include <malloc.h> #include <menu.h> |
bdccc4fed * Map ISP1362 USB... |
18 |
#include <post.h> |
fbcdf32af main: Correct hea... |
19 20 |
#include <version.h> #include <watchdog.h> |
4d91a6eca Replace space and... |
21 |
#include <linux/ctype.h> |
bdccc4fed * Map ISP1362 USB... |
22 |
|
d87080b72 GCC-4.x fixes: cl... |
23 |
DECLARE_GLOBAL_DATA_PTR; |
d87080b72 GCC-4.x fixes: cl... |
24 |
|
fad634071 make show_boot_pr... |
25 26 27 28 |
/* * Board-specific Platform code can reimplement show_boot_progress () if needed */ void inline __show_boot_progress (int val) {} |
5e2c08c3a Remove inline qua... |
29 |
void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress"))); |
fad634071 make show_boot_pr... |
30 |
|
c609719b8 Initial revision |
31 |
#define MAX_DELAY_STOP_STR 32 |
3e4088737 main: Add debug_p... |
32 33 34 35 |
#define DEBUG_PARSER 0 /* set to 1 to debug */ #define debug_parser(fmt, args...) \ debug_cond(DEBUG_PARSER, fmt, ##args) |
d34d186ef main: Add debug_b... |
36 37 38 39 40 |
#ifndef DEBUG_BOOTKEYS #define DEBUG_BOOTKEYS 0 #endif #define debug_bootkeys(fmt, args...) \ debug_cond(DEBUG_BOOTKEYS, fmt, ##args) |
c609719b8 Initial revision |
41 |
|
6475b9f91 console: Fix cons... |
42 |
char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ |
c609719b8 Initial revision |
43 |
|
3ca9122fe Fix CONFIG_CMDLIN... |
44 |
static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen); |
82359aec4 cmd editing: mark... |
45 46 |
static const char erase_seq[] = "\b \b"; /* erase sequence */ static const char tab_seq[] = " "; /* used to expand TABs */ |
c609719b8 Initial revision |
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#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 common: add run_c... |
66 |
* returns: 0 - no key string, allow autoboot 1 - got key string, abort |
c609719b8 Initial revision |
67 |
*/ |
d514544fe CONFIG_BOOTDELAY ... |
68 |
#if defined(CONFIG_BOOTDELAY) |
c609719b8 Initial revision |
69 |
# if defined(CONFIG_AUTOBOOT_KEYED) |
063ae006a main: Separate ou... |
70 |
static int abortboot_keyed(int bootdelay) |
c609719b8 Initial revision |
71 72 73 |
{ int abort = 0; uint64_t etime = endtick(bootdelay); |
19973b6ad Minor code cleanup. |
74 |
struct { |
c609719b8 Initial revision |
75 76 77 78 |
char* str; u_int len; int retry; } |
19973b6ad Minor code cleanup. |
79 |
delaykey [] = { |
c609719b8 Initial revision |
80 81 82 83 84 85 86 87 88 89 |
{ 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 Consider CONFIG_Z... |
90 91 92 93 |
#ifndef CONFIG_ZERO_BOOTDELAY_CHECK if (bootdelay == 0) return 0; #endif |
c609719b8 Initial revision |
94 |
# ifdef CONFIG_AUTOBOOT_PROMPT |
f2302d443 Fix merge problems |
95 |
printf(CONFIG_AUTOBOOT_PROMPT); |
c609719b8 Initial revision |
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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; |
d34d186ef main: Add debug_b... |
123 124 125 126 |
debug_bootkeys("%s key:<%s> ", delaykey[i].retry ? "delay" : "stop", delaykey[i].str ? delaykey[i].str : "NULL"); |
c609719b8 Initial revision |
127 128 129 130 131 |
} /* In order to keep up with incoming data, check timeout only * when catch up. */ |
c3284b030 common/main: supp... |
132 133 134 135 136 137 138 139 140 141 142 143 |
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 Initial revision |
144 145 146 147 |
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 * Code cleanup: |
148 |
delaykey[i].str, |
c609719b8 Initial revision |
149 |
delaykey[i].len) == 0) { |
d34d186ef main: Add debug_b... |
150 151 152 153 |
debug_bootkeys("got %skey ", delaykey[i].retry ? "delay" : "stop"); |
c609719b8 Initial revision |
154 155 156 157 158 159 160 161 162 |
# ifdef CONFIG_BOOT_RETRY_TIME /* don't retry auto boot */ if (! delaykey[i].retry) retry_time = -1; # endif abort = 1; } } |
c3284b030 common/main: supp... |
163 |
} while (!abort && get_ticks() <= etime); |
c609719b8 Initial revision |
164 |
|
c609719b8 Initial revision |
165 |
if (!abort) |
d34d186ef main: Add debug_b... |
166 167 |
debug_bootkeys("key timeout "); |
c609719b8 Initial revision |
168 |
|
8cb8143ef * Correct header ... |
169 |
#ifdef CONFIG_SILENT_CONSOLE |
4ec5bd55e [PATCH] simplify ... |
170 171 |
if (abort) gd->flags &= ~GD_FLG_SILENT; |
8cb8143ef * Correct header ... |
172 |
#endif |
c609719b8 Initial revision |
173 174 175 176 |
return abort; } # else /* !defined(CONFIG_AUTOBOOT_KEYED) */ |
c7de829c7 * Patch by Thomas... |
177 178 179 |
#ifdef CONFIG_MENUKEY static int menukey = 0; #endif |
063ae006a main: Separate ou... |
180 |
static int abortboot_normal(int bootdelay) |
c609719b8 Initial revision |
181 182 |
{ int abort = 0; |
b2f3e0ea3 console: USB: KBD... |
183 |
unsigned long ts; |
c609719b8 Initial revision |
184 |
|
c7de829c7 * Patch by Thomas... |
185 |
#ifdef CONFIG_MENUPROMPT |
f2302d443 Fix merge problems |
186 |
printf(CONFIG_MENUPROMPT); |
c7de829c7 * Patch by Thomas... |
187 |
#else |
93d7212fa Allow runtime con... |
188 189 |
if (bootdelay >= 0) printf("Hit any key to stop autoboot: %2d ", bootdelay); |
c7de829c7 * Patch by Thomas... |
190 |
#endif |
c609719b8 Initial revision |
191 192 |
#if defined CONFIG_ZERO_BOOTDELAY_CHECK |
8bde7f776 * Code cleanup: |
193 194 195 196 |
/* * Check if key already pressed * Don't check if bootdelay < 0 */ |
c609719b8 Initial revision |
197 198 199 |
if (bootdelay >= 0) { if (tstc()) { /* we got a key press */ (void) getc(); /* consume input */ |
4b9206ed5 * Patches by Thom... |
200 |
puts ("\b\b\b 0"); |
4ec5bd55e [PATCH] simplify ... |
201 |
abort = 1; /* don't auto boot */ |
c609719b8 Initial revision |
202 |
} |
8bde7f776 * Code cleanup: |
203 |
} |
c609719b8 Initial revision |
204 |
#endif |
f72da3406 Added config opti... |
205 |
while ((bootdelay > 0) && (!abort)) { |
c609719b8 Initial revision |
206 |
--bootdelay; |
b2f3e0ea3 console: USB: KBD... |
207 208 209 |
/* delay 1000 ms */ ts = get_timer(0); do { |
c609719b8 Initial revision |
210 211 212 |
if (tstc()) { /* we got a key press */ abort = 1; /* don't auto boot */ bootdelay = 0; /* no more delay */ |
c7de829c7 * Patch by Thomas... |
213 214 215 |
# ifdef CONFIG_MENUKEY menukey = getc(); # else |
c609719b8 Initial revision |
216 |
(void) getc(); /* consume input */ |
c7de829c7 * Patch by Thomas... |
217 |
# endif |
c609719b8 Initial revision |
218 219 |
break; } |
ada4d4009 [PATCH] simplify ... |
220 |
udelay(10000); |
b2f3e0ea3 console: USB: KBD... |
221 |
} while (!abort && get_timer(ts) < 1000); |
c609719b8 Initial revision |
222 |
|
ada4d4009 [PATCH] simplify ... |
223 |
printf("\b\b\b%2d ", bootdelay); |
c609719b8 Initial revision |
224 |
} |
ada4d4009 [PATCH] simplify ... |
225 226 |
putc(' '); |
c609719b8 Initial revision |
227 |
|
f72da3406 Added config opti... |
228 |
#ifdef CONFIG_SILENT_CONSOLE |
4ec5bd55e [PATCH] simplify ... |
229 230 |
if (abort) gd->flags &= ~GD_FLG_SILENT; |
f72da3406 Added config opti... |
231 |
#endif |
c609719b8 Initial revision |
232 233 234 |
return abort; } # endif /* CONFIG_AUTOBOOT_KEYED */ |
063ae006a main: Separate ou... |
235 236 237 238 239 240 241 242 243 |
static int abortboot(int bootdelay) { #ifdef CONFIG_AUTOBOOT_KEYED return abortboot_keyed(bootdelay); #else return abortboot_normal(bootdelay); #endif } |
d514544fe CONFIG_BOOTDELAY ... |
244 |
#endif /* CONFIG_BOOTDELAY */ |
c609719b8 Initial revision |
245 |
|
67e1ea26e fdt: Allow device... |
246 247 248 249 250 251 252 253 254 255 256 257 258 |
/* * 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 CONFIG_BOOTDELAY ... |
259 |
#if defined(CONFIG_BOOTDELAY) && defined(CONFIG_OF_CONTROL) |
67e1ea26e fdt: Allow device... |
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
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 fdt: Set kernaddr... |
296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
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 fdt: Allow device... |
310 |
#endif /* CONFIG_OF_CONTROL */ |
bc2b4c27d main: Move boot_d... |
311 312 |
#ifdef CONFIG_BOOTDELAY static void process_boot_delay(void) |
c609719b8 Initial revision |
313 |
{ |
bc2b4c27d main: Move boot_d... |
314 |
#ifdef CONFIG_OF_CONTROL |
224b72e63 fdt: Load boot co... |
315 316 |
char *env; #endif |
c609719b8 Initial revision |
317 318 |
char *s; int bootdelay; |
bdccc4fed * Map ISP1362 USB... |
319 320 321 |
#ifdef CONFIG_BOOTCOUNT_LIMIT unsigned long bootcount = 0; unsigned long bootlimit = 0; |
bdccc4fed * Map ISP1362 USB... |
322 |
#endif /* CONFIG_BOOTCOUNT_LIMIT */ |
c609719b8 Initial revision |
323 |
|
bdccc4fed * Map ISP1362 USB... |
324 325 326 327 |
#ifdef CONFIG_BOOTCOUNT_LIMIT bootcount = bootcount_load(); bootcount++; bootcount_store (bootcount); |
f2abca845 main: Use get/set... |
328 329 |
setenv_ulong("bootcount", bootcount); bootlimit = getenv_ulong("bootlimit", 10, 0); |
bdccc4fed * Map ISP1362 USB... |
330 |
#endif /* CONFIG_BOOTCOUNT_LIMIT */ |
c609719b8 Initial revision |
331 332 |
s = getenv ("bootdelay"); bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
99bd544ee fdt: allow bootde... |
333 334 335 336 |
#ifdef CONFIG_OF_CONTROL bootdelay = fdtdec_get_config_int(gd->fdt_blob, "bootdelay", bootdelay); #endif |
a6c7ad2f6 * Fix startup pro... |
337 338 339 |
debug ("### main_loop entered: bootdelay=%d ", bootdelay); |
c609719b8 Initial revision |
340 |
|
317d6c57d common, menu: sho... |
341 342 343 |
#if defined(CONFIG_MENU_SHOW) bootdelay = menu_show(bootdelay); #endif |
c609719b8 Initial revision |
344 |
# ifdef CONFIG_BOOT_RETRY_TIME |
6dd652fa4 Patches by Murray... |
345 |
init_cmd_timeout (); |
c609719b8 Initial revision |
346 |
# endif /* CONFIG_BOOT_RETRY_TIME */ |
b428f6a8c The patch introdu... |
347 348 349 350 351 352 |
#ifdef CONFIG_POST if (gd->flags & GD_FLG_POSTFAIL) { s = getenv("failbootcmd"); } else #endif /* CONFIG_POST */ |
bdccc4fed * Map ISP1362 USB... |
353 354 355 356 |
#ifdef CONFIG_BOOTCOUNT_LIMIT if (bootlimit && (bootcount > bootlimit)) { printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd. ", |
93e145964 Coding Style clea... |
357 |
(unsigned)bootlimit); |
bdccc4fed * Map ISP1362 USB... |
358 359 360 361 362 |
s = getenv ("altbootcmd"); } else #endif /* CONFIG_BOOTCOUNT_LIMIT */ s = getenv ("bootcmd"); |
224b72e63 fdt: Load boot co... |
363 364 365 366 367 |
#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 fdt: Allow device... |
368 |
|
fcabc24f4 fdt: Set kernaddr... |
369 |
process_fdt_options(gd->fdt_blob); |
67e1ea26e fdt: Allow device... |
370 371 372 373 374 375 376 |
/* * 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 fdt: Load boot co... |
377 |
#endif /* CONFIG_OF_CONTROL */ |
a6c7ad2f6 * Fix startup pro... |
378 379 380 |
debug ("### main_loop: bootcmd=\"%s\" ", s ? s : "<UNDEFINED>"); |
93d7212fa Allow runtime con... |
381 |
if (bootdelay != -1 && s && !abortboot(bootdelay)) { |
00ddacc1a autoboot: add an ... |
382 |
#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC) |
c609719b8 Initial revision |
383 |
int prev = disable_ctrlc(1); /* disable Control C checking */ |
bc2b4c27d main: Move boot_d... |
384 |
#endif |
c609719b8 Initial revision |
385 |
|
3a8a02beb Allow newlines wi... |
386 |
run_command_list(s, -1, 0); |
c609719b8 Initial revision |
387 |
|
00ddacc1a autoboot: add an ... |
388 |
#if defined(CONFIG_AUTOBOOT_KEYED) && !defined(CONFIG_AUTOBOOT_KEYED_CTRLC) |
c609719b8 Initial revision |
389 |
disable_ctrlc(prev); /* restore Control C checking */ |
bc2b4c27d main: Move boot_d... |
390 |
#endif |
c609719b8 Initial revision |
391 |
} |
c7de829c7 * Patch by Thomas... |
392 |
|
bc2b4c27d main: Move boot_d... |
393 |
#ifdef CONFIG_MENUKEY |
a6c7ad2f6 * Fix startup pro... |
394 |
if (menukey == CONFIG_MENUKEY) { |
370d1e3e5 cosmetic, main: c... |
395 |
s = getenv("menucmd"); |
c8a2079e4 common: add run_c... |
396 |
if (s) |
3a8a02beb Allow newlines wi... |
397 |
run_command_list(s, -1, 0); |
c7de829c7 * Patch by Thomas... |
398 399 |
} #endif /* CONFIG_MENUKEY */ |
bc2b4c27d main: Move boot_d... |
400 |
} |
953b7e629 Remove AmigaOneG3... |
401 |
#endif /* CONFIG_BOOTDELAY */ |
c7de829c7 * Patch by Thomas... |
402 |
|
bc2b4c27d main: Move boot_d... |
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
void main_loop(void) { #ifndef CONFIG_SYS_HUSH_PARSER static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, }; int len; int rc = 1; int flag; #endif #ifdef CONFIG_PREBOOT char *p; #endif bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop"); #ifdef CONFIG_MODEM_SUPPORT debug("DEBUG: main_loop: do_mdm_init=%d ", do_mdm_init); if (do_mdm_init) { char *str = strdup(getenv("mdm_cmd")); setenv("preboot", str); /* set or delete definition */ if (str != NULL) free(str); mdm_init(); /* wait for modem connection */ } #endif /* CONFIG_MODEM_SUPPORT */ #ifdef CONFIG_VERSION_VARIABLE { setenv("ver", version_string); /* set version variable */ } #endif /* CONFIG_VERSION_VARIABLE */ #ifdef CONFIG_SYS_HUSH_PARSER u_boot_hush_start(); #endif #if defined(CONFIG_HUSH_INIT_VAR) hush_init_var(); #endif #ifdef CONFIG_PREBOOT p = getenv("preboot"); if (p != NULL) { # ifdef CONFIG_AUTOBOOT_KEYED int prev = disable_ctrlc(1); /* disable Control C checking */ # endif run_command_list(p, -1, 0); # ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc(prev); /* restore Control C checking */ # endif } #endif /* CONFIG_PREBOOT */ #if defined(CONFIG_UPDATE_TFTP) update_tftp(0UL); #endif /* CONFIG_UPDATE_TFTP */ #ifdef CONFIG_BOOTDELAY process_boot_delay(); #endif |
c609719b8 Initial revision |
465 466 467 |
/* * Main Loop for Monitor Command Processing */ |
6d0f6bcf3 rename CFG_ macro... |
468 |
#ifdef CONFIG_SYS_HUSH_PARSER |
c609719b8 Initial revision |
469 470 471 472 473 474 475 476 477 478 479 480 481 |
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 rename CFG_ macro... |
482 |
len = readline (CONFIG_SYS_PROMPT); |
c609719b8 Initial revision |
483 484 485 486 487 488 489 490 491 492 |
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 * Patches by Thom... |
493 494 495 |
puts (" Timed out waiting for command "); |
c609719b8 Initial revision |
496 497 498 499 500 501 502 503 504 505 |
# 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 * Patches by Thom... |
506 507 |
puts ("<INTERRUPT> "); |
c609719b8 Initial revision |
508 |
else |
530715323 Stop using builti... |
509 |
rc = run_command(lastcommand, flag); |
c609719b8 Initial revision |
510 511 512 513 514 515 |
if (rc <= 0) { /* invalid command or not repeatable, forget it */ lastcommand[0] = 0; } } |
6d0f6bcf3 rename CFG_ macro... |
516 |
#endif /*CONFIG_SYS_HUSH_PARSER*/ |
c609719b8 Initial revision |
517 |
} |
6dd652fa4 Patches by Murray... |
518 519 |
#ifdef CONFIG_BOOT_RETRY_TIME /*************************************************************************** |
19973b6ad Minor code cleanup. |
520 |
* initialize command line timeout |
6dd652fa4 Patches by Murray... |
521 522 523 524 525 526 |
*/ void init_cmd_timeout(void) { char *s = getenv ("bootretry"); if (s != NULL) |
b028f7151 * Patch by Yuli B... |
527 |
retry_time = (int)simple_strtol(s, NULL, 10); |
6dd652fa4 Patches by Murray... |
528 529 530 531 532 533 |
else retry_time = CONFIG_BOOT_RETRY_TIME; if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) retry_time = CONFIG_BOOT_RETRY_MIN; } |
c609719b8 Initial revision |
534 535 536 |
/*************************************************************************** * reset command line timeout to retry_time seconds */ |
c609719b8 Initial revision |
537 538 539 540 541 |
void reset_cmd_timeout(void) { endtime = endtick(retry_time); } #endif |
501090aaa Add readline cmdl... |
542 543 544 545 546 547 |
#ifdef CONFIG_CMDLINE_EDITING /* * cmdline-editing related codes from vivi. * Author: Janghoon Lyu <nandy@mizi.com> */ |
501090aaa Add readline cmdl... |
548 |
#define putnstr(str,n) do { \ |
dc4b0b38d Fix printf errors. |
549 |
printf ("%.*s", (int)n, str); \ |
501090aaa Add readline cmdl... |
550 |
} while (0) |
501090aaa Add readline cmdl... |
551 552 |
#define CTL_CH(c) ((c) - 'a' + 1) |
501090aaa Add readline cmdl... |
553 554 555 556 557 558 559 560 561 562 |
#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 Replace MAX_CMDBU... |
563 |
#define HIST_SIZE CONFIG_SYS_CBSIZE |
501090aaa Add readline cmdl... |
564 |
|
199adb601 common/misc: spar... |
565 566 |
static int hist_max; static int hist_add_idx; |
501090aaa Add readline cmdl... |
567 |
static int hist_cur = -1; |
199adb601 common/misc: spar... |
568 |
static unsigned hist_num; |
501090aaa Add readline cmdl... |
569 |
|
199adb601 common/misc: spar... |
570 571 |
static char *hist_list[HIST_MAX]; static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ |
501090aaa Add readline cmdl... |
572 573 574 575 576 577 578 579 580 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 |
#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 Fix CONFIG_CMDLIN... |
644 |
#ifndef CONFIG_CMDLINE_EDITING |
501090aaa Add readline cmdl... |
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
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 Fix CONFIG_CMDLIN... |
664 |
#endif /* CONFIG_CMDLINE_EDITING */ |
501090aaa Add readline cmdl... |
665 666 667 668 669 670 671 672 673 674 |
#define BEGINNING_OF_LINE() { \ while (num) { \ getcmd_putch(CTL_BACKSPACE); \ num--; \ } \ } #define ERASE_TO_EOL() { \ if (num < eol_num) { \ |
8faba4894 cmd editing: opti... |
675 676 |
printf("%*s", (int)(eol_num - num), ""); \ do { \ |
501090aaa Add readline cmdl... |
677 |
getcmd_putch(CTL_BACKSPACE); \ |
8faba4894 cmd editing: opti... |
678 |
} while (--eol_num > num); \ |
501090aaa Add readline cmdl... |
679 680 681 682 683 684 685 686 687 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 |
} \ } #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 common: add possi... |
733 734 |
static int cread_line(const char *const prompt, char *buf, unsigned int *len, int timeout) |
501090aaa Add readline cmdl... |
735 736 737 |
{ unsigned long num = 0; unsigned long eol_num = 0; |
501090aaa Add readline cmdl... |
738 739 740 741 |
unsigned long wlen; char ichar; int insert = 1; int esc_len = 0; |
501090aaa Add readline cmdl... |
742 |
char esc_save[8]; |
ecc5500ee readline(): Add a... |
743 |
int init_len = strlen(buf); |
9c3483113 common: add possi... |
744 |
int first = 1; |
ecc5500ee readline(): Add a... |
745 746 747 |
if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); |
501090aaa Add readline cmdl... |
748 749 |
while (1) { |
00ac50e34 Make bootretry wo... |
750 751 752 753 |
#ifdef CONFIG_BOOT_RETRY_TIME while (!tstc()) { /* while no incoming data */ if (retry_time >= 0 && get_ticks() > endtime) return (-2); /* timed out */ |
30dc165a7 FIX: watchdog tim... |
754 |
WATCHDOG_RESET(); |
00ac50e34 Make bootretry wo... |
755 756 |
} #endif |
9c3483113 common: add possi... |
757 758 759 760 761 762 763 764 765 766 |
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 Make bootretry wo... |
767 |
|
501090aaa Add readline cmdl... |
768 769 770 771 |
ichar = getcmd_getch(); if ((ichar == ' ') || (ichar == '\r')) { |
dd9f06f0d Minor code cleanup. |
772 773 |
putc(' '); |
501090aaa Add readline cmdl... |
774 775 776 777 778 779 780 781 782 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 |
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 Minor code cleanup. |
830 831 |
puts("impossible condition #876 "); |
501090aaa Add readline cmdl... |
832 833 834 835 836 837 838 839 840 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 |
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 Allow CONFIG_AUTO... |
879 |
case CTL_CH('u'): |
501090aaa Add readline cmdl... |
880 881 882 883 884 885 886 887 888 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 |
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 Allow CONFIG_AUTO... |
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
#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 Add readline cmdl... |
950 951 952 953 954 955 956 957 958 959 960 |
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 cread_line(): Rem... |
961 |
return 0; |
501090aaa Add readline cmdl... |
962 963 964 |
} #endif /* CONFIG_CMDLINE_EDITING */ |
c609719b8 Initial revision |
965 966 967 968 969 970 971 972 973 974 975 976 |
/****************************************************************************/ /* * 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 readline(): Add a... |
977 978 979 980 981 |
/* * 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 common: add possi... |
982 |
return readline_into_buffer(prompt, console_buffer, 0); |
6636b62a6 Expose parse_line... |
983 |
} |
9c3483113 common: add possi... |
984 |
int readline_into_buffer(const char *const prompt, char *buffer, int timeout) |
6636b62a6 Expose parse_line... |
985 986 |
{ char *p = buffer; |
501090aaa Add readline cmdl... |
987 |
#ifdef CONFIG_CMDLINE_EDITING |
8804ae3b7 Replace MAX_CMDBU... |
988 |
unsigned int len = CONFIG_SYS_CBSIZE; |
d8f961bbd Fix control-c han... |
989 |
int rc; |
501090aaa Add readline cmdl... |
990 |
static int initted = 0; |
597f6c26a Fix readline_into... |
991 992 993 994 995 996 997 998 999 1000 1001 |
/* * 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 Check for NULL pr... |
1002 1003 |
if (prompt) puts (prompt); |
597f6c26a Fix readline_into... |
1004 |
|
9c3483113 common: add possi... |
1005 |
rc = cread_line(prompt, p, &len, timeout); |
597f6c26a Fix readline_into... |
1006 1007 1008 1009 |
return rc < 0 ? rc : len; } else { #endif /* CONFIG_CMDLINE_EDITING */ |
0ec595243 Fix compiler warning |
1010 |
char * p_buf = p; |
c609719b8 Initial revision |
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
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 FIX: watchdog tim... |
1028 |
WATCHDOG_RESET(); |
c609719b8 Initial revision |
1029 1030 1031 1032 1033 1034 |
} #endif WATCHDOG_RESET(); /* Trigger watchdog, if needed */ #ifdef CONFIG_SHOW_ACTIVITY while (!tstc()) { |
c609719b8 Initial revision |
1035 |
show_activity(0); |
30dc165a7 FIX: watchdog tim... |
1036 |
WATCHDOG_RESET(); |
c609719b8 Initial revision |
1037 1038 1039 1040 1041 1042 1043 1044 |
} #endif c = getc(); /* * Special character handling */ switch (c) { |
4933381a5 main: Fix typos a... |
1045 |
case '\r': /* Enter */ |
c609719b8 Initial revision |
1046 1047 1048 1049 1050 |
case ' ': *p = '\0'; puts ("\r "); |
4933381a5 main: Fix typos a... |
1051 |
return p - p_buf; |
c609719b8 Initial revision |
1052 |
|
4933381a5 main: Fix typos a... |
1053 |
case '\0': /* nul */ |
27aa81867 * Patch by Carl R... |
1054 |
continue; |
4933381a5 main: Fix typos a... |
1055 |
case 0x03: /* ^C - break */ |
6636b62a6 Expose parse_line... |
1056 |
p_buf[0] = '\0'; /* discard input */ |
4933381a5 main: Fix typos a... |
1057 |
return -1; |
c609719b8 Initial revision |
1058 |
|
4933381a5 main: Fix typos a... |
1059 |
case 0x15: /* ^U - erase line */ |
c609719b8 Initial revision |
1060 1061 1062 1063 |
while (col > plen) { puts (erase_seq); --col; } |
6636b62a6 Expose parse_line... |
1064 |
p = p_buf; |
c609719b8 Initial revision |
1065 1066 |
n = 0; continue; |
4933381a5 main: Fix typos a... |
1067 |
case 0x17: /* ^W - erase word */ |
6636b62a6 Expose parse_line... |
1068 |
p=delete_char(p_buf, p, &col, &n, plen); |
c609719b8 Initial revision |
1069 |
while ((n > 0) && (*p != ' ')) { |
6636b62a6 Expose parse_line... |
1070 |
p=delete_char(p_buf, p, &col, &n, plen); |
c609719b8 Initial revision |
1071 1072 |
} continue; |
4933381a5 main: Fix typos a... |
1073 1074 |
case 0x08: /* ^H - backspace */ case 0x7F: /* DEL - backspace */ |
6636b62a6 Expose parse_line... |
1075 |
p=delete_char(p_buf, p, &col, &n, plen); |
c609719b8 Initial revision |
1076 1077 1078 1079 1080 1081 |
continue; default: /* * Must be a normal character then */ |
6d0f6bcf3 rename CFG_ macro... |
1082 |
if (n < CONFIG_SYS_CBSIZE-2) { |
4933381a5 main: Fix typos a... |
1083 |
if (c == '\t') { /* expand TABs */ |
04a85b3b3 * Patches by Pant... |
1084 1085 1086 1087 |
#ifdef CONFIG_AUTO_COMPLETE /* if auto completion triggered just continue */ *p = '\0'; if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { |
6636b62a6 Expose parse_line... |
1088 |
p = p_buf + n; /* reset */ |
04a85b3b3 * Patches by Pant... |
1089 1090 1091 |
continue; } #endif |
c609719b8 Initial revision |
1092 1093 1094 |
puts (tab_seq+(col&07)); col += 8 - (col&07); } else { |
9a8efc460 lcd: Add support ... |
1095 1096 1097 |
char buf[2]; /* |
4933381a5 main: Fix typos a... |
1098 |
* Echo input using puts() to force an |
9a8efc460 lcd: Add support ... |
1099 1100 1101 1102 1103 1104 |
* LCD flush if we are using an LCD */ ++col; buf[0] = c; buf[1] = '\0'; puts(buf); |
c609719b8 Initial revision |
1105 1106 1107 1108 1109 1110 1111 1112 |
} *p++ = c; ++n; } else { /* Buffer full */ putc ('\a'); } } } |
597f6c26a Fix readline_into... |
1113 1114 1115 |
#ifdef CONFIG_CMDLINE_EDITING } #endif |
c609719b8 Initial revision |
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 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 |
} /****************************************************************************/ 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; |
3e4088737 main: Add debug_p... |
1155 1156 |
debug_parser("parse_line: \"%s\" ", line); |
6d0f6bcf3 rename CFG_ macro... |
1157 |
while (nargs < CONFIG_SYS_MAXARGS) { |
c609719b8 Initial revision |
1158 1159 |
/* skip any white space */ |
4d91a6eca Replace space and... |
1160 |
while (isblank(*line)) |
c609719b8 Initial revision |
1161 |
++line; |
c609719b8 Initial revision |
1162 1163 1164 |
if (*line == '\0') { /* end of line, no more args */ argv[nargs] = NULL; |
3e4088737 main: Add debug_p... |
1165 1166 1167 |
debug_parser("parse_line: nargs=%d ", nargs); return nargs; |
c609719b8 Initial revision |
1168 1169 1170 1171 1172 |
} argv[nargs++] = line; /* begin of argument string */ /* find end of string */ |
4d91a6eca Replace space and... |
1173 |
while (*line && !isblank(*line)) |
c609719b8 Initial revision |
1174 |
++line; |
c609719b8 Initial revision |
1175 1176 1177 |
if (*line == '\0') { /* end of line, no more args */ argv[nargs] = NULL; |
3e4088737 main: Add debug_p... |
1178 1179 1180 |
debug_parser("parse_line: nargs=%d ", nargs); return nargs; |
c609719b8 Initial revision |
1181 1182 1183 1184 |
} *line++ = '\0'; /* terminate current arg */ } |
6d0f6bcf3 rename CFG_ macro... |
1185 1186 |
printf ("** Too many args (max. %d) ** ", CONFIG_SYS_MAXARGS); |
c609719b8 Initial revision |
1187 |
|
3e4088737 main: Add debug_p... |
1188 1189 |
debug_parser("parse_line: nargs=%d ", nargs); |
c609719b8 Initial revision |
1190 1191 1192 1193 |
return (nargs); } /****************************************************************************/ |
7fed89e00 Don't include sta... |
1194 |
#ifndef CONFIG_SYS_HUSH_PARSER |
c609719b8 Initial revision |
1195 1196 1197 1198 |
static void process_macros (const char *input, char *output) { char c, prev; const char *varname_start = NULL; |
19973b6ad Minor code cleanup. |
1199 |
int inputcnt = strlen (input); |
6d0f6bcf3 rename CFG_ macro... |
1200 |
int outputcnt = CONFIG_SYS_CBSIZE; |
19973b6ad Minor code cleanup. |
1201 1202 1203 1204 1205 |
int state = 0; /* 0 = waiting for '$' */ /* 1 = waiting for '(' or '{' */ /* 2 = waiting for ')' or '}' */ /* 3 = waiting for ''' */ |
c609719b8 Initial revision |
1206 |
char *output_start = output; |
3e4088737 main: Add debug_p... |
1207 1208 1209 |
debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\" ", strlen(input), input); |
c609719b8 Initial revision |
1210 |
|
19973b6ad Minor code cleanup. |
1211 |
prev = '\0'; /* previous character */ |
c609719b8 Initial revision |
1212 1213 |
while (inputcnt && outputcnt) { |
8bde7f776 * Code cleanup: |
1214 |
c = *input++; |
19973b6ad Minor code cleanup. |
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 |
inputcnt--; if (state != 3) { /* remove one level of escape characters */ if ((c == '\\') && (prev != '\\')) { if (inputcnt-- == 0) break; prev = c; c = *input++; } |
c609719b8 Initial revision |
1225 |
} |
19973b6ad Minor code cleanup. |
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 |
switch (state) { case 0: /* Waiting for (unescaped) $ */ if ((c == '\'') && (prev != '\\')) { state = 3; break; } if ((c == '$') && (prev != '\\')) { state++; } else { |
c609719b8 Initial revision |
1236 1237 1238 |
*(output++) = c; outputcnt--; } |
19973b6ad Minor code cleanup. |
1239 1240 1241 1242 1243 1244 1245 1246 1247 |
break; case 1: /* Waiting for ( */ if (c == '(' || c == '{') { state++; varname_start = input; } else { state = 0; *(output++) = '$'; outputcnt--; |
c609719b8 Initial revision |
1248 |
|
19973b6ad Minor code cleanup. |
1249 1250 |
if (outputcnt) { *(output++) = c; |
c609719b8 Initial revision |
1251 1252 |
outputcnt--; } |
19973b6ad Minor code cleanup. |
1253 1254 1255 1256 1257 |
} break; case 2: /* Waiting for ) */ if (c == ')' || c == '}') { int i; |
6d0f6bcf3 rename CFG_ macro... |
1258 |
char envname[CONFIG_SYS_CBSIZE], *envval; |
19973b6ad Minor code cleanup. |
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 |
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 Patch by Detlev Z... |
1288 |
} |
19973b6ad Minor code cleanup. |
1289 |
prev = c; |
c609719b8 Initial revision |
1290 1291 1292 1293 |
} if (outputcnt) *output = 0; |
9160b96f7 Fix: Add missing ... |
1294 1295 |
else *(output - 1) = 0; |
c609719b8 Initial revision |
1296 |
|
3e4088737 main: Add debug_p... |
1297 1298 1299 |
debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\" ", strlen(output_start), output_start); |
c609719b8 Initial revision |
1300 1301 1302 1303 1304 1305 1306 1307 |
} /**************************************************************************** * 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 rename CFG_ macro... |
1308 |
* (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is |
c609719b8 Initial revision |
1309 1310 1311 1312 1313 1314 1315 1316 1317 |
* 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 Stop using builti... |
1318 |
static int builtin_run_command(const char *cmd, int flag) |
c609719b8 Initial revision |
1319 |
{ |
6d0f6bcf3 rename CFG_ macro... |
1320 |
char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */ |
c609719b8 Initial revision |
1321 1322 |
char *token; /* start of token in cmdbuf */ char *sep; /* end of token (separator) in cmdbuf */ |
6d0f6bcf3 rename CFG_ macro... |
1323 |
char finaltoken[CONFIG_SYS_CBSIZE]; |
c609719b8 Initial revision |
1324 |
char *str = cmdbuf; |
6d0f6bcf3 rename CFG_ macro... |
1325 |
char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
f07771cc2 * Fix data abort ... |
1326 |
int argc, inquotes; |
c609719b8 Initial revision |
1327 |
int repeatable = 1; |
f07771cc2 * Fix data abort ... |
1328 |
int rc = 0; |
c609719b8 Initial revision |
1329 |
|
3e4088737 main: Add debug_p... |
1330 1331 1332 1333 1334 1335 1336 |
debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd); if (DEBUG_PARSER) { /* use puts - string may be loooong */ puts(cmd ? cmd : "NULL"); puts("\" "); } |
c609719b8 Initial revision |
1337 1338 1339 1340 1341 |
clear_ctrlc(); /* forget any previous Control C */ if (!cmd || !*cmd) { return -1; /* empty command */ } |
6d0f6bcf3 rename CFG_ macro... |
1342 |
if (strlen(cmd) >= CONFIG_SYS_CBSIZE) { |
c609719b8 Initial revision |
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 |
puts ("## Command too long! "); return -1; } strcpy (cmdbuf, cmd); /* Process separators and check for invalid * repeatable commands */ |
3e4088737 main: Add debug_p... |
1353 1354 |
debug_parser("[PROCESS_SEPARATORS] %s ", cmd); |
c609719b8 Initial revision |
1355 1356 1357 1358 1359 1360 |
while (*str) { /* * Find separator, or string end * Allow simple escape of ';' by writing "\;" */ |
a25f862ba Patch by Detlev Z... |
1361 1362 1363 1364 1365 1366 1367 |
for (inquotes = 0, sep = str; *sep; sep++) { if ((*sep=='\'') && (*(sep-1) != '\\')) inquotes=!inquotes; if (!inquotes && (*sep == ';') && /* separator */ |
c609719b8 Initial revision |
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 |
( 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 */ |
3e4088737 main: Add debug_p... |
1383 1384 |
debug_parser("token: \"%s\" ", token); |
c609719b8 Initial revision |
1385 1386 1387 1388 1389 |
/* find macros in this token and replace them */ process_macros (token, finaltoken); /* Extract arguments */ |
1264b4050 Avoid dereferenci... |
1390 1391 1392 1393 |
if ((argc = parse_line (finaltoken, argv)) == 0) { rc = -1; /* no command at all */ continue; } |
c609719b8 Initial revision |
1394 |
|
34765e885 cmd_time: merge r... |
1395 |
if (cmd_process(flag, argc, argv, &repeatable, NULL)) |
030fca522 Fix the behaviour... |
1396 |
rc = -1; |
c609719b8 Initial revision |
1397 1398 1399 |
/* Did the user stop this? */ if (had_ctrlc ()) |
5afb20209 Fix 'run' not to ... |
1400 |
return -1; /* if stopped then not repeatable */ |
c609719b8 Initial revision |
1401 |
} |
f07771cc2 * Fix data abort ... |
1402 |
return rc ? rc : repeatable; |
c609719b8 Initial revision |
1403 |
} |
7fed89e00 Don't include sta... |
1404 |
#endif |
c609719b8 Initial revision |
1405 |
|
530715323 Stop using builti... |
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 |
/* * 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 Add run_command_l... |
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 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 |
#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 Initial revision |
1518 |
/****************************************************************************/ |
c3517f919 common/* non-cmd*... |
1519 |
#if defined(CONFIG_CMD_RUN) |
54841ab50 Make sure that ar... |
1520 |
int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
c609719b8 Initial revision |
1521 1522 |
{ int i; |
c609719b8 Initial revision |
1523 |
|
47e26b1bf cmd_usage(): simp... |
1524 |
if (argc < 2) |
4c12eeb8b Convert cmd_usage... |
1525 |
return CMD_RET_USAGE; |
c609719b8 Initial revision |
1526 1527 |
for (i=1; i<argc; ++i) { |
3e38691e8 * Patch by Arun D... |
1528 1529 1530 1531 1532 1533 1534 |
char *arg; if ((arg = getenv (argv[i])) == NULL) { printf ("## Error: \"%s\" not defined ", argv[i]); return 1; } |
c8a2079e4 common: add run_c... |
1535 |
|
009dde195 Rename run_comman... |
1536 |
if (run_command(arg, flag) != 0) |
3e38691e8 * Patch by Arun D... |
1537 |
return 1; |
c609719b8 Initial revision |
1538 |
} |
3e38691e8 * Patch by Arun D... |
1539 |
return 0; |
c609719b8 Initial revision |
1540 |
} |
902531788 common/: Remove l... |
1541 |
#endif |