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 | |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 55 | #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 53db4cd | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 56 | !defined(CONFIG_ENV_IS_IN_FLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 2b14d2b | 2008-09-10 22:47:58 +0200 | [diff] [blame] | 57 | !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \ |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 58 | !defined(CONFIG_ENV_IS_IN_MG_DISK) && \ |
Jean-Christophe PLAGNIOL-VILLARD | dda84dd | 2008-09-10 22:47:58 +0200 | [diff] [blame] | 59 | !defined(CONFIG_ENV_IS_IN_NAND) && \ |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 60 | !defined(CONFIG_ENV_IS_IN_NVRAM) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 7598916 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 61 | !defined(CONFIG_ENV_IS_IN_ONENAND) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 4539b1c | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 62 | !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ |
Jean-Christophe PLAGNIOL-VILLARD | 68a8756 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 63 | !defined(CONFIG_ENV_IS_NOWHERE) |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 64 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\ |
| 65 | SPI_FLASH|MG_DISK|NVRAM|NOWHERE} |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #define XMK_STR(x) #x |
| 69 | #define MK_STR(x) XMK_STR(x) |
| 70 | |
| 71 | /************************************************************************ |
| 72 | ************************************************************************/ |
| 73 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Table with supported baudrates (defined in config_xyz.h) |
| 76 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 77 | static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 78 | #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0])) |
| 79 | |
Heiko Schocher | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 80 | /* |
| 81 | * This variable is incremented on each do_setenv (), so it can |
| 82 | * be used via get_env_id() as an indication, if the environment |
| 83 | * has changed or not. So it is possible to reread an environment |
| 84 | * variable only if the environment was changed ... done so for |
| 85 | * example in NetInitLoop() |
| 86 | */ |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 87 | static int env_id = 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 88 | |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 89 | int get_env_id (void) |
| 90 | { |
| 91 | return env_id; |
| 92 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 93 | /************************************************************************ |
| 94 | * Command interface: print one or all environment variables |
| 95 | */ |
| 96 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 97 | /* |
| 98 | * state 0: finish printing this string and return (matched!) |
| 99 | * state 1: no matching to be done; print everything |
| 100 | * state 2: continue searching for matched name |
| 101 | */ |
| 102 | static int printenv(char *name, int state) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 103 | { |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 104 | int i, j; |
| 105 | char c, buf[17]; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 106 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 107 | i = 0; |
| 108 | buf[16] = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 109 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 110 | while (state && env_get_char(i) != '\0') { |
| 111 | if (state == 2 && envmatch((uchar *)name, i) >= 0) |
| 112 | state = 0; |
| 113 | |
| 114 | j = 0; |
| 115 | do { |
| 116 | buf[j++] = c = env_get_char(i++); |
| 117 | if (j == sizeof(buf) - 1) { |
| 118 | if (state <= 1) |
| 119 | puts(buf); |
| 120 | j = 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 121 | } |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 122 | } while (c != '\0'); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 123 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 124 | if (state <= 1) { |
| 125 | if (j) |
| 126 | puts(buf); |
| 127 | putc('\n'); |
| 128 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 129 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 130 | if (ctrlc()) |
| 131 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 134 | if (state == 0) |
| 135 | i = 0; |
| 136 | return i; |
| 137 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 138 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 139 | int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 140 | { |
| 141 | int i; |
| 142 | int rcode = 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 143 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 144 | if (argc == 1) { |
| 145 | /* print all env vars */ |
| 146 | rcode = printenv(NULL, 1); |
| 147 | if (rcode < 0) |
| 148 | return 1; |
| 149 | printf("\nEnvironment size: %d/%ld bytes\n", |
| 150 | rcode, (ulong)ENV_SIZE); |
| 151 | return 0; |
| 152 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 153 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 154 | /* print selected env vars */ |
| 155 | for (i = 1; i < argc; ++i) { |
| 156 | char *name = argv[i]; |
| 157 | if (printenv(name, 2)) { |
| 158 | printf("## Error: \"%s\" not defined\n", name); |
| 159 | ++rcode; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 162 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 163 | return rcode; |
| 164 | } |
| 165 | |
| 166 | /************************************************************************ |
| 167 | * Set a new environment variable, |
| 168 | * or replace or delete an existing one. |
| 169 | * |
| 170 | * This function will ONLY work with a in-RAM copy of the environment |
| 171 | */ |
| 172 | |
| 173 | int _do_setenv (int flag, int argc, char *argv[]) |
| 174 | { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 175 | int i, len, oldval; |
| 176 | int console = -1; |
| 177 | uchar *env, *nxt = NULL; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 178 | char *name; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 179 | bd_t *bd = gd->bd; |
| 180 | |
| 181 | uchar *env_data = env_get_addr(0); |
| 182 | |
| 183 | if (!env_data) /* need copy in RAM */ |
| 184 | return 1; |
| 185 | |
| 186 | name = argv[1]; |
| 187 | |
Wolfgang Denk | bc50f8c | 2006-10-28 01:14:32 +0200 | [diff] [blame] | 188 | if (strchr(name, '=')) { |
| 189 | printf ("## Error: illegal character '=' in variable name \"%s\"\n", name); |
| 190 | return 1; |
| 191 | } |
| 192 | |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 193 | env_id++; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 194 | /* |
| 195 | * search if variable with this name already exists |
| 196 | */ |
| 197 | oldval = -1; |
| 198 | for (env=env_data; *env; env=nxt+1) { |
| 199 | for (nxt=env; *nxt; ++nxt) |
| 200 | ; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 201 | if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 205 | /* Check for console redirection */ |
| 206 | if (strcmp(name,"stdin") == 0) { |
| 207 | console = stdin; |
| 208 | } else if (strcmp(name,"stdout") == 0) { |
| 209 | console = stdout; |
| 210 | } else if (strcmp(name,"stderr") == 0) { |
| 211 | console = stderr; |
| 212 | } |
| 213 | |
| 214 | if (console != -1) { |
| 215 | if (argc < 3) { /* Cannot delete it! */ |
| 216 | printf("Can't delete \"%s\"\n", name); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | #ifdef CONFIG_CONSOLE_MUX |
| 221 | i = iomux_doenv(console, argv[2]); |
| 222 | if (i) |
| 223 | return i; |
| 224 | #else |
| 225 | /* Try assigning specified device */ |
| 226 | if (console_assign (console, argv[2]) < 0) |
| 227 | return 1; |
| 228 | |
| 229 | #ifdef CONFIG_SERIAL_MULTI |
| 230 | if (serial_assign (argv[2]) < 0) |
| 231 | return 1; |
| 232 | #endif |
| 233 | #endif /* CONFIG_CONSOLE_MUX */ |
| 234 | } |
| 235 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 236 | /* |
| 237 | * Delete any existing definition |
| 238 | */ |
| 239 | if (oldval >= 0) { |
| 240 | #ifndef CONFIG_ENV_OVERWRITE |
| 241 | |
| 242 | /* |
stroese | f8105cf | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 243 | * Ethernet Address and serial# can be set only once, |
| 244 | * ver is readonly. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 245 | */ |
Steven A. Falco | 2a1d4e4 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 246 | if ( |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 247 | #ifdef CONFIG_HAS_UID |
| 248 | /* Allow serial# forced overwrite with 0xdeaf4add flag */ |
Steven A. Falco | 2a1d4e4 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 249 | ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) || |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 250 | #else |
Steven A. Falco | 2a1d4e4 | 2008-06-12 13:24:42 -0400 | [diff] [blame] | 251 | (strcmp (name, "serial#") == 0) || |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 252 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 253 | ((strcmp (name, "ethaddr") == 0) |
| 254 | #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 255 | && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 256 | #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ |
| 257 | ) ) { |
| 258 | printf ("Can't overwrite \"%s\"\n", name); |
| 259 | return 1; |
| 260 | } |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 261 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Switch to new baudrate if new baudrate is supported |
| 265 | */ |
| 266 | if (strcmp(argv[1],"baudrate") == 0) { |
| 267 | int baudrate = simple_strtoul(argv[2], NULL, 10); |
| 268 | int i; |
| 269 | for (i=0; i<N_BAUDRATES; ++i) { |
| 270 | if (baudrate == baudrate_table[i]) |
| 271 | break; |
| 272 | } |
| 273 | if (i == N_BAUDRATES) { |
| 274 | printf ("## Baudrate %d bps not supported\n", |
| 275 | baudrate); |
| 276 | return 1; |
| 277 | } |
| 278 | printf ("## Switch baudrate to %d bps and press ENTER ...\n", |
| 279 | baudrate); |
| 280 | udelay(50000); |
| 281 | gd->baudrate = baudrate; |
Bartlomiej Sieka | 23d4328 | 2006-12-20 00:29:43 +0100 | [diff] [blame] | 282 | #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 283 | gd->bd->bi_baudrate = baudrate; |
| 284 | #endif |
| 285 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 286 | serial_setbrg (); |
| 287 | udelay(50000); |
| 288 | for (;;) { |
| 289 | if (getc() == '\r') |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (*++nxt == '\0') { |
| 295 | if (env > env_data) { |
| 296 | env--; |
| 297 | } else { |
| 298 | *env = '\0'; |
| 299 | } |
| 300 | } else { |
| 301 | for (;;) { |
| 302 | *env = *nxt++; |
| 303 | if ((*env == '\0') && (*nxt == '\0')) |
| 304 | break; |
| 305 | ++env; |
| 306 | } |
| 307 | } |
| 308 | *++env = '\0'; |
| 309 | } |
| 310 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 311 | /* Delete only ? */ |
| 312 | if ((argc < 3) || argv[2] == NULL) { |
| 313 | env_crc_update (); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Append new definition at the end |
| 319 | */ |
| 320 | for (env=env_data; *env || *(env+1); ++env) |
| 321 | ; |
| 322 | if (env > env_data) |
| 323 | ++env; |
| 324 | /* |
| 325 | * Overflow when: |
| 326 | * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data) |
| 327 | */ |
| 328 | len = strlen(name) + 2; |
| 329 | /* add '=' for first arg, ' ' for all others */ |
| 330 | for (i=2; i<argc; ++i) { |
| 331 | len += strlen(argv[i]) + 1; |
| 332 | } |
| 333 | if (len > (&env_data[ENV_SIZE]-env)) { |
| 334 | printf ("## Error: environment overflow, \"%s\" deleted\n", name); |
| 335 | return 1; |
| 336 | } |
| 337 | while ((*env = *name++) != '\0') |
| 338 | env++; |
| 339 | for (i=2; i<argc; ++i) { |
| 340 | char *val = argv[i]; |
| 341 | |
| 342 | *env = (i==2) ? '=' : ' '; |
| 343 | while ((*++env = *val++) != '\0') |
| 344 | ; |
| 345 | } |
| 346 | |
| 347 | /* end is marked with double '\0' */ |
| 348 | *++env = '\0'; |
| 349 | |
| 350 | /* Update CRC */ |
| 351 | env_crc_update (); |
| 352 | |
| 353 | /* |
| 354 | * Some variables should be updated when the corresponding |
| 355 | * entry in the enviornment is changed |
| 356 | */ |
| 357 | |
Mike Frysinger | c3c533c | 2009-02-11 18:52:38 -0500 | [diff] [blame] | 358 | if (strcmp(argv[1],"ethaddr") == 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 359 | return 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 360 | |
| 361 | if (strcmp(argv[1],"ipaddr") == 0) { |
| 362 | char *s = argv[2]; /* always use only one arg */ |
| 363 | char *e; |
| 364 | unsigned long addr; |
| 365 | bd->bi_ip_addr = 0; |
| 366 | for (addr=0, i=0; i<4; ++i) { |
| 367 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 368 | addr <<= 8; |
| 369 | addr |= (val & 0xFF); |
| 370 | if (s) s = (*e) ? e+1 : e; |
| 371 | } |
| 372 | bd->bi_ip_addr = htonl(addr); |
| 373 | return 0; |
| 374 | } |
| 375 | if (strcmp(argv[1],"loadaddr") == 0) { |
| 376 | load_addr = simple_strtoul(argv[2], NULL, 16); |
| 377 | return 0; |
| 378 | } |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 379 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 380 | if (strcmp(argv[1],"bootfile") == 0) { |
| 381 | copy_filename (BootFile, argv[2], sizeof(BootFile)); |
| 382 | return 0; |
| 383 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 384 | #endif |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 385 | |
stroese | f8105cf | 2003-04-04 15:44:49 +0000 | [diff] [blame] | 386 | #ifdef CONFIG_AMIGAONEG3SE |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 387 | if (strcmp(argv[1], "vga_fg_color") == 0 || |
| 388 | strcmp(argv[1], "vga_bg_color") == 0 ) { |
| 389 | extern void video_set_color(unsigned char attr); |
| 390 | extern unsigned char video_get_attr(void); |
| 391 | |
| 392 | video_set_color(video_get_attr()); |
| 393 | return 0; |
| 394 | } |
| 395 | #endif /* CONFIG_AMIGAONEG3SE */ |
| 396 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Steven A. Falco | 1ce6f99 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 400 | int setenv (char *varname, char *varvalue) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 401 | { |
| 402 | char *argv[4] = { "setenv", varname, varvalue, NULL }; |
Jeffrey Mann | 62b896f | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 403 | if (varvalue == NULL) |
Steven A. Falco | 1ce6f99 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 404 | return _do_setenv (0, 2, argv); |
Jeffrey Mann | 62b896f | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 405 | else |
Steven A. Falco | 1ce6f99 | 2008-06-12 13:22:12 -0400 | [diff] [blame] | 406 | return _do_setenv (0, 3, argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 409 | #ifdef CONFIG_HAS_UID |
| 410 | void forceenv (char *varname, char *varvalue) |
| 411 | { |
| 412 | char *argv[4] = { "forceenv", varname, varvalue, NULL }; |
| 413 | _do_setenv (0xdeaf4add, 3, argv); |
| 414 | } |
| 415 | #endif |
| 416 | |
| 417 | int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 418 | { |
| 419 | if (argc < 2) { |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 420 | cmd_usage(cmdtp); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | return _do_setenv (flag, argc, argv); |
| 425 | } |
| 426 | |
| 427 | /************************************************************************ |
| 428 | * Prompt for environment variable |
| 429 | */ |
| 430 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 431 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 432 | int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 433 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 434 | extern char console_buffer[CONFIG_SYS_CBSIZE]; |
| 435 | char message[CONFIG_SYS_CBSIZE]; |
| 436 | int size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 437 | int len; |
| 438 | char *local_args[4]; |
| 439 | |
| 440 | local_args[0] = argv[0]; |
| 441 | local_args[1] = argv[1]; |
| 442 | local_args[2] = NULL; |
| 443 | local_args[3] = NULL; |
| 444 | |
| 445 | if (argc < 2) { |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 446 | cmd_usage(cmdtp); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 447 | return 1; |
| 448 | } |
| 449 | /* Check the syntax */ |
| 450 | switch (argc) { |
| 451 | case 1: |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 452 | cmd_usage(cmdtp); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 453 | return 1; |
| 454 | |
| 455 | case 2: /* askenv envname */ |
| 456 | sprintf (message, "Please enter '%s':", argv[1]); |
| 457 | break; |
| 458 | |
| 459 | case 3: /* askenv envname size */ |
| 460 | sprintf (message, "Please enter '%s':", argv[1]); |
| 461 | size = simple_strtoul (argv[2], NULL, 10); |
| 462 | break; |
| 463 | |
| 464 | default: /* askenv envname message1 ... messagen size */ |
| 465 | { |
| 466 | int i; |
| 467 | int pos = 0; |
| 468 | |
| 469 | for (i = 2; i < argc - 1; i++) { |
| 470 | if (pos) { |
| 471 | message[pos++] = ' '; |
| 472 | } |
| 473 | strcpy (message+pos, argv[i]); |
| 474 | pos += strlen(argv[i]); |
| 475 | } |
| 476 | message[pos] = '\0'; |
| 477 | size = simple_strtoul (argv[argc - 1], NULL, 10); |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 482 | if (size >= CONFIG_SYS_CBSIZE) |
| 483 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 484 | |
| 485 | if (size <= 0) |
| 486 | return 1; |
| 487 | |
| 488 | /* prompt for input */ |
| 489 | len = readline (message); |
| 490 | |
| 491 | if (size < len) |
| 492 | console_buffer[size] = '\0'; |
| 493 | |
| 494 | len = 2; |
| 495 | if (console_buffer[0] != '\0') { |
| 496 | local_args[2] = console_buffer; |
| 497 | len = 3; |
| 498 | } |
| 499 | |
| 500 | /* Continue calling setenv code */ |
| 501 | return _do_setenv (flag, len, local_args); |
| 502 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 503 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 504 | |
| 505 | /************************************************************************ |
| 506 | * Look up variable from environment, |
| 507 | * return address of storage for that variable, |
| 508 | * or NULL if not found |
| 509 | */ |
| 510 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 511 | char *getenv (char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 512 | { |
| 513 | int i, nxt; |
| 514 | |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 515 | WATCHDOG_RESET(); |
| 516 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 517 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 518 | int val; |
| 519 | |
| 520 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 521 | if (nxt >= CONFIG_ENV_SIZE) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 522 | return (NULL); |
| 523 | } |
| 524 | } |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 525 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 526 | continue; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 527 | return ((char *)env_get_addr(val)); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | return (NULL); |
| 531 | } |
| 532 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 533 | int getenv_r (char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 534 | { |
| 535 | int i, nxt; |
| 536 | |
| 537 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 538 | int val, n; |
| 539 | |
| 540 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 541 | if (nxt >= CONFIG_ENV_SIZE) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 542 | return (-1); |
| 543 | } |
| 544 | } |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 545 | if ((val=envmatch((uchar *)name, i)) < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 546 | continue; |
| 547 | /* found; copy out */ |
| 548 | n = 0; |
| 549 | while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0') |
| 550 | ; |
| 551 | if (len == n) |
| 552 | *buf = '\0'; |
| 553 | return (n); |
| 554 | } |
| 555 | return (-1); |
| 556 | } |
| 557 | |
Mike Frysinger | 78dcaf4 | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 558 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 559 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 560 | int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 561 | { |
| 562 | extern char * env_name_spec; |
| 563 | |
| 564 | printf ("Saving Environment to %s...\n", env_name_spec); |
| 565 | |
| 566 | return (saveenv() ? 1 : 0); |
| 567 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 568 | |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 569 | U_BOOT_CMD( |
| 570 | saveenv, 1, 0, do_saveenv, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 571 | "save environment variables to persistent storage", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 572 | "" |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 573 | ); |
| 574 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 575 | #endif |
| 576 | |
| 577 | |
| 578 | /************************************************************************ |
| 579 | * Match a name / name=value pair |
| 580 | * |
| 581 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 582 | * i2 is the environment index for a 'name2=value2' pair. |
| 583 | * If the names match, return the index for the value2, else NULL. |
| 584 | */ |
| 585 | |
Rafal Jaworowski | 75b5442 | 2008-01-09 18:05:27 +0100 | [diff] [blame] | 586 | int envmatch (uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 587 | { |
| 588 | |
| 589 | while (*s1 == env_get_char(i2++)) |
| 590 | if (*s1++ == '=') |
| 591 | return(i2); |
| 592 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
| 593 | return(i2); |
| 594 | return(-1); |
| 595 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 596 | |
| 597 | |
| 598 | /**************************************************/ |
| 599 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 600 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 601 | printenv, CONFIG_SYS_MAXARGS, 1, do_printenv, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 602 | "print environment variables", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 603 | "\n - print values of all environment variables\n" |
| 604 | "printenv name ...\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 605 | " - print value of environment variable 'name'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 606 | ); |
| 607 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 608 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 609 | setenv, CONFIG_SYS_MAXARGS, 0, do_setenv, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 610 | "set environment variables", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 611 | "name value ...\n" |
| 612 | " - set environment variable 'name' to 'value ...'\n" |
| 613 | "setenv name\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 614 | " - delete environment variable 'name'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 615 | ); |
| 616 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 617 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 618 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 619 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 620 | askenv, CONFIG_SYS_MAXARGS, 1, do_askenv, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 621 | "get environment variables from stdin", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 622 | "name [message] [size]\n" |
| 623 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 624 | "askenv name\n" |
| 625 | " - get environment variable 'name' from stdin\n" |
| 626 | "askenv name size\n" |
| 627 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 628 | "askenv name [message] size\n" |
| 629 | " - display 'message' string and get environment variable 'name'" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 630 | "from stdin (max 'size' chars)" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 631 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 632 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 633 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 634 | #if defined(CONFIG_CMD_RUN) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 635 | int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 636 | U_BOOT_CMD( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 637 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 638 | "run commands in an environment variable", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 639 | "var [...]\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 640 | " - run the commands in the environment variable(s) 'var'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 641 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 642 | #endif |