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> |
Simon Glass | 299f0be | 2014-04-10 20:01:24 -0600 | [diff] [blame^] | 17 | #include <cli_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 | |
Simon Glass | f5b1c6d | 2014-03-22 17:14:51 -0600 | [diff] [blame] | 430 | #ifndef CONFIG_SYS_GENERIC_BOARD |
| 431 | puts("Warning: Your board does not use generic board. Please read\n"); |
| 432 | puts("doc/README.generic-board and take action. Boards not\n"); |
| 433 | puts("upgraded by the late 2014 may break or be removed.\n"); |
| 434 | #endif |
| 435 | |
Simon Glass | de6f0c4 | 2013-05-15 06:23:56 +0000 | [diff] [blame] | 436 | #ifdef CONFIG_MODEM_SUPPORT |
| 437 | debug("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init); |
| 438 | if (do_mdm_init) { |
| 439 | char *str = strdup(getenv("mdm_cmd")); |
| 440 | setenv("preboot", str); /* set or delete definition */ |
| 441 | if (str != NULL) |
| 442 | free(str); |
| 443 | mdm_init(); /* wait for modem connection */ |
| 444 | } |
| 445 | #endif /* CONFIG_MODEM_SUPPORT */ |
| 446 | |
| 447 | #ifdef CONFIG_VERSION_VARIABLE |
| 448 | { |
| 449 | setenv("ver", version_string); /* set version variable */ |
| 450 | } |
| 451 | #endif /* CONFIG_VERSION_VARIABLE */ |
| 452 | |
| 453 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 454 | u_boot_hush_start(); |
| 455 | #endif |
| 456 | |
| 457 | #if defined(CONFIG_HUSH_INIT_VAR) |
| 458 | hush_init_var(); |
| 459 | #endif |
| 460 | |
| 461 | #ifdef CONFIG_PREBOOT |
| 462 | p = getenv("preboot"); |
| 463 | if (p != NULL) { |
| 464 | # ifdef CONFIG_AUTOBOOT_KEYED |
| 465 | int prev = disable_ctrlc(1); /* disable Control C checking */ |
| 466 | # endif |
| 467 | |
| 468 | run_command_list(p, -1, 0); |
| 469 | |
| 470 | # ifdef CONFIG_AUTOBOOT_KEYED |
| 471 | disable_ctrlc(prev); /* restore Control C checking */ |
| 472 | # endif |
| 473 | } |
| 474 | #endif /* CONFIG_PREBOOT */ |
| 475 | |
| 476 | #if defined(CONFIG_UPDATE_TFTP) |
| 477 | update_tftp(0UL); |
| 478 | #endif /* CONFIG_UPDATE_TFTP */ |
| 479 | |
| 480 | #ifdef CONFIG_BOOTDELAY |
| 481 | process_boot_delay(); |
| 482 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 483 | /* |
| 484 | * Main Loop for Monitor Command Processing |
| 485 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 486 | #ifdef CONFIG_SYS_HUSH_PARSER |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 487 | parse_file_outer(); |
| 488 | /* This point is never reached */ |
| 489 | for (;;); |
| 490 | #else |
| 491 | for (;;) { |
| 492 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 493 | if (rc >= 0) { |
| 494 | /* Saw enough of a valid command to |
| 495 | * restart the timeout. |
| 496 | */ |
| 497 | reset_cmd_timeout(); |
| 498 | } |
| 499 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 500 | len = readline (CONFIG_SYS_PROMPT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 501 | |
| 502 | flag = 0; /* assume no special flags for now */ |
| 503 | if (len > 0) |
| 504 | strcpy (lastcommand, console_buffer); |
| 505 | else if (len == 0) |
| 506 | flag |= CMD_FLAG_REPEAT; |
| 507 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 508 | else if (len == -2) { |
| 509 | /* -2 means timed out, retry autoboot |
| 510 | */ |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 511 | puts ("\nTimed out waiting for command\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 512 | # ifdef CONFIG_RESET_TO_RETRY |
| 513 | /* Reinit board to run initialization code again */ |
| 514 | do_reset (NULL, 0, 0, NULL); |
| 515 | # else |
| 516 | return; /* retry autoboot */ |
| 517 | # endif |
| 518 | } |
| 519 | #endif |
| 520 | |
| 521 | if (len == -1) |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 522 | puts ("<INTERRUPT>\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 523 | else |
Simon Glass | bf8c5b0 | 2012-02-14 19:59:21 +0000 | [diff] [blame] | 524 | rc = run_command(lastcommand, flag); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 525 | |
| 526 | if (rc <= 0) { |
| 527 | /* invalid command or not repeatable, forget it */ |
| 528 | lastcommand[0] = 0; |
| 529 | } |
| 530 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 531 | #endif /*CONFIG_SYS_HUSH_PARSER*/ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 532 | } |
| 533 | |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 534 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 535 | /*************************************************************************** |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 536 | * initialize command line timeout |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 537 | */ |
| 538 | void init_cmd_timeout(void) |
| 539 | { |
| 540 | char *s = getenv ("bootretry"); |
| 541 | |
| 542 | if (s != NULL) |
wdenk | 444f22b | 2003-12-07 21:39:28 +0000 | [diff] [blame] | 543 | retry_time = (int)simple_strtol(s, NULL, 10); |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 544 | else |
| 545 | retry_time = CONFIG_BOOT_RETRY_TIME; |
| 546 | |
| 547 | if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) |
| 548 | retry_time = CONFIG_BOOT_RETRY_MIN; |
| 549 | } |
| 550 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 551 | /*************************************************************************** |
| 552 | * reset command line timeout to retry_time seconds |
| 553 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 554 | void reset_cmd_timeout(void) |
| 555 | { |
| 556 | endtime = endtick(retry_time); |
| 557 | } |
| 558 | #endif |
| 559 | |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 560 | #ifdef CONFIG_CMDLINE_EDITING |
| 561 | |
| 562 | /* |
| 563 | * cmdline-editing related codes from vivi. |
| 564 | * Author: Janghoon Lyu <nandy@mizi.com> |
| 565 | */ |
| 566 | |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 567 | #define putnstr(str,n) do { \ |
Andrew Klossner | e4ad454 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 568 | printf ("%.*s", (int)n, str); \ |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 569 | } while (0) |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 570 | |
| 571 | #define CTL_CH(c) ((c) - 'a' + 1) |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 572 | #define CTL_BACKSPACE ('\b') |
| 573 | #define DEL ((char)255) |
| 574 | #define DEL7 ((char)127) |
| 575 | #define CREAD_HIST_CHAR ('!') |
| 576 | |
| 577 | #define getcmd_putch(ch) putc(ch) |
| 578 | #define getcmd_getch() getc() |
| 579 | #define getcmd_cbeep() getcmd_putch('\a') |
| 580 | |
| 581 | #define HIST_MAX 20 |
Peter Tyser | 7e0e5ca | 2010-09-29 13:30:56 -0500 | [diff] [blame] | 582 | #define HIST_SIZE CONFIG_SYS_CBSIZE |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 583 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 584 | static int hist_max; |
| 585 | static int hist_add_idx; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 586 | static int hist_cur = -1; |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 587 | static unsigned hist_num; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 588 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 589 | static char *hist_list[HIST_MAX]; |
| 590 | 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] | 591 | |
| 592 | #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) |
| 593 | |
| 594 | static void hist_init(void) |
| 595 | { |
| 596 | int i; |
| 597 | |
| 598 | hist_max = 0; |
| 599 | hist_add_idx = 0; |
| 600 | hist_cur = -1; |
| 601 | hist_num = 0; |
| 602 | |
| 603 | for (i = 0; i < HIST_MAX; i++) { |
| 604 | hist_list[i] = hist_lines[i]; |
| 605 | hist_list[i][0] = '\0'; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static void cread_add_to_hist(char *line) |
| 610 | { |
| 611 | strcpy(hist_list[hist_add_idx], line); |
| 612 | |
| 613 | if (++hist_add_idx >= HIST_MAX) |
| 614 | hist_add_idx = 0; |
| 615 | |
| 616 | if (hist_add_idx > hist_max) |
| 617 | hist_max = hist_add_idx; |
| 618 | |
| 619 | hist_num++; |
| 620 | } |
| 621 | |
| 622 | static char* hist_prev(void) |
| 623 | { |
| 624 | char *ret; |
| 625 | int old_cur; |
| 626 | |
| 627 | if (hist_cur < 0) |
| 628 | return NULL; |
| 629 | |
| 630 | old_cur = hist_cur; |
| 631 | if (--hist_cur < 0) |
| 632 | hist_cur = hist_max; |
| 633 | |
| 634 | if (hist_cur == hist_add_idx) { |
| 635 | hist_cur = old_cur; |
| 636 | ret = NULL; |
| 637 | } else |
| 638 | ret = hist_list[hist_cur]; |
| 639 | |
| 640 | return (ret); |
| 641 | } |
| 642 | |
| 643 | static char* hist_next(void) |
| 644 | { |
| 645 | char *ret; |
| 646 | |
| 647 | if (hist_cur < 0) |
| 648 | return NULL; |
| 649 | |
| 650 | if (hist_cur == hist_add_idx) |
| 651 | return NULL; |
| 652 | |
| 653 | if (++hist_cur > hist_max) |
| 654 | hist_cur = 0; |
| 655 | |
| 656 | if (hist_cur == hist_add_idx) { |
| 657 | ret = ""; |
| 658 | } else |
| 659 | ret = hist_list[hist_cur]; |
| 660 | |
| 661 | return (ret); |
| 662 | } |
| 663 | |
Stefan Roese | 59ff5a5 | 2006-07-27 16:11:19 +0200 | [diff] [blame] | 664 | #ifndef CONFIG_CMDLINE_EDITING |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 665 | static void cread_print_hist_list(void) |
| 666 | { |
| 667 | int i; |
| 668 | unsigned long n; |
| 669 | |
| 670 | n = hist_num - hist_max; |
| 671 | |
| 672 | i = hist_add_idx + 1; |
| 673 | while (1) { |
| 674 | if (i > hist_max) |
| 675 | i = 0; |
| 676 | if (i == hist_add_idx) |
| 677 | break; |
| 678 | printf("%s\n", hist_list[i]); |
| 679 | n++; |
| 680 | i++; |
| 681 | } |
| 682 | } |
Stefan Roese | 59ff5a5 | 2006-07-27 16:11:19 +0200 | [diff] [blame] | 683 | #endif /* CONFIG_CMDLINE_EDITING */ |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 684 | |
| 685 | #define BEGINNING_OF_LINE() { \ |
| 686 | while (num) { \ |
| 687 | getcmd_putch(CTL_BACKSPACE); \ |
| 688 | num--; \ |
| 689 | } \ |
| 690 | } |
| 691 | |
| 692 | #define ERASE_TO_EOL() { \ |
| 693 | if (num < eol_num) { \ |
Mike Frysinger | 3952bea | 2010-07-23 05:28:15 -0400 | [diff] [blame] | 694 | printf("%*s", (int)(eol_num - num), ""); \ |
| 695 | do { \ |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 696 | getcmd_putch(CTL_BACKSPACE); \ |
Mike Frysinger | 3952bea | 2010-07-23 05:28:15 -0400 | [diff] [blame] | 697 | } while (--eol_num > num); \ |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 698 | } \ |
| 699 | } |
| 700 | |
| 701 | #define REFRESH_TO_EOL() { \ |
| 702 | if (num < eol_num) { \ |
| 703 | wlen = eol_num - num; \ |
| 704 | putnstr(buf + num, wlen); \ |
| 705 | num = eol_num; \ |
| 706 | } \ |
| 707 | } |
| 708 | |
| 709 | static void cread_add_char(char ichar, int insert, unsigned long *num, |
| 710 | unsigned long *eol_num, char *buf, unsigned long len) |
| 711 | { |
| 712 | unsigned long wlen; |
| 713 | |
| 714 | /* room ??? */ |
| 715 | if (insert || *num == *eol_num) { |
| 716 | if (*eol_num > len - 1) { |
| 717 | getcmd_cbeep(); |
| 718 | return; |
| 719 | } |
| 720 | (*eol_num)++; |
| 721 | } |
| 722 | |
| 723 | if (insert) { |
| 724 | wlen = *eol_num - *num; |
| 725 | if (wlen > 1) { |
| 726 | memmove(&buf[*num+1], &buf[*num], wlen-1); |
| 727 | } |
| 728 | |
| 729 | buf[*num] = ichar; |
| 730 | putnstr(buf + *num, wlen); |
| 731 | (*num)++; |
| 732 | while (--wlen) { |
| 733 | getcmd_putch(CTL_BACKSPACE); |
| 734 | } |
| 735 | } else { |
| 736 | /* echo the character */ |
| 737 | wlen = 1; |
| 738 | buf[*num] = ichar; |
| 739 | putnstr(buf + *num, wlen); |
| 740 | (*num)++; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | static void cread_add_str(char *str, int strsize, int insert, unsigned long *num, |
| 745 | unsigned long *eol_num, char *buf, unsigned long len) |
| 746 | { |
| 747 | while (strsize--) { |
| 748 | cread_add_char(*str, insert, num, eol_num, buf, len); |
| 749 | str++; |
| 750 | } |
| 751 | } |
| 752 | |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 753 | static int cread_line(const char *const prompt, char *buf, unsigned int *len, |
| 754 | int timeout) |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 755 | { |
| 756 | unsigned long num = 0; |
| 757 | unsigned long eol_num = 0; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 758 | unsigned long wlen; |
| 759 | char ichar; |
| 760 | int insert = 1; |
| 761 | int esc_len = 0; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 762 | char esc_save[8]; |
Peter Tyser | 8ad15a9 | 2009-10-25 15:12:54 -0500 | [diff] [blame] | 763 | int init_len = strlen(buf); |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 764 | int first = 1; |
Peter Tyser | 8ad15a9 | 2009-10-25 15:12:54 -0500 | [diff] [blame] | 765 | |
| 766 | if (init_len) |
| 767 | cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 768 | |
| 769 | while (1) { |
Andreas Engel | 3a0ba10 | 2008-01-09 17:10:56 +0100 | [diff] [blame] | 770 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 771 | while (!tstc()) { /* while no incoming data */ |
| 772 | if (retry_time >= 0 && get_ticks() > endtime) |
| 773 | return (-2); /* timed out */ |
Jens Scharsig | 1124755 | 2010-04-09 19:02:38 +0200 | [diff] [blame] | 774 | WATCHDOG_RESET(); |
Andreas Engel | 3a0ba10 | 2008-01-09 17:10:56 +0100 | [diff] [blame] | 775 | } |
| 776 | #endif |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 777 | if (first && timeout) { |
| 778 | uint64_t etime = endtick(timeout); |
| 779 | |
| 780 | while (!tstc()) { /* while no incoming data */ |
| 781 | if (get_ticks() >= etime) |
| 782 | return -2; /* timed out */ |
| 783 | WATCHDOG_RESET(); |
| 784 | } |
| 785 | first = 0; |
| 786 | } |
Andreas Engel | 3a0ba10 | 2008-01-09 17:10:56 +0100 | [diff] [blame] | 787 | |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 788 | ichar = getcmd_getch(); |
| 789 | |
| 790 | if ((ichar == '\n') || (ichar == '\r')) { |
Wolfgang Denk | 92549de | 2006-07-21 11:34:34 +0200 | [diff] [blame] | 791 | putc('\n'); |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 792 | break; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * handle standard linux xterm esc sequences for arrow key, etc. |
| 797 | */ |
| 798 | if (esc_len != 0) { |
| 799 | if (esc_len == 1) { |
| 800 | if (ichar == '[') { |
| 801 | esc_save[esc_len] = ichar; |
| 802 | esc_len = 2; |
| 803 | } else { |
| 804 | cread_add_str(esc_save, esc_len, insert, |
| 805 | &num, &eol_num, buf, *len); |
| 806 | esc_len = 0; |
| 807 | } |
| 808 | continue; |
| 809 | } |
| 810 | |
| 811 | switch (ichar) { |
| 812 | |
| 813 | case 'D': /* <- key */ |
| 814 | ichar = CTL_CH('b'); |
| 815 | esc_len = 0; |
| 816 | break; |
| 817 | case 'C': /* -> key */ |
| 818 | ichar = CTL_CH('f'); |
| 819 | esc_len = 0; |
| 820 | break; /* pass off to ^F handler */ |
| 821 | case 'H': /* Home key */ |
| 822 | ichar = CTL_CH('a'); |
| 823 | esc_len = 0; |
| 824 | break; /* pass off to ^A handler */ |
| 825 | case 'A': /* up arrow */ |
| 826 | ichar = CTL_CH('p'); |
| 827 | esc_len = 0; |
| 828 | break; /* pass off to ^P handler */ |
| 829 | case 'B': /* down arrow */ |
| 830 | ichar = CTL_CH('n'); |
| 831 | esc_len = 0; |
| 832 | break; /* pass off to ^N handler */ |
| 833 | default: |
| 834 | esc_save[esc_len++] = ichar; |
| 835 | cread_add_str(esc_save, esc_len, insert, |
| 836 | &num, &eol_num, buf, *len); |
| 837 | esc_len = 0; |
| 838 | continue; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | switch (ichar) { |
| 843 | case 0x1b: |
| 844 | if (esc_len == 0) { |
| 845 | esc_save[esc_len] = ichar; |
| 846 | esc_len = 1; |
| 847 | } else { |
Wolfgang Denk | 92549de | 2006-07-21 11:34:34 +0200 | [diff] [blame] | 848 | puts("impossible condition #876\n"); |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 849 | esc_len = 0; |
| 850 | } |
| 851 | break; |
| 852 | |
| 853 | case CTL_CH('a'): |
| 854 | BEGINNING_OF_LINE(); |
| 855 | break; |
| 856 | case CTL_CH('c'): /* ^C - break */ |
| 857 | *buf = '\0'; /* discard input */ |
| 858 | return (-1); |
| 859 | case CTL_CH('f'): |
| 860 | if (num < eol_num) { |
| 861 | getcmd_putch(buf[num]); |
| 862 | num++; |
| 863 | } |
| 864 | break; |
| 865 | case CTL_CH('b'): |
| 866 | if (num) { |
| 867 | getcmd_putch(CTL_BACKSPACE); |
| 868 | num--; |
| 869 | } |
| 870 | break; |
| 871 | case CTL_CH('d'): |
| 872 | if (num < eol_num) { |
| 873 | wlen = eol_num - num - 1; |
| 874 | if (wlen) { |
| 875 | memmove(&buf[num], &buf[num+1], wlen); |
| 876 | putnstr(buf + num, wlen); |
| 877 | } |
| 878 | |
| 879 | getcmd_putch(' '); |
| 880 | do { |
| 881 | getcmd_putch(CTL_BACKSPACE); |
| 882 | } while (wlen--); |
| 883 | eol_num--; |
| 884 | } |
| 885 | break; |
| 886 | case CTL_CH('k'): |
| 887 | ERASE_TO_EOL(); |
| 888 | break; |
| 889 | case CTL_CH('e'): |
| 890 | REFRESH_TO_EOL(); |
| 891 | break; |
| 892 | case CTL_CH('o'): |
| 893 | insert = !insert; |
| 894 | break; |
| 895 | case CTL_CH('x'): |
Jean-Christophe PLAGNIOL-VILLARD | 6366f00 | 2007-12-22 15:52:58 +0100 | [diff] [blame] | 896 | case CTL_CH('u'): |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 897 | BEGINNING_OF_LINE(); |
| 898 | ERASE_TO_EOL(); |
| 899 | break; |
| 900 | case DEL: |
| 901 | case DEL7: |
| 902 | case 8: |
| 903 | if (num) { |
| 904 | wlen = eol_num - num; |
| 905 | num--; |
| 906 | memmove(&buf[num], &buf[num+1], wlen); |
| 907 | getcmd_putch(CTL_BACKSPACE); |
| 908 | putnstr(buf + num, wlen); |
| 909 | getcmd_putch(' '); |
| 910 | do { |
| 911 | getcmd_putch(CTL_BACKSPACE); |
| 912 | } while (wlen--); |
| 913 | eol_num--; |
| 914 | } |
| 915 | break; |
| 916 | case CTL_CH('p'): |
| 917 | case CTL_CH('n'): |
| 918 | { |
| 919 | char * hline; |
| 920 | |
| 921 | esc_len = 0; |
| 922 | |
| 923 | if (ichar == CTL_CH('p')) |
| 924 | hline = hist_prev(); |
| 925 | else |
| 926 | hline = hist_next(); |
| 927 | |
| 928 | if (!hline) { |
| 929 | getcmd_cbeep(); |
| 930 | continue; |
| 931 | } |
| 932 | |
| 933 | /* nuke the current line */ |
| 934 | /* first, go home */ |
| 935 | BEGINNING_OF_LINE(); |
| 936 | |
| 937 | /* erase to end of line */ |
| 938 | ERASE_TO_EOL(); |
| 939 | |
| 940 | /* copy new line into place and display */ |
| 941 | strcpy(buf, hline); |
| 942 | eol_num = strlen(buf); |
| 943 | REFRESH_TO_EOL(); |
| 944 | continue; |
| 945 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6366f00 | 2007-12-22 15:52:58 +0100 | [diff] [blame] | 946 | #ifdef CONFIG_AUTO_COMPLETE |
| 947 | case '\t': { |
| 948 | int num2, col; |
| 949 | |
| 950 | /* do not autocomplete when in the middle */ |
| 951 | if (num < eol_num) { |
| 952 | getcmd_cbeep(); |
| 953 | break; |
| 954 | } |
| 955 | |
| 956 | buf[num] = '\0'; |
| 957 | col = strlen(prompt) + eol_num; |
| 958 | num2 = num; |
| 959 | if (cmd_auto_complete(prompt, buf, &num2, &col)) { |
| 960 | col = num2 - num; |
| 961 | num += col; |
| 962 | eol_num += col; |
| 963 | } |
| 964 | break; |
| 965 | } |
| 966 | #endif |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 967 | default: |
| 968 | cread_add_char(ichar, insert, &num, &eol_num, buf, *len); |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | *len = eol_num; |
| 973 | buf[eol_num] = '\0'; /* lose the newline */ |
| 974 | |
| 975 | if (buf[0] && buf[0] != CREAD_HIST_CHAR) |
| 976 | cread_add_to_hist(buf); |
| 977 | hist_cur = hist_add_idx; |
| 978 | |
Peter Tyser | 03e46c4 | 2009-10-25 15:12:53 -0500 | [diff] [blame] | 979 | return 0; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | #endif /* CONFIG_CMDLINE_EDITING */ |
| 983 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 984 | /****************************************************************************/ |
| 985 | |
| 986 | /* |
| 987 | * Prompt for input and read a line. |
| 988 | * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, |
| 989 | * time out when time goes past endtime (timebase time in ticks). |
| 990 | * Return: number of read characters |
| 991 | * -1 if break |
| 992 | * -2 if timed out |
| 993 | */ |
| 994 | int readline (const char *const prompt) |
| 995 | { |
Peter Tyser | 8ad15a9 | 2009-10-25 15:12:54 -0500 | [diff] [blame] | 996 | /* |
| 997 | * If console_buffer isn't 0-length the user will be prompted to modify |
| 998 | * it instead of entering it from scratch as desired. |
| 999 | */ |
| 1000 | console_buffer[0] = '\0'; |
| 1001 | |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 1002 | return readline_into_buffer(prompt, console_buffer, 0); |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 1006 | int readline_into_buffer(const char *const prompt, char *buffer, int timeout) |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1007 | { |
| 1008 | char *p = buffer; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 1009 | #ifdef CONFIG_CMDLINE_EDITING |
Peter Tyser | 7e0e5ca | 2010-09-29 13:30:56 -0500 | [diff] [blame] | 1010 | unsigned int len = CONFIG_SYS_CBSIZE; |
Stefan Roese | dc5ff70 | 2006-08-07 15:08:44 +0200 | [diff] [blame] | 1011 | int rc; |
Wolfgang Denk | 4c9ca78 | 2006-07-21 11:33:45 +0200 | [diff] [blame] | 1012 | static int initted = 0; |
| 1013 | |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 1014 | /* |
| 1015 | * History uses a global array which is not |
| 1016 | * writable until after relocation to RAM. |
| 1017 | * Revert to non-history version if still |
| 1018 | * running from flash. |
| 1019 | */ |
| 1020 | if (gd->flags & GD_FLG_RELOC) { |
| 1021 | if (!initted) { |
| 1022 | hist_init(); |
| 1023 | initted = 1; |
| 1024 | } |
| 1025 | |
Peter Tyser | c144e01 | 2009-10-25 15:12:52 -0500 | [diff] [blame] | 1026 | if (prompt) |
| 1027 | puts (prompt); |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 1028 | |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 1029 | rc = cread_line(prompt, p, &len, timeout); |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 1030 | return rc < 0 ? rc : len; |
| 1031 | |
| 1032 | } else { |
| 1033 | #endif /* CONFIG_CMDLINE_EDITING */ |
Kumar Gala | e225b17 | 2008-01-10 02:22:05 -0600 | [diff] [blame] | 1034 | char * p_buf = p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1035 | int n = 0; /* buffer index */ |
| 1036 | int plen = 0; /* prompt length */ |
| 1037 | int col; /* output column cnt */ |
| 1038 | char c; |
| 1039 | |
| 1040 | /* print prompt */ |
| 1041 | if (prompt) { |
| 1042 | plen = strlen (prompt); |
| 1043 | puts (prompt); |
| 1044 | } |
| 1045 | col = plen; |
| 1046 | |
| 1047 | for (;;) { |
| 1048 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 1049 | while (!tstc()) { /* while no incoming data */ |
| 1050 | if (retry_time >= 0 && get_ticks() > endtime) |
| 1051 | return (-2); /* timed out */ |
Jens Scharsig | 1124755 | 2010-04-09 19:02:38 +0200 | [diff] [blame] | 1052 | WATCHDOG_RESET(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1053 | } |
| 1054 | #endif |
| 1055 | WATCHDOG_RESET(); /* Trigger watchdog, if needed */ |
| 1056 | |
| 1057 | #ifdef CONFIG_SHOW_ACTIVITY |
| 1058 | while (!tstc()) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1059 | show_activity(0); |
Jens Scharsig | 1124755 | 2010-04-09 19:02:38 +0200 | [diff] [blame] | 1060 | WATCHDOG_RESET(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1061 | } |
| 1062 | #endif |
| 1063 | c = getc(); |
| 1064 | |
| 1065 | /* |
| 1066 | * Special character handling |
| 1067 | */ |
| 1068 | switch (c) { |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1069 | case '\r': /* Enter */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1070 | case '\n': |
| 1071 | *p = '\0'; |
| 1072 | puts ("\r\n"); |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1073 | return p - p_buf; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1074 | |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1075 | case '\0': /* nul */ |
wdenk | 8d4d1f6 | 2004-03-23 22:37:33 +0000 | [diff] [blame] | 1076 | continue; |
| 1077 | |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1078 | case 0x03: /* ^C - break */ |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1079 | p_buf[0] = '\0'; /* discard input */ |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1080 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1081 | |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1082 | case 0x15: /* ^U - erase line */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1083 | while (col > plen) { |
| 1084 | puts (erase_seq); |
| 1085 | --col; |
| 1086 | } |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1087 | p = p_buf; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1088 | n = 0; |
| 1089 | continue; |
| 1090 | |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1091 | case 0x17: /* ^W - erase word */ |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1092 | p=delete_char(p_buf, p, &col, &n, plen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1093 | while ((n > 0) && (*p != ' ')) { |
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 | } |
| 1096 | continue; |
| 1097 | |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1098 | case 0x08: /* ^H - backspace */ |
| 1099 | case 0x7F: /* DEL - backspace */ |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1100 | p=delete_char(p_buf, p, &col, &n, plen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1101 | continue; |
| 1102 | |
| 1103 | default: |
| 1104 | /* |
| 1105 | * Must be a normal character then |
| 1106 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1107 | if (n < CONFIG_SYS_CBSIZE-2) { |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1108 | if (c == '\t') { /* expand TABs */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 1109 | #ifdef CONFIG_AUTO_COMPLETE |
| 1110 | /* if auto completion triggered just continue */ |
| 1111 | *p = '\0'; |
| 1112 | if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { |
James Yang | 20ffa97 | 2008-01-09 11:17:49 -0600 | [diff] [blame] | 1113 | p = p_buf + n; /* reset */ |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 1114 | continue; |
| 1115 | } |
| 1116 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1117 | puts (tab_seq+(col&07)); |
| 1118 | col += 8 - (col&07); |
| 1119 | } else { |
Simon Glass | ef45ffd | 2012-10-30 13:40:18 +0000 | [diff] [blame] | 1120 | char buf[2]; |
| 1121 | |
| 1122 | /* |
Simon Glass | e30a5f7 | 2013-05-15 06:23:58 +0000 | [diff] [blame] | 1123 | * Echo input using puts() to force an |
Simon Glass | ef45ffd | 2012-10-30 13:40:18 +0000 | [diff] [blame] | 1124 | * LCD flush if we are using an LCD |
| 1125 | */ |
| 1126 | ++col; |
| 1127 | buf[0] = c; |
| 1128 | buf[1] = '\0'; |
| 1129 | puts(buf); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1130 | } |
| 1131 | *p++ = c; |
| 1132 | ++n; |
| 1133 | } else { /* Buffer full */ |
| 1134 | putc ('\a'); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
James Yang | b83983f | 2008-05-05 10:22:53 -0500 | [diff] [blame] | 1138 | #ifdef CONFIG_CMDLINE_EDITING |
| 1139 | } |
| 1140 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /****************************************************************************/ |
| 1144 | |
| 1145 | static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen) |
| 1146 | { |
| 1147 | char *s; |
| 1148 | |
| 1149 | if (*np == 0) { |
| 1150 | return (p); |
| 1151 | } |
| 1152 | |
| 1153 | if (*(--p) == '\t') { /* will retype the whole line */ |
| 1154 | while (*colp > plen) { |
| 1155 | puts (erase_seq); |
| 1156 | (*colp)--; |
| 1157 | } |
| 1158 | for (s=buffer; s<p; ++s) { |
| 1159 | if (*s == '\t') { |
| 1160 | puts (tab_seq+((*colp) & 07)); |
| 1161 | *colp += 8 - ((*colp) & 07); |
| 1162 | } else { |
| 1163 | ++(*colp); |
| 1164 | putc (*s); |
| 1165 | } |
| 1166 | } |
| 1167 | } else { |
| 1168 | puts (erase_seq); |
| 1169 | (*colp)--; |
| 1170 | } |
| 1171 | (*np)--; |
| 1172 | return (p); |
| 1173 | } |
| 1174 | |
| 1175 | /****************************************************************************/ |
| 1176 | |
| 1177 | int parse_line (char *line, char *argv[]) |
| 1178 | { |
| 1179 | int nargs = 0; |
| 1180 | |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1181 | debug_parser("parse_line: \"%s\"\n", line); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1182 | while (nargs < CONFIG_SYS_MAXARGS) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* skip any white space */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 1185 | while (isblank(*line)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1186 | ++line; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1187 | |
| 1188 | if (*line == '\0') { /* end of line, no more args */ |
| 1189 | argv[nargs] = NULL; |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1190 | debug_parser("parse_line: nargs=%d\n", nargs); |
| 1191 | return nargs; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | argv[nargs++] = line; /* begin of argument string */ |
| 1195 | |
| 1196 | /* find end of string */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 1197 | while (*line && !isblank(*line)) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1198 | ++line; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (*line == '\0') { /* end of line, no more args */ |
| 1201 | argv[nargs] = NULL; |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1202 | debug_parser("parse_line: nargs=%d\n", nargs); |
| 1203 | return nargs; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | *line++ = '\0'; /* terminate current arg */ |
| 1207 | } |
| 1208 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1209 | printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1210 | |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1211 | debug_parser("parse_line: nargs=%d\n", nargs); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1212 | return (nargs); |
| 1213 | } |
| 1214 | |
| 1215 | /****************************************************************************/ |
| 1216 | |
Simon Glass | 82c2132 | 2012-02-14 19:59:22 +0000 | [diff] [blame] | 1217 | #ifndef CONFIG_SYS_HUSH_PARSER |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1218 | static void process_macros (const char *input, char *output) |
| 1219 | { |
| 1220 | char c, prev; |
| 1221 | const char *varname_start = NULL; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1222 | int inputcnt = strlen (input); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1223 | int outputcnt = CONFIG_SYS_CBSIZE; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1224 | int state = 0; /* 0 = waiting for '$' */ |
| 1225 | |
| 1226 | /* 1 = waiting for '(' or '{' */ |
| 1227 | /* 2 = waiting for ')' or '}' */ |
| 1228 | /* 3 = waiting for ''' */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1229 | char *output_start = output; |
| 1230 | |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1231 | debug_parser("[PROCESS_MACROS] INPUT len %zd: \"%s\"\n", strlen(input), |
| 1232 | input); |
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 | prev = '\0'; /* previous character */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1235 | |
| 1236 | while (inputcnt && outputcnt) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1237 | c = *input++; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1238 | inputcnt--; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1239 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1240 | if (state != 3) { |
| 1241 | /* remove one level of escape characters */ |
| 1242 | if ((c == '\\') && (prev != '\\')) { |
| 1243 | if (inputcnt-- == 0) |
| 1244 | break; |
| 1245 | prev = c; |
| 1246 | c = *input++; |
| 1247 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1248 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1249 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1250 | switch (state) { |
| 1251 | case 0: /* Waiting for (unescaped) $ */ |
| 1252 | if ((c == '\'') && (prev != '\\')) { |
| 1253 | state = 3; |
| 1254 | break; |
| 1255 | } |
| 1256 | if ((c == '$') && (prev != '\\')) { |
| 1257 | state++; |
| 1258 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1259 | *(output++) = c; |
| 1260 | outputcnt--; |
| 1261 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1262 | break; |
| 1263 | case 1: /* Waiting for ( */ |
| 1264 | if (c == '(' || c == '{') { |
| 1265 | state++; |
| 1266 | varname_start = input; |
| 1267 | } else { |
| 1268 | state = 0; |
| 1269 | *(output++) = '$'; |
| 1270 | outputcnt--; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1271 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1272 | if (outputcnt) { |
| 1273 | *(output++) = c; |
| 1274 | outputcnt--; |
| 1275 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1276 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | case 2: /* Waiting for ) */ |
| 1279 | if (c == ')' || c == '}') { |
| 1280 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1281 | char envname[CONFIG_SYS_CBSIZE], *envval; |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1282 | int envcnt = input - varname_start - 1; /* Varname # of chars */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1283 | |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1284 | /* Get the varname */ |
| 1285 | for (i = 0; i < envcnt; i++) { |
| 1286 | envname[i] = varname_start[i]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1287 | } |
Wolfgang Denk | 9c82c68 | 2006-10-28 00:38:39 +0200 | [diff] [blame] | 1288 | envname[i] = 0; |
| 1289 | |
| 1290 | /* Get its value */ |
| 1291 | envval = getenv (envname); |
| 1292 | |
| 1293 | /* Copy into the line if it exists */ |
| 1294 | if (envval != NULL) |
| 1295 | while ((*envval) && outputcnt) { |
| 1296 | *(output++) = *(envval++); |
| 1297 | outputcnt--; |
| 1298 | } |
| 1299 | /* Look for another '$' */ |
| 1300 | state = 0; |
| 1301 | } |
| 1302 | break; |
| 1303 | case 3: /* Waiting for ' */ |
| 1304 | if ((c == '\'') && (prev != '\\')) { |
| 1305 | state = 0; |
| 1306 | } else { |
| 1307 | *(output++) = c; |
| 1308 | outputcnt--; |
| 1309 | } |
| 1310 | break; |
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 | prev = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | if (outputcnt) |
| 1316 | *output = 0; |
Bartlomiej Sieka | cadc8e8 | 2007-05-27 17:04:18 +0200 | [diff] [blame] | 1317 | else |
| 1318 | *(output - 1) = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1319 | |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1320 | debug_parser("[PROCESS_MACROS] OUTPUT len %zd: \"%s\"\n", |
| 1321 | strlen(output_start), output_start); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /**************************************************************************** |
| 1325 | * returns: |
| 1326 | * 1 - command executed, repeatable |
| 1327 | * 0 - command executed but not repeatable, interrupted commands are |
| 1328 | * always considered not repeatable |
| 1329 | * -1 - not executed (unrecognized, bootd recursion or too many args) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1330 | * (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] | 1331 | * considered unrecognized) |
| 1332 | * |
| 1333 | * WARNING: |
| 1334 | * |
| 1335 | * We must create a temporary copy of the command since the command we get |
| 1336 | * may be the result from getenv(), which returns a pointer directly to |
| 1337 | * the environment data, which may change magicly when the command we run |
| 1338 | * creates or modifies environment variables (like "bootp" does). |
| 1339 | */ |
Simon Glass | bf8c5b0 | 2012-02-14 19:59:21 +0000 | [diff] [blame] | 1340 | static int builtin_run_command(const char *cmd, int flag) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1341 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1342 | char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1343 | char *token; /* start of token in cmdbuf */ |
| 1344 | char *sep; /* end of token (separator) in cmdbuf */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1345 | char finaltoken[CONFIG_SYS_CBSIZE]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1346 | char *str = cmdbuf; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1347 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1348 | int argc, inquotes; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1349 | int repeatable = 1; |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1350 | int rc = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1351 | |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1352 | debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd); |
| 1353 | if (DEBUG_PARSER) { |
| 1354 | /* use puts - string may be loooong */ |
| 1355 | puts(cmd ? cmd : "NULL"); |
| 1356 | puts("\"\n"); |
| 1357 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1358 | clear_ctrlc(); /* forget any previous Control C */ |
| 1359 | |
| 1360 | if (!cmd || !*cmd) { |
| 1361 | return -1; /* empty command */ |
| 1362 | } |
| 1363 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1364 | if (strlen(cmd) >= CONFIG_SYS_CBSIZE) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1365 | puts ("## Command too long!\n"); |
| 1366 | return -1; |
| 1367 | } |
| 1368 | |
| 1369 | strcpy (cmdbuf, cmd); |
| 1370 | |
| 1371 | /* Process separators and check for invalid |
| 1372 | * repeatable commands |
| 1373 | */ |
| 1374 | |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1375 | debug_parser("[PROCESS_SEPARATORS] %s\n", cmd); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1376 | while (*str) { |
| 1377 | |
| 1378 | /* |
| 1379 | * Find separator, or string end |
| 1380 | * Allow simple escape of ';' by writing "\;" |
| 1381 | */ |
wdenk | 654868c | 2003-01-02 23:57:29 +0000 | [diff] [blame] | 1382 | for (inquotes = 0, sep = str; *sep; sep++) { |
| 1383 | if ((*sep=='\'') && |
| 1384 | (*(sep-1) != '\\')) |
| 1385 | inquotes=!inquotes; |
| 1386 | |
| 1387 | if (!inquotes && |
| 1388 | (*sep == ';') && /* separator */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1389 | ( sep != str) && /* past string start */ |
| 1390 | (*(sep-1) != '\\')) /* and NOT escaped */ |
| 1391 | break; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Limit the token to data between separators |
| 1396 | */ |
| 1397 | token = str; |
| 1398 | if (*sep) { |
| 1399 | str = sep + 1; /* start of command for next pass */ |
| 1400 | *sep = '\0'; |
| 1401 | } |
| 1402 | else |
| 1403 | str = sep; /* no more commands for next pass */ |
Simon Glass | a1a16a8 | 2013-05-15 06:24:00 +0000 | [diff] [blame] | 1404 | debug_parser("token: \"%s\"\n", token); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1405 | |
| 1406 | /* find macros in this token and replace them */ |
| 1407 | process_macros (token, finaltoken); |
| 1408 | |
| 1409 | /* Extract arguments */ |
Wolfgang Denk | d44e7bb | 2006-03-12 02:20:55 +0100 | [diff] [blame] | 1410 | if ((argc = parse_line (finaltoken, argv)) == 0) { |
| 1411 | rc = -1; /* no command at all */ |
| 1412 | continue; |
| 1413 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1414 | |
Richard Genoud | 94ff067 | 2012-12-03 06:28:28 +0000 | [diff] [blame] | 1415 | if (cmd_process(flag, argc, argv, &repeatable, NULL)) |
Timo Ketola | 9ba5472 | 2012-04-22 23:57:27 +0000 | [diff] [blame] | 1416 | rc = -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1417 | |
| 1418 | /* Did the user stop this? */ |
| 1419 | if (had_ctrlc ()) |
Detlev Zundel | eeab5a1 | 2007-05-23 18:47:48 +0200 | [diff] [blame] | 1420 | return -1; /* if stopped then not repeatable */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
wdenk | f4688a2 | 2003-05-28 08:06:31 +0000 | [diff] [blame] | 1423 | return rc ? rc : repeatable; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1424 | } |
Simon Glass | 82c2132 | 2012-02-14 19:59:22 +0000 | [diff] [blame] | 1425 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1426 | |
Simon Glass | bf8c5b0 | 2012-02-14 19:59:21 +0000 | [diff] [blame] | 1427 | /* |
| 1428 | * Run a command using the selected parser. |
| 1429 | * |
| 1430 | * @param cmd Command to run |
| 1431 | * @param flag Execution flags (CMD_FLAG_...) |
| 1432 | * @return 0 on success, or != 0 on error. |
| 1433 | */ |
| 1434 | int run_command(const char *cmd, int flag) |
| 1435 | { |
| 1436 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 1437 | /* |
| 1438 | * builtin_run_command can return 0 or 1 for success, so clean up |
| 1439 | * its result. |
| 1440 | */ |
| 1441 | if (builtin_run_command(cmd, flag) == -1) |
| 1442 | return 1; |
| 1443 | |
| 1444 | return 0; |
| 1445 | #else |
| 1446 | return parse_string_outer(cmd, |
| 1447 | FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP); |
| 1448 | #endif |
| 1449 | } |
| 1450 | |
Simon Glass | a6642e0 | 2012-03-30 21:30:55 +0000 | [diff] [blame] | 1451 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 1452 | /** |
| 1453 | * Execute a list of command separated by ; or \n using the built-in parser. |
| 1454 | * |
| 1455 | * This function cannot take a const char * for the command, since if it |
| 1456 | * finds newlines in the string, it replaces them with \0. |
| 1457 | * |
| 1458 | * @param cmd String containing list of commands |
| 1459 | * @param flag Execution flags (CMD_FLAG_...) |
| 1460 | * @return 0 on success, or != 0 on error. |
| 1461 | */ |
| 1462 | static int builtin_run_command_list(char *cmd, int flag) |
| 1463 | { |
| 1464 | char *line, *next; |
| 1465 | int rcode = 0; |
| 1466 | |
| 1467 | /* |
| 1468 | * Break into individual lines, and execute each line; terminate on |
| 1469 | * error. |
| 1470 | */ |
| 1471 | line = next = cmd; |
| 1472 | while (*next) { |
| 1473 | if (*next == '\n') { |
| 1474 | *next = '\0'; |
| 1475 | /* run only non-empty commands */ |
| 1476 | if (*line) { |
| 1477 | debug("** exec: \"%s\"\n", line); |
| 1478 | if (builtin_run_command(line, 0) < 0) { |
| 1479 | rcode = 1; |
| 1480 | break; |
| 1481 | } |
| 1482 | } |
| 1483 | line = next + 1; |
| 1484 | } |
| 1485 | ++next; |
| 1486 | } |
| 1487 | if (rcode == 0 && *line) |
| 1488 | rcode = (builtin_run_command(line, 0) >= 0); |
| 1489 | |
| 1490 | return rcode; |
| 1491 | } |
| 1492 | #endif |
| 1493 | |
| 1494 | int run_command_list(const char *cmd, int len, int flag) |
| 1495 | { |
| 1496 | int need_buff = 1; |
| 1497 | char *buff = (char *)cmd; /* cast away const */ |
| 1498 | int rcode = 0; |
| 1499 | |
| 1500 | if (len == -1) { |
| 1501 | len = strlen(cmd); |
| 1502 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 1503 | /* hush will never change our string */ |
| 1504 | need_buff = 0; |
| 1505 | #else |
| 1506 | /* the built-in parser will change our string if it sees \n */ |
| 1507 | need_buff = strchr(cmd, '\n') != NULL; |
| 1508 | #endif |
| 1509 | } |
| 1510 | if (need_buff) { |
| 1511 | buff = malloc(len + 1); |
| 1512 | if (!buff) |
| 1513 | return 1; |
| 1514 | memcpy(buff, cmd, len); |
| 1515 | buff[len] = '\0'; |
| 1516 | } |
| 1517 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 1518 | rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON); |
| 1519 | #else |
| 1520 | /* |
| 1521 | * This function will overwrite any \n it sees with a \0, which |
| 1522 | * is why it can't work with a const char *. Here we are making |
| 1523 | * using of internal knowledge of this function, to avoid always |
| 1524 | * doing a malloc() which is actually required only in a case that |
| 1525 | * is pretty rare. |
| 1526 | */ |
| 1527 | rcode = builtin_run_command_list(buff, flag); |
| 1528 | if (need_buff) |
| 1529 | free(buff); |
| 1530 | #endif |
| 1531 | |
| 1532 | return rcode; |
| 1533 | } |
| 1534 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1535 | /****************************************************************************/ |
| 1536 | |
Jon Loeliger | 052fc84 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 1537 | #if defined(CONFIG_CMD_RUN) |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 1538 | 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] | 1539 | { |
| 1540 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1541 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 1542 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1543 | return CMD_RET_USAGE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1544 | |
| 1545 | for (i=1; i<argc; ++i) { |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1546 | char *arg; |
| 1547 | |
| 1548 | if ((arg = getenv (argv[i])) == NULL) { |
| 1549 | printf ("## Error: \"%s\" not defined\n", argv[i]); |
| 1550 | return 1; |
| 1551 | } |
Jason Hobbs | a56febb | 2011-08-31 05:37:24 +0000 | [diff] [blame] | 1552 | |
Simon Glass | 4c39b1d | 2013-10-25 23:01:32 -0600 | [diff] [blame] | 1553 | if (run_command_list(arg, -1, flag) != 0) |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1554 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1555 | } |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 1556 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1557 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1558 | #endif |