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