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