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