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