wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 5 | * Add to readline cmdline-editing by |
| 6 | * (C) Copyright 2005 |
| 7 | * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> |
| 8 | * |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
wdenk | 9dd2b88 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 28 | /* #define DEBUG */ |
| 29 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | #include <common.h> |
| 31 | #include <watchdog.h> |
| 32 | #include <command.h> |
Wolfgang Denk | b072379 | 2005-09-30 15:18:23 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_MODEM_SUPPORT |
| 34 | #include <malloc.h> /* for free() prototype */ |
| 35 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | |
| 37 | #ifdef CFG_HUSH_PARSER |
| 38 | #include <hush.h> |
| 39 | #endif |
| 40 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 41 | #include <post.h> |
| 42 | |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 43 | #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING) |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 44 | DECLARE_GLOBAL_DATA_PTR; |
| 45 | #endif |
| 46 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 47 | /* |
| 48 | * Board-specific Platform code can reimplement show_boot_progress () if needed |
| 49 | */ |
| 50 | void inline __show_boot_progress (int val) {} |
| 51 | void inline show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress"))); |
| 52 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY) |
| 54 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */ |
| 55 | #endif |
| 56 | |
| 57 | extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 58 | |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame^] | 59 | #if defined(CONFIG_UPDATE_TFTP) |
| 60 | void update_tftp (void); |
| 61 | #endif /* CONFIG_UPDATE_TFTP */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 62 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | #define MAX_DELAY_STOP_STR 32 |
| 64 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 66 | static int abortboot(int); |
| 67 | #endif |
| 68 | |
| 69 | #undef DEBUG_PARSER |
| 70 | |
| 71 | char console_buffer[CFG_CBSIZE]; /* console I/O buffer */ |
| 72 | |
Stefan Roese | 59ff5a5 | 2006-07-27 16:11:19 +0200 | [diff] [blame] | 73 | static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 74 | static char erase_seq[] = "\b \b"; /* erase sequence */ |
| 75 | static char tab_seq[] = " "; /* used to expand TABs */ |
| 76 | |
| 77 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 78 | static uint64_t endtime = 0; /* must be set, default is instant timeout */ |
| 79 | static int retry_time = -1; /* -1 so can call readline before main_loop */ |
| 80 | #endif |
| 81 | |
| 82 | #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) |
| 83 | |
| 84 | #ifndef CONFIG_BOOT_RETRY_MIN |
| 85 | #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME |
| 86 | #endif |
| 87 | |
| 88 | #ifdef CONFIG_MODEM_SUPPORT |
| 89 | int do_mdm_init = 0; |
| 90 | extern void mdm_init(void); /* defined in board.c */ |
| 91 | #endif |
| 92 | |
| 93 | /*************************************************************************** |
| 94 | * Watch for 'delay' seconds for autoboot stop or autoboot delay string. |
| 95 | * returns: 0 - no key string, allow autoboot |
| 96 | * 1 - got key string, abort |
| 97 | */ |
| 98 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 99 | # if defined(CONFIG_AUTOBOOT_KEYED) |
| 100 | static __inline__ int abortboot(int bootdelay) |
| 101 | { |
| 102 | int abort = 0; |
| 103 | uint64_t etime = endtick(bootdelay); |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 104 | struct { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | char* str; |
| 106 | u_int len; |
| 107 | int retry; |
| 108 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 109 | delaykey [] = { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | { str: getenv ("bootdelaykey"), retry: 1 }, |
| 111 | { str: getenv ("bootdelaykey2"), retry: 1 }, |
| 112 | { str: getenv ("bootstopkey"), retry: 0 }, |
| 113 | { str: getenv ("bootstopkey2"), retry: 0 }, |
| 114 | }; |
| 115 | |
| 116 | char presskey [MAX_DELAY_STOP_STR]; |
| 117 | u_int presskey_len = 0; |
| 118 | u_int presskey_max = 0; |
| 119 | u_int i; |
| 120 | |
| 121 | # ifdef CONFIG_AUTOBOOT_PROMPT |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 122 | printf(CONFIG_AUTOBOOT_PROMPT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | # endif |
| 124 | |
| 125 | # ifdef CONFIG_AUTOBOOT_DELAY_STR |
| 126 | if (delaykey[0].str == NULL) |
| 127 | delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR; |
| 128 | # endif |
| 129 | # ifdef CONFIG_AUTOBOOT_DELAY_STR2 |
| 130 | if (delaykey[1].str == NULL) |
| 131 | delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2; |
| 132 | # endif |
| 133 | # ifdef CONFIG_AUTOBOOT_STOP_STR |
| 134 | if (delaykey[2].str == NULL) |
| 135 | delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR; |
| 136 | # endif |
| 137 | # ifdef CONFIG_AUTOBOOT_STOP_STR2 |
| 138 | if (delaykey[3].str == NULL) |
| 139 | delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2; |
| 140 | # endif |
| 141 | |
| 142 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { |
| 143 | delaykey[i].len = delaykey[i].str == NULL ? |
| 144 | 0 : strlen (delaykey[i].str); |
| 145 | delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ? |
| 146 | MAX_DELAY_STOP_STR : delaykey[i].len; |
| 147 | |
| 148 | presskey_max = presskey_max > delaykey[i].len ? |
| 149 | presskey_max : delaykey[i].len; |
| 150 | |
| 151 | # if DEBUG_BOOTKEYS |
| 152 | printf("%s key:<%s>\n", |
| 153 | delaykey[i].retry ? "delay" : "stop", |
| 154 | delaykey[i].str ? delaykey[i].str : "NULL"); |
| 155 | # endif |
| 156 | } |
| 157 | |
| 158 | /* In order to keep up with incoming data, check timeout only |
| 159 | * when catch up. |
| 160 | */ |
| 161 | while (!abort && get_ticks() <= etime) { |
| 162 | for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) { |
| 163 | if (delaykey[i].len > 0 && |
| 164 | presskey_len >= delaykey[i].len && |
| 165 | memcmp (presskey + presskey_len - delaykey[i].len, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 166 | delaykey[i].str, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 167 | delaykey[i].len) == 0) { |
| 168 | # if DEBUG_BOOTKEYS |
| 169 | printf("got %skey\n", |
| 170 | delaykey[i].retry ? "delay" : "stop"); |
| 171 | # endif |
| 172 | |
| 173 | # ifdef CONFIG_BOOT_RETRY_TIME |
| 174 | /* don't retry auto boot */ |
| 175 | if (! delaykey[i].retry) |
| 176 | retry_time = -1; |
| 177 | # endif |
| 178 | abort = 1; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (tstc()) { |
| 183 | if (presskey_len < presskey_max) { |
| 184 | presskey [presskey_len ++] = getc(); |
| 185 | } |
| 186 | else { |
| 187 | for (i = 0; i < presskey_max - 1; i ++) |
| 188 | presskey [i] = presskey [i + 1]; |
| 189 | |
| 190 | presskey [i] = getc(); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | # if DEBUG_BOOTKEYS |
| 195 | if (!abort) |
Ladislav Michl | 83fc7e5 | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 196 | puts("key timeout\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | # endif |
| 198 | |
dzu | 6605c84 | 2003-10-24 13:14:45 +0000 | [diff] [blame] | 199 | #ifdef CONFIG_SILENT_CONSOLE |
Ladislav Michl | 6c6a38a | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 200 | if (abort) |
| 201 | gd->flags &= ~GD_FLG_SILENT; |
dzu | 6605c84 | 2003-10-24 13:14:45 +0000 | [diff] [blame] | 202 | #endif |
| 203 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | return abort; |
| 205 | } |
| 206 | |
| 207 | # else /* !defined(CONFIG_AUTOBOOT_KEYED) */ |
| 208 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 209 | #ifdef CONFIG_MENUKEY |
| 210 | static int menukey = 0; |
| 211 | #endif |
| 212 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 213 | static __inline__ int abortboot(int bootdelay) |
| 214 | { |
| 215 | int abort = 0; |
| 216 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 217 | #ifdef CONFIG_MENUPROMPT |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 218 | printf(CONFIG_MENUPROMPT); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 219 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | printf("Hit any key to stop autoboot: %2d ", bootdelay); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 221 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 222 | |
| 223 | #if defined CONFIG_ZERO_BOOTDELAY_CHECK |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 224 | /* |
| 225 | * Check if key already pressed |
| 226 | * Don't check if bootdelay < 0 |
| 227 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 228 | if (bootdelay >= 0) { |
| 229 | if (tstc()) { /* we got a key press */ |
| 230 | (void) getc(); /* consume input */ |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 231 | puts ("\b\b\b 0"); |
Ladislav Michl | 6c6a38a | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 232 | abort = 1; /* don't auto boot */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 234 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 235 | #endif |
| 236 | |
wdenk | 808532a | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 237 | while ((bootdelay > 0) && (!abort)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 238 | int i; |
| 239 | |
| 240 | --bootdelay; |
| 241 | /* delay 100 * 10ms */ |
| 242 | for (i=0; !abort && i<100; ++i) { |
| 243 | if (tstc()) { /* we got a key press */ |
| 244 | abort = 1; /* don't auto boot */ |
| 245 | bootdelay = 0; /* no more delay */ |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 246 | # ifdef CONFIG_MENUKEY |
| 247 | menukey = getc(); |
| 248 | # else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | (void) getc(); /* consume input */ |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 250 | # endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
Ladislav Michl | 83fc7e5 | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 253 | udelay(10000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Ladislav Michl | 83fc7e5 | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 256 | printf("\b\b\b%2d ", bootdelay); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Ladislav Michl | 83fc7e5 | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 259 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 260 | |
wdenk | 808532a | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 261 | #ifdef CONFIG_SILENT_CONSOLE |
Ladislav Michl | 6c6a38a | 2007-04-25 16:01:26 +0200 | [diff] [blame] | 262 | if (abort) |
| 263 | gd->flags &= ~GD_FLG_SILENT; |
wdenk | 808532a | 2003-10-10 10:05:42 +0000 | [diff] [blame] | 264 | #endif |
| 265 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 266 | return abort; |
| 267 | } |
| 268 | # endif /* CONFIG_AUTOBOOT_KEYED */ |
| 269 | #endif /* CONFIG_BOOTDELAY >= 0 */ |
| 270 | |
| 271 | /****************************************************************************/ |
| 272 | |
| 273 | void main_loop (void) |
| 274 | { |
| 275 | #ifndef CFG_HUSH_PARSER |
| 276 | static char lastcommand[CFG_CBSIZE] = { 0, }; |
| 277 | int len; |
| 278 | int rc = 1; |
| 279 | int flag; |
| 280 | #endif |
| 281 | |
| 282 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 283 | char *s; |
| 284 | int bootdelay; |
| 285 | #endif |
| 286 | #ifdef CONFIG_PREBOOT |
| 287 | char *p; |
| 288 | #endif |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 289 | #ifdef CONFIG_BOOTCOUNT_LIMIT |
| 290 | unsigned long bootcount = 0; |
| 291 | unsigned long bootlimit = 0; |
| 292 | char *bcs; |
| 293 | char bcs_set[16]; |
| 294 | #endif /* CONFIG_BOOTCOUNT_LIMIT */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 295 | |
| 296 | #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO) |
| 297 | ulong bmp = 0; /* default bitmap */ |
| 298 | extern int trab_vfd (ulong bitmap); |
| 299 | |
| 300 | #ifdef CONFIG_MODEM_SUPPORT |
| 301 | if (do_mdm_init) |
| 302 | bmp = 1; /* alternate bitmap */ |
| 303 | #endif |
| 304 | trab_vfd (bmp); |
| 305 | #endif /* CONFIG_VFD && VFD_TEST_LOGO */ |
| 306 | |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame^] | 307 | #if defined(CONFIG_UPDATE_TFTP) |
| 308 | update_tftp (); |
| 309 | #endif /* CONFIG_UPDATE_TFTP */ |
| 310 | |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 311 | #ifdef CONFIG_BOOTCOUNT_LIMIT |
| 312 | bootcount = bootcount_load(); |
| 313 | bootcount++; |
| 314 | bootcount_store (bootcount); |
| 315 | sprintf (bcs_set, "%lu", bootcount); |
| 316 | setenv ("bootcount", bcs_set); |
| 317 | bcs = getenv ("bootlimit"); |
| 318 | bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0; |
| 319 | #endif /* CONFIG_BOOTCOUNT_LIMIT */ |
| 320 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 321 | #ifdef CONFIG_MODEM_SUPPORT |
| 322 | debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init); |
| 323 | if (do_mdm_init) { |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 324 | char *str = strdup(getenv("mdm_cmd")); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 325 | setenv ("preboot", str); /* set or delete definition */ |
| 326 | if (str != NULL) |
| 327 | free (str); |
| 328 | mdm_init(); /* wait for modem connection */ |
| 329 | } |
| 330 | #endif /* CONFIG_MODEM_SUPPORT */ |
| 331 | |
stroese | f8105cf | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 332 | #ifdef CONFIG_VERSION_VARIABLE |
| 333 | { |
| 334 | extern char version_string[]; |
stroese | f8105cf | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 335 | |
stroese | b9ab27c | 2003-07-11 08:00:33 +0000 | [diff] [blame] | 336 | setenv ("ver", version_string); /* set version variable */ |
stroese | f8105cf | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 337 | } |
| 338 | #endif /* CONFIG_VERSION_VARIABLE */ |
| 339 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | #ifdef CFG_HUSH_PARSER |
| 341 | u_boot_hush_start (); |
| 342 | #endif |
| 343 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 344 | #ifdef CONFIG_AUTO_COMPLETE |
| 345 | install_auto_complete(); |
| 346 | #endif |
| 347 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 348 | #ifdef CONFIG_PREBOOT |
| 349 | if ((p = getenv ("preboot")) != NULL) { |
| 350 | # ifdef CONFIG_AUTOBOOT_KEYED |
| 351 | int prev = disable_ctrlc(1); /* disable Control C checking */ |
| 352 | # endif |
| 353 | |
| 354 | # ifndef CFG_HUSH_PARSER |
| 355 | run_command (p, 0); |
| 356 | # else |
| 357 | parse_string_outer(p, FLAG_PARSE_SEMICOLON | |
| 358 | FLAG_EXIT_FROM_LOOP); |
| 359 | # endif |
| 360 | |
| 361 | # ifdef CONFIG_AUTOBOOT_KEYED |
| 362 | disable_ctrlc(prev); /* restore Control C checking */ |
| 363 | # endif |
| 364 | } |
| 365 | #endif /* CONFIG_PREBOOT */ |
| 366 | |
| 367 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 368 | s = getenv ("bootdelay"); |
| 369 | bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; |
| 370 | |
wdenk | 9dd2b88 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 371 | debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 372 | |
| 373 | # ifdef CONFIG_BOOT_RETRY_TIME |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 374 | init_cmd_timeout (); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 375 | # endif /* CONFIG_BOOT_RETRY_TIME */ |
| 376 | |
Yuri Tikhonov | d773efb | 2008-02-04 14:11:03 +0100 | [diff] [blame] | 377 | #ifdef CONFIG_POST |
| 378 | if (gd->flags & GD_FLG_POSTFAIL) { |
| 379 | s = getenv("failbootcmd"); |
| 380 | } |
| 381 | else |
| 382 | #endif /* CONFIG_POST */ |
wdenk | 0a65855 | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 383 | #ifdef CONFIG_BOOTCOUNT_LIMIT |
| 384 | if (bootlimit && (bootcount > bootlimit)) { |
| 385 | printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n", |
| 386 | (unsigned)bootlimit); |
| 387 | s = getenv ("altbootcmd"); |
| 388 | } |
| 389 | else |
| 390 | #endif /* CONFIG_BOOTCOUNT_LIMIT */ |
| 391 | s = getenv ("bootcmd"); |
wdenk | 9dd2b88 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 392 | |
| 393 | debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>"); |
| 394 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 395 | if (bootdelay >= 0 && s && !abortboot (bootdelay)) { |
| 396 | # ifdef CONFIG_AUTOBOOT_KEYED |
| 397 | int prev = disable_ctrlc(1); /* disable Control C checking */ |
| 398 | # endif |
| 399 | |
| 400 | # ifndef CFG_HUSH_PARSER |
| 401 | run_command (s, 0); |
| 402 | # else |
| 403 | parse_string_outer(s, FLAG_PARSE_SEMICOLON | |
| 404 | FLAG_EXIT_FROM_LOOP); |
| 405 | # endif |
| 406 | |
| 407 | # ifdef CONFIG_AUTOBOOT_KEYED |
| 408 | disable_ctrlc(prev); /* restore Control C checking */ |
| 409 | # endif |
| 410 | } |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 411 | |
| 412 | # ifdef CONFIG_MENUKEY |
wdenk | 9dd2b88 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 413 | if (menukey == CONFIG_MENUKEY) { |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 414 | s = getenv("menucmd"); |
wdenk | 9dd2b88 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 415 | if (s) { |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 416 | # ifndef CFG_HUSH_PARSER |
wdenk | 91a4f36 | 2005-01-09 23:33:49 +0000 | [diff] [blame] | 417 | run_command (s, 0); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 418 | # else |
| 419 | parse_string_outer(s, FLAG_PARSE_SEMICOLON | |
| 420 | FLAG_EXIT_FROM_LOOP); |
| 421 | # endif |
| 422 | } |
| 423 | } |
| 424 | #endif /* CONFIG_MENUKEY */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 425 | #endif /* CONFIG_BOOTDELAY */ |
| 426 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 427 | #ifdef CONFIG_AMIGAONEG3SE |
| 428 | { |
| 429 | extern void video_banner(void); |
| 430 | video_banner(); |
| 431 | } |
| 432 | #endif |
| 433 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 434 | /* |
| 435 | * Main Loop for Monitor Command Processing |
| 436 | */ |
| 437 | #ifdef CFG_HUSH_PARSER |
| 438 | parse_file_outer(); |
| 439 | /* This point is never reached */ |
| 440 | for (;;); |
| 441 | #else |
| 442 | for (;;) { |
| 443 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 444 | if (rc >= 0) { |
| 445 | /* Saw enough of a valid command to |
| 446 | * restart the timeout. |
| 447 | */ |
| 448 | reset_cmd_timeout(); |
| 449 | } |
| 450 | #endif |
| 451 | len = readline (CFG_PROMPT); |
| 452 | |
| 453 | flag = 0; /* assume no special flags for now */ |
| 454 | if (len > 0) |
| 455 | strcpy (lastcommand, console_buffer); |
| 456 | else if (len == 0) |
| 457 | flag |= CMD_FLAG_REPEAT; |
| 458 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 459 | else if (len == -2) { |
| 460 | /* -2 means timed out, retry autoboot |
| 461 | */ |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 462 | puts ("\nTimed out waiting for command\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 463 | # ifdef CONFIG_RESET_TO_RETRY |
| 464 | /* Reinit board to run initialization code again */ |
| 465 | do_reset (NULL, 0, 0, NULL); |
| 466 | # else |
| 467 | return; /* retry autoboot */ |
| 468 | # endif |
| 469 | } |
| 470 | #endif |
| 471 | |
| 472 | if (len == -1) |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 473 | puts ("<INTERRUPT>\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 474 | else |
| 475 | rc = run_command (lastcommand, flag); |
| 476 | |
| 477 | if (rc <= 0) { |
| 478 | /* invalid command or not repeatable, forget it */ |
| 479 | lastcommand[0] = 0; |
| 480 | } |
| 481 | } |
| 482 | #endif /*CFG_HUSH_PARSER*/ |
| 483 | } |
| 484 | |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 485 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 486 | /*************************************************************************** |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 487 | * initialize command line timeout |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 488 | */ |
| 489 | void init_cmd_timeout(void) |
| 490 | { |
| 491 | char *s = getenv ("bootretry"); |
| 492 | |
| 493 | if (s != NULL) |
wdenk | 444f22b | 2003-12-07 21:39:28 +0000 | [diff] [blame] | 494 | retry_time = (int)simple_strtol(s, NULL, 10); |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 495 | else |
| 496 | retry_time = CONFIG_BOOT_RETRY_TIME; |
| 497 | |
| 498 | if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) |
| 499 | retry_time = CONFIG_BOOT_RETRY_MIN; |
| 500 | } |
| 501 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 502 | /*************************************************************************** |
| 503 | * reset command line timeout to retry_time seconds |
| 504 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 505 | void reset_cmd_timeout(void) |
| 506 | { |
| 507 | endtime = endtick(retry_time); |
| 508 | } |
| 509 | #endif |
| 510 | |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 511 | #ifdef CONFIG_CMDLINE_EDITING |
| 512 | |
| 513 | /* |
| 514 | * cmdline-editing related codes from vivi. |
| 515 | * Author: Janghoon Lyu <nandy@mizi.com> |
| 516 | */ |
| 517 | |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 518 | #define putnstr(str,n) do { \ |
Andrew Klossner | e4ad454 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 519 | printf ("%.*s", (int)n, str); \ |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 520 | } while (0) |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 521 | |
| 522 | #define CTL_CH(c) ((c) - 'a' + 1) |
| 523 | |
| 524 | #define MAX_CMDBUF_SIZE 256 |
| 525 | |
| 526 | #define CTL_BACKSPACE ('\b') |
| 527 | #define DEL ((char)255) |
| 528 | #define DEL7 ((char)127) |
| 529 | #define CREAD_HIST_CHAR ('!') |
| 530 | |
| 531 | #define getcmd_putch(ch) putc(ch) |
| 532 | #define getcmd_getch() getc() |
| 533 | #define getcmd_cbeep() getcmd_putch('\a') |
| 534 | |
| 535 | #define HIST_MAX 20 |
| 536 | #define HIST_SIZE MAX_CMDBUF_SIZE |
| 537 | |
| 538 | static int hist_max = 0; |
| 539 | static int hist_add_idx = 0; |
| 540 | static int hist_cur = -1; |
| 541 | unsigned hist_num = 0; |
| 542 | |
| 543 | char* hist_list[HIST_MAX]; |
| 544 | char hist_lines[HIST_MAX][HIST_SIZE]; |
| 545 | |
| 546 | #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) |
| 547 | |
| 548 | static void hist_init(void) |
| 549 | { |
| 550 | int i; |
| 551 | |
| 552 | hist_max = 0; |
| 553 | hist_add_idx = 0; |
| 554 | hist_cur = -1; |
| 555 | hist_num = 0; |
| 556 | |
| 557 | for (i = 0; i < HIST_MAX; i++) { |
| 558 | hist_list[i] = hist_lines[i]; |
| 559 | hist_list[i][0] = '\0'; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static void cread_add_to_hist(char *line) |
| 564 | { |
| 565 | strcpy(hist_list[hist_add_idx], line); |
| 566 | |
| 567 | if (++hist_add_idx >= HIST_MAX) |
| 568 | hist_add_idx = 0; |
| 569 | |
| 570 | if (hist_add_idx > hist_max) |
| 571 | hist_max = hist_add_idx; |
| 572 | |
| 573 | hist_num++; |
| 574 | } |
| 575 | |
| 576 | static char* hist_prev(void) |
| 577 | { |
| 578 | char *ret; |
| 579 | int old_cur; |
| 580 | |
| 581 | if (hist_cur < 0) |
| 582 | return NULL; |
| 583 | |
| 584 | old_cur = hist_cur; |
| 585 | if (--hist_cur < 0) |
| 586 | hist_cur = hist_max; |
| 587 | |
| 588 | if (hist_cur == hist_add_idx) { |
| 589 | hist_cur = old_cur; |
| 590 | ret = NULL; |
| 591 | } else |
| 592 | ret = hist_list[hist_cur]; |
| 593 | |
| 594 | return (ret); |
| 595 | } |
| 596 | |
| 597 | static char* hist_next(void) |
| 598 | { |
| 599 | char *ret; |
| 600 | |
| 601 | if (hist_cur < 0) |
| 602 | return NULL; |
| 603 | |
| 604 | if (hist_cur == hist_add_idx) |
| 605 | return NULL; |
| 606 | |
| 607 | if (++hist_cur > hist_max) |
| 608 | hist_cur = 0; |
| 609 | |
| 610 | if (hist_cur == hist_add_idx) { |
| 611 | ret = ""; |
| 612 | } else |
| 613 | ret = hist_list[hist_cur]; |
| 614 | |
| 615 | return (ret); |
| 616 | } |
| 617 | |
Stefan Roese | 59ff5a5 | 2006-07-27 16:11:19 +0200 | [diff] [blame] | 618 | #ifndef CONFIG_CMDLINE_EDITING |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 619 | static void cread_print_hist_list(void) |
| 620 | { |
| 621 | int i; |
| 622 | unsigned long n; |
| 623 | |
| 624 | n = hist_num - hist_max; |
| 625 | |
| 626 | i = hist_add_idx + 1; |
| 627 | while (1) { |
| 628 | if (i > hist_max) |
| 629 | i = 0; |
| 630 | if (i == hist_add_idx) |
| 631 | break; |
| 632 | printf("%s\n", hist_list[i]); |
| 633 | n++; |
| 634 | i++; |
| 635 | } |
| 636 | } |
Stefan Roese | 59ff5a5 | 2006-07-27 16:11:19 +0200 | [diff] [blame] | 637 | #endif /* CONFIG_CMDLINE_EDITING */ |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 638 | |
| 639 | #define BEGINNING_OF_LINE() { \ |
| 640 | while (num) { \ |
| 641 | getcmd_putch(CTL_BACKSPACE); \ |
| 642 | num--; \ |
| 643 | } \ |
| 644 | } |
| 645 | |
| 646 | #define ERASE_TO_EOL() { \ |
| 647 | if (num < eol_num) { \ |
| 648 | int tmp; \ |
| 649 | for (tmp = num; tmp < eol_num; tmp++) \ |
| 650 | getcmd_putch(' '); \ |
| 651 | while (tmp-- > num) \ |
| 652 | getcmd_putch(CTL_BACKSPACE); \ |
| 653 | eol_num = num; \ |
| 654 | } \ |
| 655 | } |
| 656 | |
| 657 | #define REFRESH_TO_EOL() { \ |
| 658 | if (num < eol_num) { \ |
| 659 | wlen = eol_num - num; \ |
| 660 | putnstr(buf + num, wlen); \ |
| 661 | num = eol_num; \ |
| 662 | } \ |
| 663 | } |
| 664 | |
| 665 | static void cread_add_char(char ichar, int insert, unsigned long *num, |
| 666 | unsigned long *eol_num, char *buf, unsigned long len) |
| 667 | { |
| 668 | unsigned long wlen; |
| 669 | |
| 670 | /* room ??? */ |
| 671 | if (insert || *num == *eol_num) { |
| 672 | if (*eol_num > len - 1) { |
| 673 | getcmd_cbeep(); |
| 674 | return; |
| 675 | } |
| 676 | (*eol_num)++; |
| 677 | } |
| 678 | |
| 679 | if (insert) { |
| 680 | wlen = *eol_num - *num; |
| 681 | if (wlen > 1) { |
| 682 | memmove(&buf[*num+1], &buf[*num], wlen-1); |
| 683 | } |
| 684 | |
| 685 | buf[*num] = ichar; |
| 686 | putnstr(buf + *num, wlen); |
| 687 | (*num)++; |
| 688 | while (--wlen) { |
| 689 | getcmd_putch(CTL_BACKSPACE); |
| 690 | } |
| 691 | } else { |
| 692 | /* echo the character */ |
| 693 | wlen = 1; |
| 694 | buf[*num] = ichar; |
| 695 | putnstr(buf + *num, wlen); |
| 696 | (*num)++; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static void cread_add_str(char *str, int strsize, int insert, unsigned long *num, |
| 701 | unsigned long *eol_num, char *buf, unsigned long len) |
| 702 | { |
| 703 | while (strsize--) { |
| 704 | cread_add_char(*str, insert, num, eol_num, buf, len); |
| 705 | str++; |
| 706 | } |
| 707 | } |
| 708 | |
Jean-Christophe PLAGNIOL-VILLARD | 6366f00 | 2007-12-22 15:52:58 +0100 | [diff] [blame] | 709 | static int cread_line(const char *const prompt, char *buf, unsigned int *len) |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 710 | { |
| 711 | unsigned long num = 0; |
| 712 | unsigned long eol_num = 0; |
| 713 | unsigned long rlen; |
| 714 | unsigned long wlen; |
| 715 | char ichar; |
| 716 | int insert = 1; |
| 717 | int esc_len = 0; |
| 718 | int rc = 0; |
| 719 | char esc_save[8]; |
| 720 | |
| 721 | while (1) { |
| 722 | rlen = 1; |
Andreas Engel | 3a0ba10 | 2008-01-09 17:10:56 +0100 | [diff] [blame] | 723 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 724 | while (!tstc()) { /* while no incoming data */ |
| 725 | if (retry_time >= 0 && get_ticks() > endtime) |
| 726 | return (-2); /* timed out */ |
| 727 | } |
| 728 | #endif |
| 729 | |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 730 | ichar = getcmd_getch(); |
| 731 | |
| 732 | if ((ichar == '\n') || (ichar == '\r')) { |
Wolfgang Denk | 92549de | 2006-07-21 11:34:34 +0200 | [diff] [blame] | 733 | putc('\n'); |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 734 | break; |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * handle standard linux xterm esc sequences for arrow key, etc. |
| 739 | */ |
| 740 | if (esc_len != 0) { |
| 741 | if (esc_len == 1) { |
| 742 | if (ichar == '[') { |
| 743 | esc_save[esc_len] = ichar; |
| 744 | esc_len = 2; |
| 745 | } else { |
| 746 | cread_add_str(esc_save, esc_len, insert, |
| 747 | &num, &eol_num, buf, *len); |
| 748 | esc_len = 0; |
| 749 | } |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | switch (ichar) { |
| 754 | |
| 755 | case 'D': /* <- key */ |
| 756 | ichar = CTL_CH('b'); |
| 757 | esc_len = 0; |
| 758 | break; |
| 759 | case 'C': /* -> key */ |
| 760 | ichar = CTL_CH('f'); |
| 761 | esc_len = 0; |
| 762 | break; /* pass off to ^F handler */ |
| 763 | case 'H': /* Home key */ |
| 764 | ichar = CTL_CH('a'); |
| 765 | esc_len = 0; |
| 766 | break; /* pass off to ^A handler */ |
| 767 | case 'A': /* up arrow */ |
| 768 | ichar = CTL_CH('p'); |
| 769 | esc_len = 0; |
| 770 | break; /* pass off to ^P handler */ |
| 771 | case 'B': /* down arrow */ |
| 772 | ichar = CTL_CH('n'); |
| 773 | esc_len = 0; |
| 774 | break; /* pass off to ^N handler */ |
| 775 | default: |
| 776 | esc_save[esc_len++] = ichar; |
| 777 | cread_add_str(esc_save, esc_len, insert, |
| 778 | &num, &eol_num, buf, *len); |
| 779 | esc_len = 0; |
| 780 | continue; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | switch (ichar) { |
| 785 | case 0x1b: |
| 786 | if (esc_len == 0) { |
| 787 | esc_save[esc_len] = ichar; |
| 788 | esc_len = 1; |
| 789 | } else { |
Wolfgang Denk | 92549de | 2006-07-21 11:34:34 +0200 | [diff] [blame] | 790 | puts("impossible condition #876\n"); |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 791 | esc_len = 0; |
| 792 | } |
| 793 | break; |
| 794 | |
| 795 | case CTL_CH('a'): |
| 796 | BEGINNING_OF_LINE(); |
| 797 | break; |
| 798 | case CTL_CH('c'): /* ^C - break */ |
| 799 | *buf = '\0'; /* discard input */ |
| 800 | return (-1); |
| 801 | case CTL_CH('f'): |
| 802 | if (num < eol_num) { |
| 803 | getcmd_putch(buf[num]); |
| 804 | num++; |
| 805 | } |
| 806 | break; |
| 807 | case CTL_CH('b'): |
| 808 | if (num) { |
| 809 | getcmd_putch(CTL_BACKSPACE); |
| 810 | num--; |
| 811 | } |
| 812 | break; |
| 813 | case CTL_CH('d'): |
| 814 | if (num < eol_num) { |
| 815 | wlen = eol_num - num - 1; |
| 816 | if (wlen) { |
| 817 | memmove(&buf[num], &buf[num+1], wlen); |
| 818 | putnstr(buf + num, wlen); |
| 819 | } |
| 820 | |
| 821 | getcmd_putch(' '); |
| 822 | do { |
| 823 | getcmd_putch(CTL_BACKSPACE); |
| 824 | } while (wlen--); |
| 825 | eol_num--; |
| 826 | } |
| 827 | break; |
| 828 | case CTL_CH('k'): |
| 829 | ERASE_TO_EOL(); |
| 830 | break; |
| 831 | case CTL_CH('e'): |
| 832 | REFRESH_TO_EOL(); |
| 833 | break; |
| 834 | case CTL_CH('o'): |
| 835 | insert = !insert; |
| 836 | break; |
| 837 | case CTL_CH('x'): |
Jean-Christophe PLAGNIOL-VILLARD | 6366f00 | 2007-12-22 15:52:58 +0100 | [diff] [blame] | 838 | case CTL_CH('u'): |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 839 | BEGINNING_OF_LINE(); |
| 840 | ERASE_TO_EOL(); |
| 841 | break; |
| 842 | case DEL: |
| 843 | case DEL7: |
| 844 | case 8: |
| 845 | if (num) { |
| 846 | wlen = eol_num - num; |
| 847 | num--; |
| 848 | memmove(&buf[num], &buf[num+1], wlen); |
| 849 | getcmd_putch(CTL_BACKSPACE); |
| 850 | putnstr(buf + num, wlen); |
| 851 | getcmd_putch(' '); |
| 852 | do { |
| 853 | getcmd_putch(CTL_BACKSPACE); |
| 854 | } while (wlen--); |
| 855 | eol_num--; |
| 856 | } |
| 857 | break; |
| 858 | case CTL_CH('p'): |
| 859 | case CTL_CH('n'): |
| 860 | { |
| 861 | char * hline; |
| 862 | |
| 863 | esc_len = 0; |
| 864 | |
| 865 | if (ichar == CTL_CH('p')) |
| 866 | hline = hist_prev(); |
| 867 | else |
| 868 | hline = hist_next(); |
| 869 | |
| 870 | if (!hline) { |
| 871 | getcmd_cbeep(); |
| 872 | continue; |
| 873 | } |
| 874 | |
| 875 | /* nuke the current line */ |
| 876 | /* first, go home */ |
| 877 | BEGINNING_OF_LINE(); |
| 878 | |
| 879 | /* erase to end of line */ |
| 880 | ERASE_TO_EOL(); |
| 881 | |
| 882 | /* copy new line into place and display */ |
| 883 | strcpy(buf, hline); |
| 884 | eol_num = strlen(buf); |
| 885 | REFRESH_TO_EOL(); |
| 886 | continue; |
| 887 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6366f00 | 2007-12-22 15:52:58 +0100 | [diff] [blame] | 888 | #ifdef CONFIG_AUTO_COMPLETE |
| 889 | case '\t': { |
| 890 | int num2, col; |
| 891 | |
| 892 | /* do not autocomplete when in the middle */ |
| 893 | if (num < eol_num) { |
| 894 | getcmd_cbeep(); |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | buf[num] = '\0'; |
| 899 | col = strlen(prompt) + eol_num; |
| 900 | num2 = num; |
| 901 | if (cmd_auto_complete(prompt, buf, &num2, &col)) { |
| 902 | col = num2 - num; |
| 903 | num += col; |
| 904 | eol_num += col; |
| 905 | } |
| 906 | break; |
| 907 | } |
| 908 | #endif |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 909 | default: |
| 910 | cread_add_char(ichar, insert, &num, &eol_num, buf, *len); |
| 911 | break; |
| 912 | } |
| 913 | } |
| 914 | *len = eol_num; |
| 915 | buf[eol_num] = '\0'; /* lose the newline */ |
| 916 | |
| 917 | if (buf[0] && buf[0] != CREAD_HIST_CHAR) |
| 918 | cread_add_to_hist(buf); |
| 919 | hist_cur = hist_add_idx; |
| 920 | |
| 921 | return (rc); |
| 922 | } |
| 923 | |
| 924 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 925 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 926 | /****************************************************************************/ |
| 927 | |
| 928 | /* |
| 929 | * Prompt for input and read a line. |
| 930 | * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, |
| 931 | * time out when time goes past endtime (timebase time in ticks). |
| 932 | * Return: number of read characters |
| 933 | * -1 if break |
| 934 | * -2 if timed out |
| 935 | */ |
| 936 | int readline (const char *const prompt) |
| 937 | { |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 938 | return readline_into_buffer(prompt, console_buffer); |
| 939 | } |
| 940 | |
| 941 | |
| 942 | int readline_into_buffer (const char *const prompt, char * buffer) |
| 943 | { |
| 944 | char *p = buffer; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 945 | #ifdef CONFIG_CMDLINE_EDITING |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 946 | unsigned int len=MAX_CMDBUF_SIZE; |
Stefan Roese | dc5ff70 | 2006-08-07 15:08:44 +0200 | [diff] [blame] | 947 | int rc; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 948 | static int initted = 0; |
| 949 | |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 950 | /* |
| 951 | * History uses a global array which is not |
| 952 | * writable until after relocation to RAM. |
| 953 | * Revert to non-history version if still |
| 954 | * running from flash. |
| 955 | */ |
| 956 | if (gd->flags & GD_FLG_RELOC) { |
| 957 | if (!initted) { |
| 958 | hist_init(); |
| 959 | initted = 1; |
| 960 | } |
| 961 | |
| 962 | puts (prompt); |
| 963 | |
| 964 | rc = cread_line(prompt, p, &len); |
| 965 | return rc < 0 ? rc : len; |
| 966 | |
| 967 | } else { |
| 968 | #endif /* CONFIG_CMDLINE_EDITING */ |
Kumar Gala | e225b17 | 2008-01-10 02:22:05 -0600 | [diff] [blame] | 969 | char * p_buf = p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 970 | int n = 0; /* buffer index */ |
| 971 | int plen = 0; /* prompt length */ |
| 972 | int col; /* output column cnt */ |
| 973 | char c; |
| 974 | |
| 975 | /* print prompt */ |
| 976 | if (prompt) { |
| 977 | plen = strlen (prompt); |
| 978 | puts (prompt); |
| 979 | } |
| 980 | col = plen; |
| 981 | |
| 982 | for (;;) { |
| 983 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 984 | while (!tstc()) { /* while no incoming data */ |
| 985 | if (retry_time >= 0 && get_ticks() > endtime) |
| 986 | return (-2); /* timed out */ |
| 987 | } |
| 988 | #endif |
| 989 | WATCHDOG_RESET(); /* Trigger watchdog, if needed */ |
| 990 | |
| 991 | #ifdef CONFIG_SHOW_ACTIVITY |
| 992 | while (!tstc()) { |
| 993 | extern void show_activity(int arg); |
| 994 | show_activity(0); |
| 995 | } |
| 996 | #endif |
| 997 | c = getc(); |
| 998 | |
| 999 | /* |
| 1000 | * Special character handling |
| 1001 | */ |
| 1002 | switch (c) { |
| 1003 | case '\r': /* Enter */ |
| 1004 | case '\n': |
| 1005 | *p = '\0'; |
| 1006 | puts ("\r\n"); |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1007 | return (p - p_buf); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1008 | |
wdenk | 8d4d1f6 | 2004-03-23 22:37:33 +0000 | [diff] [blame] | 1009 | case '\0': /* nul */ |
| 1010 | continue; |
| 1011 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1012 | case 0x03: /* ^C - break */ |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1013 | p_buf[0] = '\0'; /* discard input */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1014 | return (-1); |
| 1015 | |
| 1016 | case 0x15: /* ^U - erase line */ |
| 1017 | while (col > plen) { |
| 1018 | puts (erase_seq); |
| 1019 | --col; |
| 1020 | } |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1021 | p = p_buf; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1022 | n = 0; |
| 1023 | continue; |
| 1024 | |
Wolfgang Denk | 70df7bc | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 1025 | case 0x17: /* ^W - erase word */ |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1026 | p=delete_char(p_buf, p, &col, &n, plen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1027 | while ((n > 0) && (*p != ' ')) { |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1028 | p=delete_char(p_buf, p, &col, &n, plen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1029 | } |
| 1030 | continue; |
| 1031 | |
| 1032 | case 0x08: /* ^H - backspace */ |
| 1033 | case 0x7F: /* DEL - backspace */ |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1034 | p=delete_char(p_buf, p, &col, &n, plen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1035 | continue; |
| 1036 | |
| 1037 | default: |
| 1038 | /* |
| 1039 | * Must be a normal character then |
| 1040 | */ |
| 1041 | if (n < CFG_CBSIZE-2) { |
| 1042 | if (c == '\t') { /* expand TABs */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 1043 | #ifdef CONFIG_AUTO_COMPLETE |
| 1044 | /* if auto completion triggered just continue */ |
| 1045 | *p = '\0'; |
| 1046 | if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1047 | p = p_buf + n; /* reset */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 1048 | continue; |
| 1049 | } |
| 1050 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1051 | puts (tab_seq+(col&07)); |
| 1052 | col += 8 - (col&07); |
| 1053 | } else { |
| 1054 | ++col; /* echo input */ |
| 1055 | putc (c); |
| 1056 | } |
| 1057 | *p++ = c; |
| 1058 | ++n; |
| 1059 | } else { /* Buffer full */ |
| 1060 | putc ('\a'); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 1064 | #ifdef CONFIG_CMDLINE_EDITING |
| 1065 | } |
| 1066 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /****************************************************************************/ |
| 1070 | |
| 1071 | static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen) |
| 1072 | { |
| 1073 | char *s; |
| 1074 | |
| 1075 | if (*np == 0) { |
| 1076 | return (p); |
| 1077 | } |
| 1078 | |
| 1079 | if (*(--p) == '\t') { /* will retype the whole line */ |
| 1080 | while (*colp > plen) { |
| 1081 | puts (erase_seq); |
| 1082 | (*colp)--; |
| 1083 | } |
| 1084 | for (s=buffer; s<p; ++s) { |
| 1085 | if (*s == '\t') { |
| 1086 | puts (tab_seq+((*colp) & 07)); |
| 1087 | *colp += 8 - ((*colp) & 07); |
| 1088 | } else { |
| 1089 | ++(*colp); |
| 1090 | putc (*s); |
| 1091 | } |
| 1092 | } |
| 1093 | } else { |
| 1094 | puts (erase_seq); |
| 1095 | (*colp)--; |
| 1096 | } |
| 1097 | (*np)--; |
| 1098 | return (p); |
| 1099 | } |
| 1100 | |
| 1101 | /****************************************************************************/ |
| 1102 | |
| 1103 | int parse_line (char *line, char *argv[]) |
| 1104 | { |
| 1105 | int nargs = 0; |
| 1106 | |
| 1107 | #ifdef DEBUG_PARSER |
| 1108 | printf ("parse_line: \"%s\"\n", line); |
| 1109 | #endif |
| 1110 | while (nargs < CFG_MAXARGS) { |
| 1111 | |
| 1112 | /* skip any white space */ |
| 1113 | while ((*line == ' ') || (*line == '\t')) { |
| 1114 | ++line; |
| 1115 | } |
| 1116 | |
| 1117 | if (*line == '\0') { /* end of line, no more args */ |
| 1118 | argv[nargs] = NULL; |
| 1119 | #ifdef DEBUG_PARSER |
| 1120 | printf ("parse_line: nargs=%d\n", nargs); |
| 1121 | #endif |
| 1122 | return (nargs); |
| 1123 | } |
| 1124 | |
| 1125 | argv[nargs++] = line; /* begin of argument string */ |
| 1126 | |
| 1127 | /* find end of string */ |
| 1128 | while (*line && (*line != ' ') && (*line != '\t')) { |
| 1129 | ++line; |
| 1130 | } |
| 1131 | |
| 1132 | if (*line == '\0') { /* end of line, no more args */ |
| 1133 | argv[nargs] = NULL; |
| 1134 | #ifdef DEBUG_PARSER |
| 1135 | printf ("parse_line: nargs=%d\n", nargs); |
| 1136 | #endif |
| 1137 | return (nargs); |
| 1138 | } |
| 1139 | |
| 1140 | *line++ = '\0'; /* terminate current arg */ |
| 1141 | } |
| 1142 | |
| 1143 | printf ("** Too many args (max. %d) **\n", CFG_MAXARGS); |
| 1144 | |
| 1145 | #ifdef DEBUG_PARSER |
| 1146 | printf ("parse_line: nargs=%d\n", nargs); |
| 1147 | #endif |
| 1148 | return (nargs); |
| 1149 | } |
| 1150 | |
| 1151 | /****************************************************************************/ |
| 1152 | |
| 1153 | static void process_macros (const char *input, char *output) |
| 1154 | { |
| 1155 | char c, prev; |
| 1156 | const char *varname_start = NULL; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1157 | int inputcnt = strlen (input); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1158 | int outputcnt = CFG_CBSIZE; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1159 | int state = 0; /* 0 = waiting for '$' */ |
| 1160 | |
| 1161 | /* 1 = waiting for '(' or '{' */ |
| 1162 | /* 2 = waiting for ')' or '}' */ |
| 1163 | /* 3 = waiting for ''' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1164 | #ifdef DEBUG_PARSER |
| 1165 | char *output_start = output; |
| 1166 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1167 | printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input), |
| 1168 | input); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1169 | #endif |
| 1170 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1171 | prev = '\0'; /* previous character */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1172 | |
| 1173 | while (inputcnt && outputcnt) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1174 | c = *input++; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1175 | inputcnt--; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1176 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1177 | if (state != 3) { |
| 1178 | /* remove one level of escape characters */ |
| 1179 | if ((c == '\\') && (prev != '\\')) { |
| 1180 | if (inputcnt-- == 0) |
| 1181 | break; |
| 1182 | prev = c; |
| 1183 | c = *input++; |
| 1184 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1185 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1186 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1187 | switch (state) { |
| 1188 | case 0: /* Waiting for (unescaped) $ */ |
| 1189 | if ((c == '\'') && (prev != '\\')) { |
| 1190 | state = 3; |
| 1191 | break; |
| 1192 | } |
| 1193 | if ((c == '$') && (prev != '\\')) { |
| 1194 | state++; |
| 1195 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1196 | *(output++) = c; |
| 1197 | outputcnt--; |
| 1198 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1199 | break; |
| 1200 | case 1: /* Waiting for ( */ |
| 1201 | if (c == '(' || c == '{') { |
| 1202 | state++; |
| 1203 | varname_start = input; |
| 1204 | } else { |
| 1205 | state = 0; |
| 1206 | *(output++) = '$'; |
| 1207 | outputcnt--; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1208 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1209 | if (outputcnt) { |
| 1210 | *(output++) = c; |
| 1211 | outputcnt--; |
| 1212 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1213 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1214 | break; |
| 1215 | case 2: /* Waiting for ) */ |
| 1216 | if (c == ')' || c == '}') { |
| 1217 | int i; |
| 1218 | char envname[CFG_CBSIZE], *envval; |
| 1219 | int envcnt = input - varname_start - 1; /* Varname # of chars */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1220 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1221 | /* Get the varname */ |
| 1222 | for (i = 0; i < envcnt; i++) { |
| 1223 | envname[i] = varname_start[i]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1224 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1225 | envname[i] = 0; |
| 1226 | |
| 1227 | /* Get its value */ |
| 1228 | envval = getenv (envname); |
| 1229 | |
| 1230 | /* Copy into the line if it exists */ |
| 1231 | if (envval != NULL) |
| 1232 | while ((*envval) && outputcnt) { |
| 1233 | *(output++) = *(envval++); |
| 1234 | outputcnt--; |
| 1235 | } |
| 1236 | /* Look for another '$' */ |
| 1237 | state = 0; |
| 1238 | } |
| 1239 | break; |
| 1240 | case 3: /* Waiting for ' */ |
| 1241 | if ((c == '\'') && (prev != '\\')) { |
| 1242 | state = 0; |
| 1243 | } else { |
| 1244 | *(output++) = c; |
| 1245 | outputcnt--; |
| 1246 | } |
| 1247 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1248 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1249 | prev = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | if (outputcnt) |
| 1253 | *output = 0; |
Bartlomiej Sieka | cadc8e8 | 2007-05-27 17:04:18 +0200 | [diff] [blame] | 1254 | else |
| 1255 | *(output - 1) = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1256 | |
| 1257 | #ifdef DEBUG_PARSER |
| 1258 | printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n", |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1259 | strlen (output_start), output_start); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1260 | #endif |
| 1261 | } |
| 1262 | |
| 1263 | /**************************************************************************** |
| 1264 | * returns: |
| 1265 | * 1 - command executed, repeatable |
| 1266 | * 0 - command executed but not repeatable, interrupted commands are |
| 1267 | * always considered not repeatable |
| 1268 | * -1 - not executed (unrecognized, bootd recursion or too many args) |
| 1269 | * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is |
| 1270 | * considered unrecognized) |
| 1271 | * |
| 1272 | * WARNING: |
| 1273 | * |
| 1274 | * We must create a temporary copy of the command since the command we get |
| 1275 | * may be the result from getenv(), which returns a pointer directly to |
| 1276 | * the environment data, which may change magicly when the command we run |
| 1277 | * creates or modifies environment variables (like "bootp" does). |
| 1278 | */ |
| 1279 | |
| 1280 | int run_command (const char *cmd, int flag) |
| 1281 | { |
| 1282 | cmd_tbl_t *cmdtp; |
| 1283 | char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */ |
| 1284 | char *token; /* start of token in cmdbuf */ |
| 1285 | char *sep; /* end of token (separator) in cmdbuf */ |
| 1286 | char finaltoken[CFG_CBSIZE]; |
| 1287 | char *str = cmdbuf; |
| 1288 | char *argv[CFG_MAXARGS + 1]; /* NULL terminated */ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1289 | int argc, inquotes; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1290 | int repeatable = 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1291 | int rc = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1292 | |
| 1293 | #ifdef DEBUG_PARSER |
| 1294 | printf ("[RUN_COMMAND] cmd[%p]=\"", cmd); |
| 1295 | puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */ |
| 1296 | puts ("\"\n"); |
| 1297 | #endif |
| 1298 | |
| 1299 | clear_ctrlc(); /* forget any previous Control C */ |
| 1300 | |
| 1301 | if (!cmd || !*cmd) { |
| 1302 | return -1; /* empty command */ |
| 1303 | } |
| 1304 | |
| 1305 | if (strlen(cmd) >= CFG_CBSIZE) { |
| 1306 | puts ("## Command too long!\n"); |
| 1307 | return -1; |
| 1308 | } |
| 1309 | |
| 1310 | strcpy (cmdbuf, cmd); |
| 1311 | |
| 1312 | /* Process separators and check for invalid |
| 1313 | * repeatable commands |
| 1314 | */ |
| 1315 | |
| 1316 | #ifdef DEBUG_PARSER |
| 1317 | printf ("[PROCESS_SEPARATORS] %s\n", cmd); |
| 1318 | #endif |
| 1319 | while (*str) { |
| 1320 | |
| 1321 | /* |
| 1322 | * Find separator, or string end |
| 1323 | * Allow simple escape of ';' by writing "\;" |
| 1324 | */ |
wdenk | 654868c | 2003-01-02 23:57:29 +0000 | [diff] [blame] | 1325 | for (inquotes = 0, sep = str; *sep; sep++) { |
| 1326 | if ((*sep=='\'') && |
| 1327 | (*(sep-1) != '\\')) |
| 1328 | inquotes=!inquotes; |
| 1329 | |
| 1330 | if (!inquotes && |
| 1331 | (*sep == ';') && /* separator */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1332 | ( sep != str) && /* past string start */ |
| 1333 | (*(sep-1) != '\\')) /* and NOT escaped */ |
| 1334 | break; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Limit the token to data between separators |
| 1339 | */ |
| 1340 | token = str; |
| 1341 | if (*sep) { |
| 1342 | str = sep + 1; /* start of command for next pass */ |
| 1343 | *sep = '\0'; |
| 1344 | } |
| 1345 | else |
| 1346 | str = sep; /* no more commands for next pass */ |
| 1347 | #ifdef DEBUG_PARSER |
| 1348 | printf ("token: \"%s\"\n", token); |
| 1349 | #endif |
| 1350 | |
| 1351 | /* find macros in this token and replace them */ |
| 1352 | process_macros (token, finaltoken); |
| 1353 | |
| 1354 | /* Extract arguments */ |
Wolfgang Denk | d44e7bb | 2006-03-12 02:20:55 +0100 | [diff] [blame] | 1355 | if ((argc = parse_line (finaltoken, argv)) == 0) { |
| 1356 | rc = -1; /* no command at all */ |
| 1357 | continue; |
| 1358 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1359 | |
| 1360 | /* Look up command in command table */ |
| 1361 | if ((cmdtp = find_cmd(argv[0])) == NULL) { |
| 1362 | printf ("Unknown command '%s' - try 'help'\n", argv[0]); |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1363 | rc = -1; /* give up after bad command */ |
| 1364 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /* found - check max args */ |
| 1368 | if (argc > cmdtp->maxargs) { |
| 1369 | printf ("Usage:\n%s\n", cmdtp->usage); |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1370 | rc = -1; |
| 1371 | continue; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Jon Loeliger | 052fc84 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 1374 | #if defined(CONFIG_CMD_BOOTD) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1375 | /* avoid "bootd" recursion */ |
| 1376 | if (cmdtp->cmd == do_bootd) { |
| 1377 | #ifdef DEBUG_PARSER |
| 1378 | printf ("[%s]\n", finaltoken); |
| 1379 | #endif |
| 1380 | if (flag & CMD_FLAG_BOOTD) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1381 | puts ("'bootd' recursion detected\n"); |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1382 | rc = -1; |
| 1383 | continue; |
Wolfgang Denk | d44e7bb | 2006-03-12 02:20:55 +0100 | [diff] [blame] | 1384 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1385 | flag |= CMD_FLAG_BOOTD; |
Wolfgang Denk | d44e7bb | 2006-03-12 02:20:55 +0100 | [diff] [blame] | 1386 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1387 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1388 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1389 | |
| 1390 | /* OK - call function to do the command */ |
| 1391 | if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) { |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1392 | rc = -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | repeatable &= cmdtp->repeatable; |
| 1396 | |
| 1397 | /* Did the user stop this? */ |
| 1398 | if (had_ctrlc ()) |
Detlev Zundel | eeab5a1 | 2007-05-23 18:47:48 +0200 | [diff] [blame] | 1399 | return -1; /* if stopped then not repeatable */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1402 | return rc ? rc : repeatable; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /****************************************************************************/ |
| 1406 | |
Jon Loeliger | 052fc84 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 1407 | #if defined(CONFIG_CMD_RUN) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1408 | int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 1409 | { |
| 1410 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (argc < 2) { |
| 1413 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 1414 | return 1; |
| 1415 | } |
| 1416 | |
| 1417 | for (i=1; i<argc; ++i) { |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1418 | char *arg; |
| 1419 | |
| 1420 | if ((arg = getenv (argv[i])) == NULL) { |
| 1421 | printf ("## Error: \"%s\" not defined\n", argv[i]); |
| 1422 | return 1; |
| 1423 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1424 | #ifndef CFG_HUSH_PARSER |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1425 | if (run_command (arg, flag) == -1) |
| 1426 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1427 | #else |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1428 | if (parse_string_outer(arg, |
wdenk | 541a76d | 2003-05-03 15:50:43 +0000 | [diff] [blame] | 1429 | FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0) |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1430 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1431 | #endif |
| 1432 | } |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1433 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1434 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1435 | #endif |