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