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