wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | /************************************************************************** |
| 28 | * |
| 29 | * Support for persistent environment data |
| 30 | * |
| 31 | * The "environment" is stored as a list of '\0' terminated |
| 32 | * "name=value" strings. The end of the list is marked by a double |
| 33 | * '\0'. New entries are always added at the end. Deleting an entry |
| 34 | * shifts the remaining entries to the front. Replacing an entry is a |
| 35 | * combination of deleting the old value and adding the new one. |
| 36 | * |
| 37 | * The environment is preceeded by a 32 bit CRC over the data part. |
| 38 | * |
| 39 | ************************************************************************** |
| 40 | */ |
| 41 | |
| 42 | #include <common.h> |
| 43 | #include <command.h> |
| 44 | #include <environment.h> |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 45 | #include <watchdog.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 46 | #include <serial.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 47 | #include <linux/stddef.h> |
| 48 | #include <asm/byteorder.h> |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 49 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 50 | #include <net.h> |
| 51 | #endif |
| 52 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
Jean-Christophe PLAGNIOL-VILLARD | 9314cee | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 55 | #if !defined(CONFIG_ENV_IS_IN_NVRAM) && \ |
Jean-Christophe PLAGNIOL-VILLARD | bb1f8b4 | 2008-09-05 09:19:30 +0200 | [diff] [blame] | 56 | !defined(CONFIG_ENV_IS_IN_EEPROM) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 5a1aceb | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 57 | !defined(CONFIG_ENV_IS_IN_FLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 057c849 | 2008-09-10 22:47:58 +0200 | [diff] [blame] | 58 | !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 51bfee1 | 2008-09-10 22:47:58 +0200 | [diff] [blame] | 59 | !defined(CONFIG_ENV_IS_IN_NAND) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 9656138 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 60 | !defined(CONFIG_ENV_IS_IN_ONENAND) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 0b5099a | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 61 | !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 93f6d72 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 62 | !defined(CONFIG_ENV_IS_NOWHERE) |
Jean-Christophe PLAGNIOL-VILLARD | 1ede787 | 2008-09-10 22:48:05 +0200 | [diff] [blame] | 63 | # error Define one of CONFIG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|ONENAND|SPI_FLASH|NOWHERE} |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
| 66 | #define XMK_STR(x) #x |
| 67 | #define MK_STR(x) XMK_STR(x) |
| 68 | |
| 69 | /************************************************************************ |
| 70 | ************************************************************************/ |
| 71 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 72 | /* |
| 73 | * Table with supported baudrates (defined in config_xyz.h) |
| 74 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 76 | #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0])) |
| 77 | |
| 78 | |
| 79 | /************************************************************************ |
| 80 | * Command interface: print one or all environment variables |
| 81 | */ |
| 82 | |
| 83 | int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 84 | { |
| 85 | int i, j, k, nxt; |
| 86 | int rcode = 0; |
| 87 | |
| 88 | if (argc == 1) { /* Print all env variables */ |
| 89 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 90 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) |
| 91 | ; |
| 92 | for (k=i; k<nxt; ++k) |
| 93 | putc(env_get_char(k)); |
| 94 | putc ('\n'); |
| 95 | |
| 96 | if (ctrlc()) { |
| 97 | puts ("\n ** Abort\n"); |
| 98 | return 1; |
| 99 | } |
| 100 | } |
| 101 | |
Wolfgang Denk | d5996dd | 2008-07-13 19:51:00 +0200 | [diff] [blame] | 102 | printf("\nEnvironment size: %d/%ld bytes\n", |
| 103 | i, (ulong)ENV_SIZE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | for (i=1; i<argc; ++i) { /* print single env variables */ |
| 109 | char *name = argv[i]; |
| 110 | |
| 111 | k = -1; |
| 112 | |
| 113 | for (j=0; env_get_char(j) != '\0'; j=nxt+1) { |
| 114 | |
| 115 | for (nxt=j; env_get_char(nxt) != '\0'; ++nxt) |
| 116 | ; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 117 | k = envmatch((uchar *)name, j); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 118 | if (k < 0) { |
| 119 | continue; |
| 120 | } |
| 121 | puts (name); |
| 122 | putc ('='); |
| 123 | while (k < nxt) |
| 124 | putc(env_get_char(k++)); |
| 125 | putc ('\n'); |
| 126 | break; |
| 127 | } |
| 128 | if (k < 0) { |
| 129 | printf ("## Error: \"%s\" not defined\n", name); |
| 130 | rcode ++; |
| 131 | } |
| 132 | } |
| 133 | return rcode; |
| 134 | } |
| 135 | |
| 136 | /************************************************************************ |
| 137 | * Set a new environment variable, |
| 138 | * or replace or delete an existing one. |
| 139 | * |
| 140 | * This function will ONLY work with a in-RAM copy of the environment |
| 141 | */ |
| 142 | |
| 143 | int _do_setenv (int flag, int argc, char *argv[]) |
| 144 | { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 145 | int i, len, oldval; |
| 146 | int console = -1; |
| 147 | uchar *env, *nxt = NULL; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 148 | char *name; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 149 | bd_t *bd = gd->bd; |
| 150 | |
| 151 | uchar *env_data = env_get_addr(0); |
| 152 | |
| 153 | if (!env_data) /* need copy in RAM */ |
| 154 | return 1; |
| 155 | |
| 156 | name = argv[1]; |
| 157 | |
Wolfgang Denk | 471a7be | 2006-10-28 01:14:32 +0200 | [diff] [blame] | 158 | if (strchr(name, '=')) { |
| 159 | printf ("## Error: illegal character '=' in variable name \"%s\"\n", name); |
| 160 | return 1; |
| 161 | } |
| 162 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 163 | /* |
| 164 | * search if variable with this name already exists |
| 165 | */ |
| 166 | oldval = -1; |
| 167 | for (env=env_data; *env; env=nxt+1) { |
| 168 | for (nxt=env; *nxt; ++nxt) |
| 169 | ; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 170 | if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Delete any existing definition |
| 176 | */ |
| 177 | if (oldval >= 0) { |
| 178 | #ifndef CONFIG_ENV_OVERWRITE |
| 179 | |
| 180 | /* |
stroese | 0587597 | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 181 | * Ethernet Address and serial# can be set only once, |
| 182 | * ver is readonly. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 183 | */ |
Steven A. Falco | f6a6955 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 184 | if ( |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 185 | #ifdef CONFIG_HAS_UID |
| 186 | /* Allow serial# forced overwrite with 0xdeaf4add flag */ |
Steven A. Falco | f6a6955 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 187 | ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) || |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 188 | #else |
Steven A. Falco | f6a6955 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 189 | (strcmp (name, "serial#") == 0) || |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 190 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 191 | ((strcmp (name, "ethaddr") == 0) |
| 192 | #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 193 | && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 194 | #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ |
| 195 | ) ) { |
| 196 | printf ("Can't overwrite \"%s\"\n", name); |
| 197 | return 1; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | /* Check for console redirection */ |
| 202 | if (strcmp(name,"stdin") == 0) { |
| 203 | console = stdin; |
| 204 | } else if (strcmp(name,"stdout") == 0) { |
| 205 | console = stdout; |
| 206 | } else if (strcmp(name,"stderr") == 0) { |
| 207 | console = stderr; |
| 208 | } |
| 209 | |
| 210 | if (console != -1) { |
| 211 | if (argc < 3) { /* Cannot delete it! */ |
| 212 | printf("Can't delete \"%s\"\n", name); |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | /* Try assigning specified device */ |
| 217 | if (console_assign (console, argv[2]) < 0) |
| 218 | return 1; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 219 | |
| 220 | #ifdef CONFIG_SERIAL_MULTI |
| 221 | if (serial_assign (argv[2]) < 0) |
| 222 | return 1; |
| 223 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Switch to new baudrate if new baudrate is supported |
| 228 | */ |
| 229 | if (strcmp(argv[1],"baudrate") == 0) { |
| 230 | int baudrate = simple_strtoul(argv[2], NULL, 10); |
| 231 | int i; |
| 232 | for (i=0; i<N_BAUDRATES; ++i) { |
| 233 | if (baudrate == baudrate_table[i]) |
| 234 | break; |
| 235 | } |
| 236 | if (i == N_BAUDRATES) { |
| 237 | printf ("## Baudrate %d bps not supported\n", |
| 238 | baudrate); |
| 239 | return 1; |
| 240 | } |
| 241 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 242 | baudrate); |
| 243 | udelay(50000); |
| 244 | gd->baudrate = baudrate; |
Bartlomiej Sieka | c84bad0 | 2006-12-20 00:29:43 +0100 | [diff] [blame] | 245 | #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2) |
wdenk | d0fb80c | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 246 | gd->bd->bi_baudrate = baudrate; |
| 247 | #endif |
| 248 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 249 | serial_setbrg (); |
| 250 | udelay(50000); |
| 251 | for (;;) { |
| 252 | if (getc() == '\r') |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (*++nxt == '\0') { |
| 258 | if (env > env_data) { |
| 259 | env--; |
| 260 | } else { |
| 261 | *env = '\0'; |
| 262 | } |
| 263 | } else { |
| 264 | for (;;) { |
| 265 | *env = *nxt++; |
| 266 | if ((*env == '\0') && (*nxt == '\0')) |
| 267 | break; |
| 268 | ++env; |
| 269 | } |
| 270 | } |
| 271 | *++env = '\0'; |
| 272 | } |
| 273 | |
| 274 | #ifdef CONFIG_NET_MULTI |
| 275 | if (strncmp(name, "eth", 3) == 0) { |
| 276 | char *end; |
| 277 | int num = simple_strtoul(name+3, &end, 10); |
| 278 | |
| 279 | if (strcmp(end, "addr") == 0) { |
| 280 | eth_set_enetaddr(num, argv[2]); |
| 281 | } |
| 282 | } |
| 283 | #endif |
| 284 | |
| 285 | |
| 286 | /* Delete only ? */ |
| 287 | if ((argc < 3) || argv[2] == NULL) { |
| 288 | env_crc_update (); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Append new definition at the end |
| 294 | */ |
| 295 | for (env=env_data; *env || *(env+1); ++env) |
| 296 | ; |
| 297 | if (env > env_data) |
| 298 | ++env; |
| 299 | /* |
| 300 | * Overflow when: |
| 301 | * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data) |
| 302 | */ |
| 303 | len = strlen(name) + 2; |
| 304 | /* add '=' for first arg, ' ' for all others */ |
| 305 | for (i=2; i<argc; ++i) { |
| 306 | len += strlen(argv[i]) + 1; |
| 307 | } |
| 308 | if (len > (&env_data[ENV_SIZE]-env)) { |
| 309 | printf ("## Error: environment overflow, \"%s\" deleted\n", name); |
| 310 | return 1; |
| 311 | } |
| 312 | while ((*env = *name++) != '\0') |
| 313 | env++; |
| 314 | for (i=2; i<argc; ++i) { |
| 315 | char *val = argv[i]; |
| 316 | |
| 317 | *env = (i==2) ? '=' : ' '; |
| 318 | while ((*++env = *val++) != '\0') |
| 319 | ; |
| 320 | } |
| 321 | |
| 322 | /* end is marked with double '\0' */ |
| 323 | *++env = '\0'; |
| 324 | |
| 325 | /* Update CRC */ |
| 326 | env_crc_update (); |
| 327 | |
| 328 | /* |
| 329 | * Some variables should be updated when the corresponding |
| 330 | * entry in the enviornment is changed |
| 331 | */ |
| 332 | |
| 333 | if (strcmp(argv[1],"ethaddr") == 0) { |
| 334 | char *s = argv[2]; /* always use only one arg */ |
| 335 | char *e; |
| 336 | for (i=0; i<6; ++i) { |
| 337 | bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0; |
| 338 | if (s) s = (*e) ? e+1 : e; |
| 339 | } |
| 340 | #ifdef CONFIG_NET_MULTI |
| 341 | eth_set_enetaddr(0, argv[2]); |
| 342 | #endif |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | if (strcmp(argv[1],"ipaddr") == 0) { |
| 347 | char *s = argv[2]; /* always use only one arg */ |
| 348 | char *e; |
| 349 | unsigned long addr; |
| 350 | bd->bi_ip_addr = 0; |
| 351 | for (addr=0, i=0; i<4; ++i) { |
| 352 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 353 | addr <<= 8; |
| 354 | addr |= (val & 0xFF); |
| 355 | if (s) s = (*e) ? e+1 : e; |
| 356 | } |
| 357 | bd->bi_ip_addr = htonl(addr); |
| 358 | return 0; |
| 359 | } |
| 360 | if (strcmp(argv[1],"loadaddr") == 0) { |
| 361 | load_addr = simple_strtoul(argv[2], NULL, 16); |
| 362 | return 0; |
| 363 | } |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 364 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 365 | if (strcmp(argv[1],"bootfile") == 0) { |
| 366 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 367 | return 0; |
| 368 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 369 | #endif |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 370 | |
stroese | 0587597 | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 371 | #ifdef CONFIG_AMIGAONEG3SE |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 372 | if (strcmp(argv[1], "vga_fg_color") == 0 || |
| 373 | strcmp(argv[1], "vga_bg_color") == 0 ) { |
| 374 | extern void video_set_color(unsigned char attr); |
| 375 | extern unsigned char video_get_attr(void); |
| 376 | |
| 377 | video_set_color(video_get_attr()); |
| 378 | return 0; |
| 379 | } |
| 380 | #endif /* CONFIG_AMIGAONEG3SE */ |
| 381 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Steven A. Falco | 75678c8 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 385 | int setenv (char *varname, char *varvalue) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 386 | { |
| 387 | char *argv[4] = { "setenv", varname, varvalue, NULL }; |
Jeffrey Mann | 9ffd451 | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 388 | if (varvalue == NULL) |
Steven A. Falco | 75678c8 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 389 | return _do_setenv (0, 2, argv); |
Jeffrey Mann | 9ffd451 | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 390 | else |
Steven A. Falco | 75678c8 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 391 | return _do_setenv (0, 3, argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 394 | #ifdef CONFIG_HAS_UID |
| 395 | void forceenv (char *varname, char *varvalue) |
| 396 | { |
| 397 | char *argv[4] = { "forceenv", varname, varvalue, NULL }; |
| 398 | _do_setenv (0xdeaf4add, 3, argv); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 403 | { |
| 404 | if (argc < 2) { |
| 405 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | return _do_setenv (flag, argc, argv); |
| 410 | } |
| 411 | |
| 412 | /************************************************************************ |
| 413 | * Prompt for environment variable |
| 414 | */ |
| 415 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 416 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 417 | int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 418 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 419 | extern char console_buffer[CONFIG_SYS_CBSIZE]; |
| 420 | char message[CONFIG_SYS_CBSIZE]; |
| 421 | int size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 422 | int len; |
| 423 | char *local_args[4]; |
| 424 | |
| 425 | local_args[0] = argv[0]; |
| 426 | local_args[1] = argv[1]; |
| 427 | local_args[2] = NULL; |
| 428 | local_args[3] = NULL; |
| 429 | |
| 430 | if (argc < 2) { |
| 431 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 432 | return 1; |
| 433 | } |
| 434 | /* Check the syntax */ |
| 435 | switch (argc) { |
| 436 | case 1: |
| 437 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 438 | return 1; |
| 439 | |
| 440 | case 2: /* askenv envname */ |
| 441 | sprintf (message, "Please enter '%s':", argv[1]); |
| 442 | break; |
| 443 | |
| 444 | case 3: /* askenv envname size */ |
| 445 | sprintf (message, "Please enter '%s':", argv[1]); |
| 446 | size = simple_strtoul (argv[2], NULL, 10); |
| 447 | break; |
| 448 | |
| 449 | default: /* askenv envname message1 ... messagen size */ |
| 450 | { |
| 451 | int i; |
| 452 | int pos = 0; |
| 453 | |
| 454 | for (i = 2; i < argc - 1; i++) { |
| 455 | if (pos) { |
| 456 | message[pos++] = ' '; |
| 457 | } |
| 458 | strcpy (message+pos, argv[i]); |
| 459 | pos += strlen(argv[i]); |
| 460 | } |
| 461 | message[pos] = '\0'; |
| 462 | size = simple_strtoul (argv[argc - 1], NULL, 10); |
| 463 | } |
| 464 | break; |
| 465 | } |
| 466 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 467 | if (size >= CONFIG_SYS_CBSIZE) |
| 468 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 469 | |
| 470 | if (size <= 0) |
| 471 | return 1; |
| 472 | |
| 473 | /* prompt for input */ |
| 474 | len = readline (message); |
| 475 | |
| 476 | if (size < len) |
| 477 | console_buffer[size] = '\0'; |
| 478 | |
| 479 | len = 2; |
| 480 | if (console_buffer[0] != '\0') { |
| 481 | local_args[2] = console_buffer; |
| 482 | len = 3; |
| 483 | } |
| 484 | |
| 485 | /* Continue calling setenv code */ |
| 486 | return _do_setenv (flag, len, local_args); |
| 487 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 488 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 489 | |
| 490 | /************************************************************************ |
| 491 | * Look up variable from environment, |
| 492 | * return address of storage for that variable, |
| 493 | * or NULL if not found |
| 494 | */ |
| 495 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 496 | char *getenv (char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 497 | { |
| 498 | int i, nxt; |
| 499 | |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 500 | WATCHDOG_RESET(); |
| 501 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 502 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 503 | int val; |
| 504 | |
| 505 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 506 | if (nxt >= CONFIG_ENV_SIZE) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 507 | return (NULL); |
| 508 | } |
| 509 | } |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 510 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 511 | continue; |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 512 | return ((char *)env_get_addr(val)); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return (NULL); |
| 516 | } |
| 517 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 518 | int getenv_r (char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 519 | { |
| 520 | int i, nxt; |
| 521 | |
| 522 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 523 | int val, n; |
| 524 | |
| 525 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 526 | if (nxt >= CONFIG_ENV_SIZE) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 527 | return (-1); |
| 528 | } |
| 529 | } |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 530 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 531 | continue; |
| 532 | /* found; copy out */ |
| 533 | n = 0; |
| 534 | while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0') |
| 535 | ; |
| 536 | if (len == n) |
| 537 | *buf = '\0'; |
| 538 | return (n); |
| 539 | } |
| 540 | return (-1); |
| 541 | } |
| 542 | |
Jean-Christophe PLAGNIOL-VILLARD | 9314cee | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 543 | #if ((defined(CONFIG_ENV_IS_IN_NVRAM) || defined(CONFIG_ENV_IS_IN_EEPROM) \ |
Jon Loeliger | 65c450b | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 544 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 545 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \ |
Jean-Christophe PLAGNIOL-VILLARD | 00b48a4 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 546 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND))) \ |
Jean-Christophe PLAGNIOL-VILLARD | 93f6d72 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 547 | && !defined(CONFIG_ENV_IS_NOWHERE)) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 548 | int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 549 | { |
| 550 | extern char * env_name_spec; |
| 551 | |
| 552 | printf ("Saving Environment to %s...\n", env_name_spec); |
| 553 | |
| 554 | return (saveenv() ? 1 : 0); |
| 555 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 556 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 557 | #endif |
| 558 | |
| 559 | |
| 560 | /************************************************************************ |
| 561 | * Match a name / name=value pair |
| 562 | * |
| 563 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 564 | * i2 is the environment index for a 'name2=value2' pair. |
| 565 | * If the names match, return the index for the value2, else NULL. |
| 566 | */ |
| 567 | |
Rafal Jaworowski | 26a4179 | 2008-01-09 18:05:27 +0100 | [diff] [blame] | 568 | int envmatch (uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 569 | { |
| 570 | |
| 571 | while (*s1 == env_get_char(i2++)) |
| 572 | if (*s1++ == '=') |
| 573 | return(i2); |
| 574 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
| 575 | return(i2); |
| 576 | return(-1); |
| 577 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 578 | |
| 579 | |
| 580 | /**************************************************/ |
| 581 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 582 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 583 | printenv, CONFIG_SYS_MAXARGS, 1, do_printenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 584 | "printenv- print environment variables\n", |
| 585 | "\n - print values of all environment variables\n" |
| 586 | "printenv name ...\n" |
| 587 | " - print value of environment variable 'name'\n" |
| 588 | ); |
| 589 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 590 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 591 | setenv, CONFIG_SYS_MAXARGS, 0, do_setenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 592 | "setenv - set environment variables\n", |
| 593 | "name value ...\n" |
| 594 | " - set environment variable 'name' to 'value ...'\n" |
| 595 | "setenv name\n" |
| 596 | " - delete environment variable 'name'\n" |
| 597 | ); |
| 598 | |
Jean-Christophe PLAGNIOL-VILLARD | 9314cee | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 599 | #if ((defined(CONFIG_ENV_IS_IN_NVRAM) || defined(CONFIG_ENV_IS_IN_EEPROM) \ |
Jon Loeliger | 65c450b | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 600 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \ |
Kyungmin Park | d7e8ce1 | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 601 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \ |
Jean-Christophe PLAGNIOL-VILLARD | 00b48a4 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 602 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND))) \ |
Jean-Christophe PLAGNIOL-VILLARD | 93f6d72 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 603 | && !defined(CONFIG_ENV_IS_NOWHERE)) |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 604 | U_BOOT_CMD( |
| 605 | saveenv, 1, 0, do_saveenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 606 | "saveenv - save environment variables to persistent storage\n", |
| 607 | NULL |
| 608 | ); |
| 609 | |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 610 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 611 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 612 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 613 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 614 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 615 | askenv, CONFIG_SYS_MAXARGS, 1, do_askenv, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 616 | "askenv - get environment variables from stdin\n", |
| 617 | "name [message] [size]\n" |
| 618 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 619 | "askenv name\n" |
| 620 | " - get environment variable 'name' from stdin\n" |
| 621 | "askenv name size\n" |
| 622 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 623 | "askenv name [message] size\n" |
| 624 | " - display 'message' string and get environment variable 'name'" |
| 625 | "from stdin (max 'size' chars)\n" |
| 626 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 627 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 628 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 629 | #if defined(CONFIG_CMD_RUN) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 630 | int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 631 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 632 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 633 | "run - run commands in an environment variable\n", |
| 634 | "var [...]\n" |
| 635 | " - run the commands in the environment variable(s) 'var'\n" |
| 636 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 637 | #endif |