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