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