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 | 7ac1610 | 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 | d76b5c1 | 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 | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
Jean-Christophe PLAGNIOL-VILLARD | fdb79c3 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 55 | #if !defined(CONFIG_ENV_IS_IN_NVRAM) && \ |
Jean-Christophe PLAGNIOL-VILLARD | e46af64 | 2008-09-05 09:19:30 +0200 | [diff] [blame] | 56 | !defined(CONFIG_ENV_IS_IN_EEPROM) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 53db4cd | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 57 | !defined(CONFIG_ENV_IS_IN_FLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 2b14d2b | 2008-09-10 22:47:58 +0200 | [diff] [blame] | 58 | !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | dda84dd | 2008-09-10 22:47:58 +0200 | [diff] [blame] | 59 | !defined(CONFIG_ENV_IS_IN_NAND) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 7598916 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 60 | !defined(CONFIG_ENV_IS_IN_ONENAND) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 4539b1c | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 61 | !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 68a8756 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 62 | !defined(CONFIG_ENV_IS_NOWHERE) |
Jean-Christophe PLAGNIOL-VILLARD | f80353b | 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 | 0383694 | 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 | fe036dd | 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 | 7fb5266 | 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 | 7fb5266 | 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 | bc50f8c | 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 | 7fb5266 | 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 | f8105cf | 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 | 2a1d4e4 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 184 | if ( |
Sergey Kubushyn | e8f3912 | 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 | 2a1d4e4 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 187 | ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) || |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 188 | #else |
Steven A. Falco | 2a1d4e4 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 189 | (strcmp (name, "serial#") == 0) || |
Sergey Kubushyn | e8f3912 | 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 | 7fb5266 | 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 | |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 216 | #ifdef CONFIG_CONSOLE_MUX |
| 217 | i = iomux_doenv(console, argv[2]); |
| 218 | if (i) |
| 219 | return i; |
| 220 | #else |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 221 | /* Try assigning specified device */ |
| 222 | if (console_assign (console, argv[2]) < 0) |
| 223 | return 1; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 224 | |
| 225 | #ifdef CONFIG_SERIAL_MULTI |
| 226 | if (serial_assign (argv[2]) < 0) |
| 227 | return 1; |
| 228 | #endif |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 229 | #endif /* CONFIG_CONSOLE_MUX */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Switch to new baudrate if new baudrate is supported |
| 234 | */ |
| 235 | if (strcmp(argv[1],"baudrate") == 0) { |
| 236 | int baudrate = simple_strtoul(argv[2], NULL, 10); |
| 237 | int i; |
| 238 | for (i=0; i<N_BAUDRATES; ++i) { |
| 239 | if (baudrate == baudrate_table[i]) |
| 240 | break; |
| 241 | } |
| 242 | if (i == N_BAUDRATES) { |
| 243 | printf ("## Baudrate %d bps not supported\n", |
| 244 | baudrate); |
| 245 | return 1; |
| 246 | } |
| 247 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 248 | baudrate); |
| 249 | udelay(50000); |
| 250 | gd->baudrate = baudrate; |
Bartlomiej Sieka | 23d4328 | 2006-12-20 00:29:43 +0100 | [diff] [blame] | 251 | #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 252 | gd->bd->bi_baudrate = baudrate; |
| 253 | #endif |
| 254 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 255 | serial_setbrg (); |
| 256 | udelay(50000); |
| 257 | for (;;) { |
| 258 | if (getc() == '\r') |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (*++nxt == '\0') { |
| 264 | if (env > env_data) { |
| 265 | env--; |
| 266 | } else { |
| 267 | *env = '\0'; |
| 268 | } |
| 269 | } else { |
| 270 | for (;;) { |
| 271 | *env = *nxt++; |
| 272 | if ((*env == '\0') && (*nxt == '\0')) |
| 273 | break; |
| 274 | ++env; |
| 275 | } |
| 276 | } |
| 277 | *++env = '\0'; |
| 278 | } |
| 279 | |
| 280 | #ifdef CONFIG_NET_MULTI |
| 281 | if (strncmp(name, "eth", 3) == 0) { |
| 282 | char *end; |
| 283 | int num = simple_strtoul(name+3, &end, 10); |
| 284 | |
| 285 | if (strcmp(end, "addr") == 0) { |
| 286 | eth_set_enetaddr(num, argv[2]); |
| 287 | } |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | |
| 292 | /* Delete only ? */ |
| 293 | if ((argc < 3) || argv[2] == NULL) { |
| 294 | env_crc_update (); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Append new definition at the end |
| 300 | */ |
| 301 | for (env=env_data; *env || *(env+1); ++env) |
| 302 | ; |
| 303 | if (env > env_data) |
| 304 | ++env; |
| 305 | /* |
| 306 | * Overflow when: |
| 307 | * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data) |
| 308 | */ |
| 309 | len = strlen(name) + 2; |
| 310 | /* add '=' for first arg, ' ' for all others */ |
| 311 | for (i=2; i<argc; ++i) { |
| 312 | len += strlen(argv[i]) + 1; |
| 313 | } |
| 314 | if (len > (&env_data[ENV_SIZE]-env)) { |
| 315 | printf ("## Error: environment overflow, \"%s\" deleted\n", name); |
| 316 | return 1; |
| 317 | } |
| 318 | while ((*env = *name++) != '\0') |
| 319 | env++; |
| 320 | for (i=2; i<argc; ++i) { |
| 321 | char *val = argv[i]; |
| 322 | |
| 323 | *env = (i==2) ? '=' : ' '; |
| 324 | while ((*++env = *val++) != '\0') |
| 325 | ; |
| 326 | } |
| 327 | |
| 328 | /* end is marked with double '\0' */ |
| 329 | *++env = '\0'; |
| 330 | |
| 331 | /* Update CRC */ |
| 332 | env_crc_update (); |
| 333 | |
| 334 | /* |
| 335 | * Some variables should be updated when the corresponding |
| 336 | * entry in the enviornment is changed |
| 337 | */ |
| 338 | |
| 339 | if (strcmp(argv[1],"ethaddr") == 0) { |
| 340 | char *s = argv[2]; /* always use only one arg */ |
| 341 | char *e; |
| 342 | for (i=0; i<6; ++i) { |
| 343 | bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0; |
| 344 | if (s) s = (*e) ? e+1 : e; |
| 345 | } |
| 346 | #ifdef CONFIG_NET_MULTI |
| 347 | eth_set_enetaddr(0, argv[2]); |
| 348 | #endif |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | if (strcmp(argv[1],"ipaddr") == 0) { |
| 353 | char *s = argv[2]; /* always use only one arg */ |
| 354 | char *e; |
| 355 | unsigned long addr; |
| 356 | bd->bi_ip_addr = 0; |
| 357 | for (addr=0, i=0; i<4; ++i) { |
| 358 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 359 | addr <<= 8; |
| 360 | addr |= (val & 0xFF); |
| 361 | if (s) s = (*e) ? e+1 : e; |
| 362 | } |
| 363 | bd->bi_ip_addr = htonl(addr); |
| 364 | return 0; |
| 365 | } |
| 366 | if (strcmp(argv[1],"loadaddr") == 0) { |
| 367 | load_addr = simple_strtoul(argv[2], NULL, 16); |
| 368 | return 0; |
| 369 | } |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 370 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 371 | if (strcmp(argv[1],"bootfile") == 0) { |
| 372 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 373 | return 0; |
| 374 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 375 | #endif |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 376 | |
stroese | f8105cf | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 377 | #ifdef CONFIG_AMIGAONEG3SE |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 378 | if (strcmp(argv[1], "vga_fg_color") == 0 || |
| 379 | strcmp(argv[1], "vga_bg_color") == 0 ) { |
| 380 | extern void video_set_color(unsigned char attr); |
| 381 | extern unsigned char video_get_attr(void); |
| 382 | |
| 383 | video_set_color(video_get_attr()); |
| 384 | return 0; |
| 385 | } |
| 386 | #endif /* CONFIG_AMIGAONEG3SE */ |
| 387 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Steven A. Falco | 1ce6f99 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 391 | int setenv (char *varname, char *varvalue) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 392 | { |
| 393 | char *argv[4] = { "setenv", varname, varvalue, NULL }; |
Jeffrey Mann | 62b896f | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 394 | if (varvalue == NULL) |
Steven A. Falco | 1ce6f99 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 395 | return _do_setenv (0, 2, argv); |
Jeffrey Mann | 62b896f | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 396 | else |
Steven A. Falco | 1ce6f99 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 397 | return _do_setenv (0, 3, argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 400 | #ifdef CONFIG_HAS_UID |
| 401 | void forceenv (char *varname, char *varvalue) |
| 402 | { |
| 403 | char *argv[4] = { "forceenv", varname, varvalue, NULL }; |
| 404 | _do_setenv (0xdeaf4add, 3, argv); |
| 405 | } |
| 406 | #endif |
| 407 | |
| 408 | int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 409 | { |
| 410 | if (argc < 2) { |
| 411 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | return _do_setenv (flag, argc, argv); |
| 416 | } |
| 417 | |
| 418 | /************************************************************************ |
| 419 | * Prompt for environment variable |
| 420 | */ |
| 421 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 422 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 423 | int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 424 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 425 | extern char console_buffer[CONFIG_SYS_CBSIZE]; |
| 426 | char message[CONFIG_SYS_CBSIZE]; |
| 427 | int size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 428 | int len; |
| 429 | char *local_args[4]; |
| 430 | |
| 431 | local_args[0] = argv[0]; |
| 432 | local_args[1] = argv[1]; |
| 433 | local_args[2] = NULL; |
| 434 | local_args[3] = NULL; |
| 435 | |
| 436 | if (argc < 2) { |
| 437 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 438 | return 1; |
| 439 | } |
| 440 | /* Check the syntax */ |
| 441 | switch (argc) { |
| 442 | case 1: |
| 443 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 444 | return 1; |
| 445 | |
| 446 | case 2: /* askenv envname */ |
| 447 | sprintf (message, "Please enter '%s':", argv[1]); |
| 448 | break; |
| 449 | |
| 450 | case 3: /* askenv envname size */ |
| 451 | sprintf (message, "Please enter '%s':", argv[1]); |
| 452 | size = simple_strtoul (argv[2], NULL, 10); |
| 453 | break; |
| 454 | |
| 455 | default: /* askenv envname message1 ... messagen size */ |
| 456 | { |
| 457 | int i; |
| 458 | int pos = 0; |
| 459 | |
| 460 | for (i = 2; i < argc - 1; i++) { |
| 461 | if (pos) { |
| 462 | message[pos++] = ' '; |
| 463 | } |
| 464 | strcpy (message+pos, argv[i]); |
| 465 | pos += strlen(argv[i]); |
| 466 | } |
| 467 | message[pos] = '\0'; |
| 468 | size = simple_strtoul (argv[argc - 1], NULL, 10); |
| 469 | } |
| 470 | break; |
| 471 | } |
| 472 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 473 | if (size >= CONFIG_SYS_CBSIZE) |
| 474 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 475 | |
| 476 | if (size <= 0) |
| 477 | return 1; |
| 478 | |
| 479 | /* prompt for input */ |
| 480 | len = readline (message); |
| 481 | |
| 482 | if (size < len) |
| 483 | console_buffer[size] = '\0'; |
| 484 | |
| 485 | len = 2; |
| 486 | if (console_buffer[0] != '\0') { |
| 487 | local_args[2] = console_buffer; |
| 488 | len = 3; |
| 489 | } |
| 490 | |
| 491 | /* Continue calling setenv code */ |
| 492 | return _do_setenv (flag, len, local_args); |
| 493 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 494 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 495 | |
| 496 | /************************************************************************ |
| 497 | * Look up variable from environment, |
| 498 | * return address of storage for that variable, |
| 499 | * or NULL if not found |
| 500 | */ |
| 501 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 502 | char *getenv (char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 503 | { |
| 504 | int i, nxt; |
| 505 | |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 506 | WATCHDOG_RESET(); |
| 507 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 508 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 509 | int val; |
| 510 | |
| 511 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 512 | if (nxt >= CONFIG_ENV_SIZE) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 513 | return (NULL); |
| 514 | } |
| 515 | } |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 516 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 517 | continue; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 518 | return ((char *)env_get_addr(val)); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | return (NULL); |
| 522 | } |
| 523 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 524 | int getenv_r (char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 525 | { |
| 526 | int i, nxt; |
| 527 | |
| 528 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 529 | int val, n; |
| 530 | |
| 531 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 532 | if (nxt >= CONFIG_ENV_SIZE) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 533 | return (-1); |
| 534 | } |
| 535 | } |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 536 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 537 | continue; |
| 538 | /* found; copy out */ |
| 539 | n = 0; |
| 540 | while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0') |
| 541 | ; |
| 542 | if (len == n) |
| 543 | *buf = '\0'; |
| 544 | return (n); |
| 545 | } |
| 546 | return (-1); |
| 547 | } |
| 548 | |
Jean-Christophe PLAGNIOL-VILLARD | fdb79c3 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 549 | #if ((defined(CONFIG_ENV_IS_IN_NVRAM) || defined(CONFIG_ENV_IS_IN_EEPROM) \ |
Jon Loeliger | 3de8b24 | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 550 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 551 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \ |
Jean-Christophe PLAGNIOL-VILLARD | 43ad623 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 552 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND))) \ |
Jean-Christophe PLAGNIOL-VILLARD | 68a8756 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 553 | && !defined(CONFIG_ENV_IS_NOWHERE)) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 554 | int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 555 | { |
| 556 | extern char * env_name_spec; |
| 557 | |
| 558 | printf ("Saving Environment to %s...\n", env_name_spec); |
| 559 | |
| 560 | return (saveenv() ? 1 : 0); |
| 561 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 562 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 563 | #endif |
| 564 | |
| 565 | |
| 566 | /************************************************************************ |
| 567 | * Match a name / name=value pair |
| 568 | * |
| 569 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 570 | * i2 is the environment index for a 'name2=value2' pair. |
| 571 | * If the names match, return the index for the value2, else NULL. |
| 572 | */ |
| 573 | |
Rafal Jaworowski | 75b5442 | 2008-01-09 18:05:27 +0100 | [diff] [blame] | 574 | int envmatch (uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 575 | { |
| 576 | |
| 577 | while (*s1 == env_get_char(i2++)) |
| 578 | if (*s1++ == '=') |
| 579 | return(i2); |
| 580 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
| 581 | return(i2); |
| 582 | return(-1); |
| 583 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 584 | |
| 585 | |
| 586 | /**************************************************/ |
| 587 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 588 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 589 | printenv, CONFIG_SYS_MAXARGS, 1, do_printenv, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 590 | "printenv- print environment variables\n", |
| 591 | "\n - print values of all environment variables\n" |
| 592 | "printenv name ...\n" |
| 593 | " - print value of environment variable 'name'\n" |
| 594 | ); |
| 595 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 596 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 597 | setenv, CONFIG_SYS_MAXARGS, 0, do_setenv, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 598 | "setenv - set environment variables\n", |
| 599 | "name value ...\n" |
| 600 | " - set environment variable 'name' to 'value ...'\n" |
| 601 | "setenv name\n" |
| 602 | " - delete environment variable 'name'\n" |
| 603 | ); |
| 604 | |
Jean-Christophe PLAGNIOL-VILLARD | fdb79c3 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 605 | #if ((defined(CONFIG_ENV_IS_IN_NVRAM) || defined(CONFIG_ENV_IS_IN_EEPROM) \ |
Jon Loeliger | 3de8b24 | 2007-06-11 19:01:54 -0500 | [diff] [blame] | 606 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 607 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \ |
Jean-Christophe PLAGNIOL-VILLARD | 43ad623 | 2008-02-23 12:15:56 +0100 | [diff] [blame] | 608 | || (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND))) \ |
Jean-Christophe PLAGNIOL-VILLARD | 68a8756 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 609 | && !defined(CONFIG_ENV_IS_NOWHERE)) |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 610 | U_BOOT_CMD( |
| 611 | saveenv, 1, 0, do_saveenv, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 612 | "saveenv - save environment variables to persistent storage\n", |
| 613 | NULL |
| 614 | ); |
| 615 | |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 616 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 617 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 618 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 619 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 620 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 621 | askenv, CONFIG_SYS_MAXARGS, 1, do_askenv, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 622 | "askenv - get environment variables from stdin\n", |
| 623 | "name [message] [size]\n" |
| 624 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 625 | "askenv name\n" |
| 626 | " - get environment variable 'name' from stdin\n" |
| 627 | "askenv name size\n" |
| 628 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 629 | "askenv name [message] size\n" |
| 630 | " - display 'message' string and get environment variable 'name'" |
| 631 | "from stdin (max 'size' chars)\n" |
| 632 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 633 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 634 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 635 | #if defined(CONFIG_CMD_RUN) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 636 | int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 637 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 638 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 639 | "run - run commands in an environment variable\n", |
| 640 | "var [...]\n" |
| 641 | " - run the commands in the environment variable(s) 'var'\n" |
| 642 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 643 | #endif |