wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 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> |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 7 | * |
| 8 | * Copyright 2011 Freescale Semiconductor, Inc. |
| 9 | * |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 29 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 30 | * Support for persistent environment data |
| 31 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | * The "environment" is stored on external storage as a list of '\0' |
| 33 | * terminated "name=value" strings. The end of the list is marked by |
| 34 | * a double '\0'. The environment is preceeded by a 32 bit CRC over |
| 35 | * the data part and, in case of redundant environment, a byte of |
| 36 | * flags. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 37 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 38 | * This linearized representation will also be used before |
| 39 | * relocation, i. e. as long as we don't have a full C runtime |
| 40 | * environment. After that, we use a hash table. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
| 43 | #include <common.h> |
| 44 | #include <command.h> |
| 45 | #include <environment.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 46 | #include <search.h> |
| 47 | #include <errno.h> |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 48 | #include <malloc.h> |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 49 | #include <watchdog.h> |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 50 | #include <serial.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 51 | #include <linux/stddef.h> |
| 52 | #include <asm/byteorder.h> |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 53 | #if defined(CONFIG_CMD_NET) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 54 | #include <net.h> |
| 55 | #endif |
| 56 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 57 | DECLARE_GLOBAL_DATA_PTR; |
| 58 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 59 | #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \ |
| 60 | !defined(CONFIG_ENV_IS_IN_FLASH) && \ |
| 61 | !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \ |
| 62 | !defined(CONFIG_ENV_IS_IN_MG_DISK) && \ |
| 63 | !defined(CONFIG_ENV_IS_IN_MMC) && \ |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 64 | !defined(CONFIG_ENV_IS_IN_FAT) && \ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 65 | !defined(CONFIG_ENV_IS_IN_NAND) && \ |
| 66 | !defined(CONFIG_ENV_IS_IN_NVRAM) && \ |
| 67 | !defined(CONFIG_ENV_IS_IN_ONENAND) && \ |
| 68 | !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ |
Liu Gang | 85bcd73 | 2012-03-08 00:33:20 +0000 | [diff] [blame] | 69 | !defined(CONFIG_ENV_IS_IN_REMOTE) && \ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 70 | !defined(CONFIG_ENV_IS_NOWHERE) |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 71 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\ |
Liu Gang | 85bcd73 | 2012-03-08 00:33:20 +0000 | [diff] [blame] | 72 | SPI_FLASH|MG_DISK|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | #define XMK_STR(x) #x |
| 76 | #define MK_STR(x) XMK_STR(x) |
| 77 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 78 | /* |
| 79 | * Maximum expected input data size for import command |
| 80 | */ |
| 81 | #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 82 | |
Mike Frysinger | 3b70e55 | 2010-12-21 14:08:27 -0500 | [diff] [blame] | 83 | ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */ |
Simon Glass | 34f5229 | 2011-10-21 18:51:38 +0000 | [diff] [blame] | 84 | ulong save_addr; /* Default Save Address */ |
| 85 | ulong save_size; /* Default Save Size (in bytes) */ |
Mike Frysinger | 3b70e55 | 2010-12-21 14:08:27 -0500 | [diff] [blame] | 86 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 87 | /* |
| 88 | * Table with supported baudrates (defined in config_xyz.h) |
| 89 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 90 | static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 91 | #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0])) |
| 92 | |
Heiko Schocher | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 93 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 94 | * This variable is incremented on each do_env_set(), so it can |
Heiko Schocher | 3b195ff | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 95 | * be used via get_env_id() as an indication, if the environment |
| 96 | * has changed or not. So it is possible to reread an environment |
| 97 | * variable only if the environment was changed ... done so for |
| 98 | * example in NetInitLoop() |
| 99 | */ |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 100 | static int env_id = 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 101 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 102 | int get_env_id(void) |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 103 | { |
| 104 | return env_id; |
| 105 | } |
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 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 108 | * Command interface: print one or all environment variables |
| 109 | * |
| 110 | * Returns 0 in case of error, or length of printed string |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 111 | */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 112 | static int env_print(char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 113 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | char *res = NULL; |
| 115 | size_t len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 116 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 117 | if (name) { /* print a single name */ |
| 118 | ENTRY e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 119 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 120 | e.key = name; |
| 121 | e.data = NULL; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 122 | hsearch_r(e, FIND, &ep, &env_htab); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 123 | if (ep == NULL) |
| 124 | return 0; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 125 | len = printf("%s=%s\n", ep->key, ep->data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 126 | return len; |
| 127 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 128 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 129 | /* print whole list */ |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 130 | len = hexport_r(&env_htab, '\n', &res, 0, 0, NULL); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 131 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 132 | if (len > 0) { |
| 133 | puts(res); |
| 134 | free(res); |
| 135 | return len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 138 | /* should never happen */ |
| 139 | return 0; |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 140 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 141 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 142 | int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 143 | { |
| 144 | int i; |
| 145 | int rcode = 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 146 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 147 | if (argc == 1) { |
| 148 | /* print all env vars */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 149 | rcode = env_print(NULL); |
| 150 | if (!rcode) |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 151 | return 1; |
| 152 | printf("\nEnvironment size: %d/%ld bytes\n", |
| 153 | rcode, (ulong)ENV_SIZE); |
| 154 | return 0; |
| 155 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 156 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 157 | /* print selected env vars */ |
| 158 | for (i = 1; i < argc; ++i) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 159 | int rc = env_print(argv[i]); |
| 160 | if (!rc) { |
| 161 | printf("## Error: \"%s\" not defined\n", argv[i]); |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 162 | ++rcode; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 163 | } |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return rcode; |
| 167 | } |
| 168 | |
| 169 | #ifdef CONFIG_CMD_GREPENV |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 170 | static int do_env_grep(cmd_tbl_t *cmdtp, int flag, |
| 171 | int argc, char * const argv[]) |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 172 | { |
| 173 | ENTRY *match; |
| 174 | unsigned char matched[env_htab.size / 8]; |
| 175 | int rcode = 1, arg = 1, idx; |
| 176 | |
| 177 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 178 | return CMD_RET_USAGE; |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 179 | |
| 180 | memset(matched, 0, env_htab.size / 8); |
| 181 | |
| 182 | while (arg <= argc) { |
| 183 | idx = 0; |
Kumar Gala | e7c338b | 2011-11-23 09:48:02 +0000 | [diff] [blame] | 184 | while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) { |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 185 | if (!(matched[idx / 8] & (1 << (idx & 7)))) { |
| 186 | puts(match->key); |
| 187 | puts("="); |
| 188 | puts(match->data); |
| 189 | puts("\n"); |
| 190 | } |
| 191 | matched[idx / 8] |= 1 << (idx & 7); |
| 192 | rcode = 0; |
| 193 | } |
| 194 | arg++; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 195 | } |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 196 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 197 | return rcode; |
| 198 | } |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 199 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 200 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 201 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 202 | * Set a new environment variable, |
| 203 | * or replace or delete an existing one. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 204 | */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 205 | int _do_env_set(int flag, int argc, char * const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 206 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 207 | bd_t *bd = gd->bd; |
| 208 | int i, len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 209 | int console = -1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 210 | char *name, *value, *s; |
| 211 | ENTRY e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 212 | |
| 213 | name = argv[1]; |
| 214 | |
Wolfgang Denk | bc50f8c | 2006-10-28 01:14:32 +0200 | [diff] [blame] | 215 | if (strchr(name, '=')) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 216 | printf("## Error: illegal character '=' in variable name" |
| 217 | "\"%s\"\n", name); |
Wolfgang Denk | bc50f8c | 2006-10-28 01:14:32 +0200 | [diff] [blame] | 218 | return 1; |
| 219 | } |
| 220 | |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 221 | env_id++; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 222 | /* |
| 223 | * search if variable with this name already exists |
| 224 | */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 225 | e.key = name; |
| 226 | e.data = NULL; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 227 | hsearch_r(e, FIND, &ep, &env_htab); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 228 | |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 229 | /* Check for console redirection */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 230 | if (strcmp(name, "stdin") == 0) |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 231 | console = stdin; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 232 | else if (strcmp(name, "stdout") == 0) |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 233 | console = stdout; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 234 | else if (strcmp(name, "stderr") == 0) |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 235 | console = stderr; |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 236 | |
| 237 | if (console != -1) { |
| 238 | if (argc < 3) { /* Cannot delete it! */ |
| 239 | printf("Can't delete \"%s\"\n", name); |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | #ifdef CONFIG_CONSOLE_MUX |
| 244 | i = iomux_doenv(console, argv[2]); |
| 245 | if (i) |
| 246 | return i; |
| 247 | #else |
| 248 | /* Try assigning specified device */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 249 | if (console_assign(console, argv[2]) < 0) |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 250 | return 1; |
| 251 | |
| 252 | #ifdef CONFIG_SERIAL_MULTI |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 253 | if (serial_assign(argv[2]) < 0) |
Alessandro Rubini | 5d4d06c | 2009-10-08 14:29:14 +0200 | [diff] [blame] | 254 | return 1; |
| 255 | #endif |
| 256 | #endif /* CONFIG_CONSOLE_MUX */ |
| 257 | } |
| 258 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 259 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 260 | * Some variables like "ethaddr" and "serial#" can be set only |
| 261 | * once and cannot be deleted; also, "ver" is readonly. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 262 | */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 263 | if (ep) { /* variable exists */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 264 | #ifndef CONFIG_ENV_OVERWRITE |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 265 | if (strcmp(name, "serial#") == 0 || |
| 266 | (strcmp(name, "ethaddr") == 0 |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 267 | #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 268 | && strcmp(ep->data, MK_STR(CONFIG_ETHADDR)) != 0 |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 269 | #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 270 | )) { |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 271 | printf("Can't overwrite \"%s\"\n", name); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 272 | return 1; |
| 273 | } |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 274 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 275 | /* |
| 276 | * Switch to new baudrate if new baudrate is supported |
| 277 | */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 278 | if (strcmp(name, "baudrate") == 0) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 279 | int baudrate = simple_strtoul(argv[2], NULL, 10); |
| 280 | int i; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 281 | for (i = 0; i < N_BAUDRATES; ++i) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 282 | if (baudrate == baudrate_table[i]) |
| 283 | break; |
| 284 | } |
| 285 | if (i == N_BAUDRATES) { |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 286 | printf("## Baudrate %d bps not supported\n", |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 287 | baudrate); |
| 288 | return 1; |
| 289 | } |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 290 | printf("## Switch baudrate to %d bps and" |
| 291 | "press ENTER ...\n", baudrate); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 292 | udelay(50000); |
| 293 | gd->baudrate = baudrate; |
Bartlomiej Sieka | 23d4328 | 2006-12-20 00:29:43 +0100 | [diff] [blame] | 294 | #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2) |
wdenk | e7f34c6 | 2003-01-11 09:48:40 +0000 | [diff] [blame] | 295 | gd->bd->bi_baudrate = baudrate; |
| 296 | #endif |
| 297 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 298 | serial_setbrg(); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 299 | udelay(50000); |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 300 | while (getc() != '\r') |
| 301 | ; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 302 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 303 | } |
| 304 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 305 | /* Delete only ? */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 306 | if (argc < 3 || argv[2] == NULL) { |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 307 | int rc = hdelete_r(name, &env_htab); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 308 | return !rc; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 312 | * Insert / replace new value |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 313 | */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 314 | for (i = 2, len = 0; i < argc; ++i) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 315 | len += strlen(argv[i]) + 1; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 316 | |
| 317 | value = malloc(len); |
| 318 | if (value == NULL) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 319 | printf("## Can't malloc %d bytes\n", len); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 320 | return 1; |
| 321 | } |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 322 | for (i = 2, s = value; i < argc; ++i) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 323 | char *v = argv[i]; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 324 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 325 | while ((*s++ = *v++) != '\0') |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 326 | ; |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 327 | *(s - 1) = ' '; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 328 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 329 | if (s != value) |
| 330 | *--s = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 331 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 332 | e.key = name; |
| 333 | e.data = value; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 334 | hsearch_r(e, ENTER, &ep, &env_htab); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 335 | free(value); |
| 336 | if (!ep) { |
| 337 | printf("## Error inserting \"%s\" variable, errno=%d\n", |
| 338 | name, errno); |
| 339 | return 1; |
| 340 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Some variables should be updated when the corresponding |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 344 | * entry in the environment is changed |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 345 | */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 346 | if (strcmp(name, "ipaddr") == 0) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 347 | char *s = argv[2]; /* always use only one arg */ |
| 348 | char *e; |
| 349 | unsigned long addr; |
| 350 | bd->bi_ip_addr = 0; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 351 | for (addr = 0, i = 0; i < 4; ++i) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 352 | ulong val = s ? simple_strtoul(s, &e, 10) : 0; |
| 353 | addr <<= 8; |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 354 | addr |= val & 0xFF; |
| 355 | if (s) |
| 356 | s = *e ? e + 1 : e; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 357 | } |
| 358 | bd->bi_ip_addr = htonl(addr); |
| 359 | return 0; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 360 | } else if (strcmp(argv[1], "loadaddr") == 0) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 361 | load_addr = simple_strtoul(argv[2], NULL, 16); |
| 362 | return 0; |
| 363 | } |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 364 | #if defined(CONFIG_CMD_NET) |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 365 | else if (strcmp(argv[1], "bootfile") == 0) { |
| 366 | copy_filename(BootFile, argv[2], sizeof(BootFile)); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 367 | return 0; |
| 368 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 369 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 373 | int setenv(const char *varname, const char *varvalue) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 374 | { |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 375 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
| 376 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 377 | if (varvalue == NULL || varvalue[0] == '\0') |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 378 | return _do_env_set(0, 2, (char * const *)argv); |
Jeffrey Mann | 62b896f | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 379 | else |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 380 | return _do_env_set(0, 3, (char * const *)argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Simon Glass | b8e926e | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 383 | /** |
| 384 | * Set an environment variable to an integer value |
| 385 | * |
| 386 | * @param varname Environmet variable to set |
| 387 | * @param value Value to set it to |
| 388 | * @return 0 if ok, 1 on error |
| 389 | */ |
| 390 | int setenv_ulong(const char *varname, ulong value) |
| 391 | { |
| 392 | /* TODO: this should be unsigned */ |
| 393 | char *str = simple_itoa(value); |
| 394 | |
| 395 | return setenv(varname, str); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Set an environment variable to an address in hex |
| 400 | * |
| 401 | * @param varname Environmet variable to set |
| 402 | * @param addr Value to set it to |
| 403 | * @return 0 if ok, 1 on error |
| 404 | */ |
| 405 | int setenv_addr(const char *varname, const void *addr) |
| 406 | { |
| 407 | char str[17]; |
| 408 | |
Simon Glass | c6f4e12 | 2011-11-04 06:42:36 +0000 | [diff] [blame] | 409 | sprintf(str, "%lx", (uintptr_t)addr); |
Simon Glass | b8e926e | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 410 | return setenv(varname, str); |
| 411 | } |
| 412 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 413 | int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 414 | { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 415 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 416 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 417 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 418 | return _do_env_set(flag, argc, argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 421 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 422 | * Prompt for environment variable |
| 423 | */ |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 424 | #if defined(CONFIG_CMD_ASKENV) |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 425 | int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 426 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 427 | char message[CONFIG_SYS_CBSIZE]; |
| 428 | int size = CONFIG_SYS_CBSIZE - 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 429 | int i, len, pos; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 430 | char *local_args[4]; |
| 431 | |
| 432 | local_args[0] = argv[0]; |
| 433 | local_args[1] = argv[1]; |
| 434 | local_args[2] = NULL; |
| 435 | local_args[3] = NULL; |
| 436 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 437 | /* Check the syntax */ |
| 438 | switch (argc) { |
| 439 | case 1: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 440 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 441 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 442 | case 2: /* env_ask envname */ |
| 443 | sprintf(message, "Please enter '%s':", argv[1]); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 444 | break; |
| 445 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 446 | case 3: /* env_ask envname size */ |
| 447 | sprintf(message, "Please enter '%s':", argv[1]); |
| 448 | size = simple_strtoul(argv[2], NULL, 10); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 449 | break; |
| 450 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 451 | default: /* env_ask envname message1 ... messagen size */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 452 | for (i = 2, pos = 0; i < argc - 1; i++) { |
| 453 | if (pos) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 454 | message[pos++] = ' '; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 455 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 456 | strcpy(message + pos, argv[i]); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 457 | pos += strlen(argv[i]); |
| 458 | } |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 459 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 460 | message[pos] = '\0'; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 461 | size = simple_strtoul(argv[argc - 1], NULL, 10); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 465 | if (size >= CONFIG_SYS_CBSIZE) |
| 466 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 467 | |
| 468 | if (size <= 0) |
| 469 | return 1; |
| 470 | |
| 471 | /* prompt for input */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 472 | len = readline(message); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 473 | |
| 474 | if (size < len) |
| 475 | console_buffer[size] = '\0'; |
| 476 | |
| 477 | len = 2; |
| 478 | if (console_buffer[0] != '\0') { |
| 479 | local_args[2] = console_buffer; |
| 480 | len = 3; |
| 481 | } |
| 482 | |
| 483 | /* Continue calling setenv code */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 484 | return _do_env_set(flag, len, local_args); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 485 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 486 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 487 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 488 | /* |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 489 | * Interactively edit an environment variable |
| 490 | */ |
| 491 | #if defined(CONFIG_CMD_EDITENV) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 492 | int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 493 | { |
| 494 | char buffer[CONFIG_SYS_CBSIZE]; |
| 495 | char *init_val; |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 496 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 497 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 498 | return CMD_RET_USAGE; |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 499 | |
| 500 | /* Set read buffer to initial value or empty sting */ |
| 501 | init_val = getenv(argv[1]); |
| 502 | if (init_val) |
Marek Vasut | 305e02a | 2011-09-26 02:26:03 +0200 | [diff] [blame] | 503 | sprintf(buffer, "%s", init_val); |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 504 | else |
| 505 | buffer[0] = '\0'; |
| 506 | |
Heiko Schocher | 0eab4d5 | 2012-01-16 21:13:05 +0000 | [diff] [blame] | 507 | readline_into_buffer("edit: ", buffer, 0); |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 508 | |
| 509 | return setenv(argv[1], buffer); |
| 510 | } |
| 511 | #endif /* CONFIG_CMD_EDITENV */ |
| 512 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 513 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 514 | * Look up variable from environment, |
| 515 | * return address of storage for that variable, |
| 516 | * or NULL if not found |
| 517 | */ |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 518 | char *getenv(const char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 519 | { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 520 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 521 | ENTRY e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 522 | |
Wolfgang Denk | f710efd | 2010-07-24 20:22:02 +0200 | [diff] [blame] | 523 | WATCHDOG_RESET(); |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 524 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 525 | e.key = name; |
| 526 | e.data = NULL; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 527 | hsearch_r(e, FIND, &ep, &env_htab); |
Wolfgang Denk | f710efd | 2010-07-24 20:22:02 +0200 | [diff] [blame] | 528 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 529 | return ep ? ep->data : NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 532 | /* restricted capabilities before import */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 533 | if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) |
| 534 | return (char *)(gd->env_buf); |
| 535 | |
| 536 | return NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 539 | /* |
| 540 | * Look up variable from environment for restricted C runtime env. |
| 541 | */ |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 542 | int getenv_f(const char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 543 | { |
| 544 | int i, nxt; |
| 545 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 546 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 547 | int val, n; |
| 548 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 549 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) { |
| 550 | if (nxt >= CONFIG_ENV_SIZE) |
| 551 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 552 | } |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 553 | |
| 554 | val = envmatch((uchar *)name, i); |
| 555 | if (val < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 556 | continue; |
Wolfgang Denk | 7a060a7 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 557 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 558 | /* found; copy out */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 559 | for (n = 0; n < len; ++n, ++buf) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 560 | *buf = env_get_char(val++); |
| 561 | if (*buf == '\0') |
Wolfgang Denk | 7a060a7 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 562 | return n; |
| 563 | } |
| 564 | |
| 565 | if (n) |
| 566 | *--buf = '\0'; |
| 567 | |
Wolfgang Denk | 4328547 | 2011-05-04 10:29:49 +0000 | [diff] [blame] | 568 | printf("env_buf [%d bytes] too small for value of \"%s\"\n", |
| 569 | len, name); |
Wolfgang Denk | 7a060a7 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 570 | |
| 571 | return n; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 572 | } |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 573 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 574 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Simon Glass | bf690f8 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 577 | /** |
| 578 | * Decode the integer value of an environment variable and return it. |
| 579 | * |
| 580 | * @param name Name of environemnt variable |
| 581 | * @param base Number base to use (normally 10, or 16 for hex) |
| 582 | * @param default_val Default value to return if the variable is not |
| 583 | * found |
| 584 | * @return the decoded value, or default_val if not found |
| 585 | */ |
| 586 | ulong getenv_ulong(const char *name, int base, ulong default_val) |
| 587 | { |
| 588 | /* |
| 589 | * We can use getenv() here, even before relocation, since the |
| 590 | * environment variable value is an integer and thus short. |
| 591 | */ |
| 592 | const char *str = getenv(name); |
| 593 | |
| 594 | return str ? simple_strtoul(str, NULL, base) : default_val; |
| 595 | } |
| 596 | |
Mike Frysinger | 78dcaf4 | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 597 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 598 | int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 599 | { |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 600 | printf("Saving Environment to %s...\n", env_name_spec); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 601 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 602 | return saveenv() ? 1 : 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 603 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 604 | |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 605 | U_BOOT_CMD( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 606 | saveenv, 1, 0, do_env_save, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 607 | "save environment variables to persistent storage", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 608 | "" |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 609 | ); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 610 | #endif |
| 611 | |
| 612 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 613 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 614 | * Match a name / name=value pair |
| 615 | * |
| 616 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 617 | * i2 is the environment index for a 'name2=value2' pair. |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 618 | * If the names match, return the index for the value2, else -1. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 619 | */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 620 | int envmatch(uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 621 | { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 622 | while (*s1 == env_get_char(i2++)) |
| 623 | if (*s1++ == '=') |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 624 | return i2; |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 625 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 626 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 627 | return i2; |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 628 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 629 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 630 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 631 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 632 | static int do_env_default(cmd_tbl_t *cmdtp, int flag, |
| 633 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 634 | { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 635 | if (argc != 2 || strcmp(argv[1], "-f") != 0) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 636 | return CMD_RET_USAGE; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 637 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 638 | set_default_env("## Resetting to default environment\n"); |
| 639 | return 0; |
| 640 | } |
| 641 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 642 | static int do_env_delete(cmd_tbl_t *cmdtp, int flag, |
| 643 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 644 | { |
| 645 | printf("Not implemented yet\n"); |
| 646 | return 0; |
| 647 | } |
| 648 | |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 649 | #ifdef CONFIG_CMD_EXPORTENV |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 650 | /* |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 651 | * env export [-t | -b | -c] [-s size] addr [var ...] |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 652 | * -t: export as text format; if size is given, data will be |
| 653 | * padded with '\0' bytes; if not, one terminating '\0' |
| 654 | * will be added (which is included in the "filesize" |
| 655 | * setting so you can for exmple copy this to flash and |
| 656 | * keep the termination). |
| 657 | * -b: export as binary format (name=value pairs separated by |
| 658 | * '\0', list end marked by double "\0\0") |
| 659 | * -c: export as checksum protected environment format as |
| 660 | * used for example by "saveenv" command |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 661 | * -s size: |
| 662 | * size of output buffer |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 663 | * addr: memory address where environment gets stored |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 664 | * var... List of variable names that get included into the |
| 665 | * export. Without arguments, the whole environment gets |
| 666 | * exported. |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 667 | * |
| 668 | * With "-c" and size is NOT given, then the export command will |
| 669 | * format the data as currently used for the persistent storage, |
| 670 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and |
| 671 | * prepend a valid CRC32 checksum and, in case of resundant |
| 672 | * environment, a "current" redundancy flag. If size is given, this |
| 673 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 |
| 674 | * checksum and redundancy flag will be inserted. |
| 675 | * |
| 676 | * With "-b" and "-t", always only the real data (including a |
| 677 | * terminating '\0' byte) will be written; here the optional size |
| 678 | * argument will be used to make sure not to overflow the user |
| 679 | * provided buffer; the command will abort if the size is not |
| 680 | * sufficient. Any remainign space will be '\0' padded. |
| 681 | * |
| 682 | * On successful return, the variable "filesize" will be set. |
| 683 | * Note that filesize includes the trailing/terminating '\0' byte(s). |
| 684 | * |
| 685 | * Usage szenario: create a text snapshot/backup of the current settings: |
| 686 | * |
| 687 | * => env export -t 100000 |
| 688 | * => era ${backup_addr} +${filesize} |
| 689 | * => cp.b 100000 ${backup_addr} ${filesize} |
| 690 | * |
| 691 | * Re-import this snapshot, deleting all other settings: |
| 692 | * |
| 693 | * => env import -d -t ${backup_addr} |
| 694 | */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 695 | static int do_env_export(cmd_tbl_t *cmdtp, int flag, |
| 696 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 697 | { |
| 698 | char buf[32]; |
| 699 | char *addr, *cmd, *res; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 700 | size_t size = 0; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 701 | ssize_t len; |
| 702 | env_t *envp; |
| 703 | char sep = '\n'; |
| 704 | int chk = 0; |
| 705 | int fmt = 0; |
| 706 | |
| 707 | cmd = *argv; |
| 708 | |
| 709 | while (--argc > 0 && **++argv == '-') { |
| 710 | char *arg = *argv; |
| 711 | while (*++arg) { |
| 712 | switch (*arg) { |
| 713 | case 'b': /* raw binary format */ |
| 714 | if (fmt++) |
| 715 | goto sep_err; |
| 716 | sep = '\0'; |
| 717 | break; |
| 718 | case 'c': /* external checksum format */ |
| 719 | if (fmt++) |
| 720 | goto sep_err; |
| 721 | sep = '\0'; |
| 722 | chk = 1; |
| 723 | break; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 724 | case 's': /* size given */ |
| 725 | if (--argc <= 0) |
| 726 | return cmd_usage(cmdtp); |
| 727 | size = simple_strtoul(*++argv, NULL, 16); |
| 728 | goto NXTARG; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 729 | case 't': /* text format */ |
| 730 | if (fmt++) |
| 731 | goto sep_err; |
| 732 | sep = '\n'; |
| 733 | break; |
| 734 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 735 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 736 | } |
| 737 | } |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 738 | NXTARG: ; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 741 | if (argc < 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 742 | return CMD_RET_USAGE; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 743 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 744 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
| 745 | |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 746 | if (size) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 747 | memset(addr, '\0', size); |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 748 | |
| 749 | argc--; |
| 750 | argv++; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 751 | |
| 752 | if (sep) { /* export as text file */ |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 753 | len = hexport_r(&env_htab, sep, &addr, size, argc, argv); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 754 | if (len < 0) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 755 | error("Cannot export environment: errno = %d\n", errno); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 756 | return 1; |
| 757 | } |
Andreas Bießmann | 92b9b2e | 2011-02-09 15:10:29 +0100 | [diff] [blame] | 758 | sprintf(buf, "%zX", (size_t)len); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 759 | setenv("filesize", buf); |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | envp = (env_t *)addr; |
| 765 | |
| 766 | if (chk) /* export as checksum protected block */ |
| 767 | res = (char *)envp->data; |
| 768 | else /* export as raw binary data */ |
| 769 | res = addr; |
| 770 | |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 771 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, argc, argv); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 772 | if (len < 0) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 773 | error("Cannot export environment: errno = %d\n", errno); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 774 | return 1; |
| 775 | } |
| 776 | |
| 777 | if (chk) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 778 | envp->crc = crc32(0, envp->data, ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 779 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 780 | envp->flags = ACTIVE_FLAG; |
| 781 | #endif |
| 782 | } |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 783 | sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data))); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 784 | setenv("filesize", buf); |
| 785 | |
| 786 | return 0; |
| 787 | |
| 788 | sep_err: |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 789 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 790 | return 1; |
| 791 | } |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 792 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 793 | |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 794 | #ifdef CONFIG_CMD_IMPORTENV |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 795 | /* |
| 796 | * env import [-d] [-t | -b | -c] addr [size] |
| 797 | * -d: delete existing environment before importing; |
| 798 | * otherwise overwrite / append to existion definitions |
| 799 | * -t: assume text format; either "size" must be given or the |
| 800 | * text data must be '\0' terminated |
| 801 | * -b: assume binary format ('\0' separated, "\0\0" terminated) |
| 802 | * -c: assume checksum protected environment format |
| 803 | * addr: memory address to read from |
| 804 | * size: length of input data; if missing, proper '\0' |
| 805 | * termination is mandatory |
| 806 | */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 807 | static int do_env_import(cmd_tbl_t *cmdtp, int flag, |
| 808 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 809 | { |
| 810 | char *cmd, *addr; |
| 811 | char sep = '\n'; |
| 812 | int chk = 0; |
| 813 | int fmt = 0; |
| 814 | int del = 0; |
| 815 | size_t size; |
| 816 | |
| 817 | cmd = *argv; |
| 818 | |
| 819 | while (--argc > 0 && **++argv == '-') { |
| 820 | char *arg = *argv; |
| 821 | while (*++arg) { |
| 822 | switch (*arg) { |
| 823 | case 'b': /* raw binary format */ |
| 824 | if (fmt++) |
| 825 | goto sep_err; |
| 826 | sep = '\0'; |
| 827 | break; |
| 828 | case 'c': /* external checksum format */ |
| 829 | if (fmt++) |
| 830 | goto sep_err; |
| 831 | sep = '\0'; |
| 832 | chk = 1; |
| 833 | break; |
| 834 | case 't': /* text format */ |
| 835 | if (fmt++) |
| 836 | goto sep_err; |
| 837 | sep = '\n'; |
| 838 | break; |
| 839 | case 'd': |
| 840 | del = 1; |
| 841 | break; |
| 842 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 843 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 848 | if (argc < 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 849 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 850 | |
| 851 | if (!fmt) |
| 852 | printf("## Warning: defaulting to text format\n"); |
| 853 | |
| 854 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
| 855 | |
| 856 | if (argc == 2) { |
| 857 | size = simple_strtoul(argv[1], NULL, 16); |
| 858 | } else { |
| 859 | char *s = addr; |
| 860 | |
| 861 | size = 0; |
| 862 | |
| 863 | while (size < MAX_ENV_SIZE) { |
| 864 | if ((*s == sep) && (*(s+1) == '\0')) |
| 865 | break; |
| 866 | ++s; |
| 867 | ++size; |
| 868 | } |
| 869 | if (size == MAX_ENV_SIZE) { |
| 870 | printf("## Warning: Input data exceeds %d bytes" |
| 871 | " - truncated\n", MAX_ENV_SIZE); |
| 872 | } |
Horst Kronstorfer | 295da64 | 2011-12-16 23:33:10 +0000 | [diff] [blame] | 873 | size += 2; |
Simon Glass | c6f4e12 | 2011-11-04 06:42:36 +0000 | [diff] [blame] | 874 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | if (chk) { |
| 878 | uint32_t crc; |
| 879 | env_t *ep = (env_t *)addr; |
| 880 | |
| 881 | size -= offsetof(env_t, data); |
| 882 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 883 | |
| 884 | if (crc32(0, ep->data, size) != crc) { |
| 885 | puts("## Error: bad CRC, import failed\n"); |
| 886 | return 1; |
| 887 | } |
| 888 | addr = (char *)ep->data; |
| 889 | } |
| 890 | |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 891 | if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR) == 0) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 892 | error("Environment import failed: errno = %d\n", errno); |
| 893 | return 1; |
| 894 | } |
| 895 | gd->flags |= GD_FLG_ENV_READY; |
| 896 | |
| 897 | return 0; |
| 898 | |
| 899 | sep_err: |
| 900 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 901 | cmd); |
| 902 | return 1; |
| 903 | } |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 904 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 905 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 906 | /* |
| 907 | * New command line interface: "env" command with subcommands |
| 908 | */ |
| 909 | static cmd_tbl_t cmd_env_sub[] = { |
| 910 | #if defined(CONFIG_CMD_ASKENV) |
| 911 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), |
| 912 | #endif |
| 913 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), |
| 914 | U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""), |
| 915 | #if defined(CONFIG_CMD_EDITENV) |
| 916 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), |
| 917 | #endif |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 918 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 919 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 920 | #endif |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 921 | #if defined(CONFIG_CMD_GREPENV) |
| 922 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), |
| 923 | #endif |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 924 | #if defined(CONFIG_CMD_IMPORTENV) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 925 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 926 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 927 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
| 928 | #if defined(CONFIG_CMD_RUN) |
| 929 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), |
| 930 | #endif |
| 931 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
| 932 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), |
| 933 | #endif |
| 934 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), |
| 935 | }; |
| 936 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 937 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | faa0d2e | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 938 | void env_reloc(void) |
| 939 | { |
| 940 | fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 941 | } |
| 942 | #endif |
| 943 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 944 | static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 945 | { |
| 946 | cmd_tbl_t *cp; |
| 947 | |
Thomas Weber | 4d28265 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 948 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 949 | return CMD_RET_USAGE; |
Thomas Weber | 4d28265 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 950 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 951 | /* drop initial "env" arg */ |
| 952 | argc--; |
| 953 | argv++; |
| 954 | |
| 955 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 956 | |
| 957 | if (cp) |
| 958 | return cp->cmd(cmdtp, flag, argc, argv); |
| 959 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 960 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | U_BOOT_CMD( |
| 964 | env, CONFIG_SYS_MAXARGS, 1, do_env, |
| 965 | "environment handling commands", |
| 966 | #if defined(CONFIG_CMD_ASKENV) |
| 967 | "ask name [message] [size] - ask for environment variable\nenv " |
| 968 | #endif |
| 969 | "default -f - reset default environment\n" |
| 970 | #if defined(CONFIG_CMD_EDITENV) |
| 971 | "env edit name - edit environment variable\n" |
| 972 | #endif |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 973 | "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 974 | #if defined(CONFIG_CMD_GREPENV) |
| 975 | "env grep string [...] - search environment\n" |
| 976 | #endif |
| 977 | "env import [-d] [-t | -b | -c] addr [size] - import environment\n" |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 978 | "env print [name ...] - print environment\n" |
| 979 | #if defined(CONFIG_CMD_RUN) |
| 980 | "env run var [...] - run commands in an environment variable\n" |
| 981 | #endif |
Horst Kronstorfer | c63e900 | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 982 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 983 | "env save - save environment\n" |
Horst Kronstorfer | c63e900 | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 984 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 985 | "env set [-f] name [arg ...]\n" |
| 986 | ); |
| 987 | |
| 988 | /* |
| 989 | * Old command line interface, kept for compatibility |
| 990 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 991 | |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 992 | #if defined(CONFIG_CMD_EDITENV) |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 993 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 994 | editenv, 2, 0, do_env_edit, |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 995 | "edit environment variable", |
| 996 | "name\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 997 | " - edit environment variable 'name'", |
| 998 | var_complete |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 999 | ); |
| 1000 | #endif |
| 1001 | |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1002 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1003 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1004 | "print environment variables", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1005 | "\n - print values of all environment variables\n" |
| 1006 | "printenv name ...\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1007 | " - print value of environment variable 'name'", |
| 1008 | var_complete |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1009 | ); |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1010 | |
| 1011 | #ifdef CONFIG_CMD_GREPENV |
| 1012 | U_BOOT_CMD_COMPLETE( |
| 1013 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, |
| 1014 | "search environment variables", |
| 1015 | "string ...\n" |
| 1016 | " - list environment name=value pairs matching 'string'", |
| 1017 | var_complete |
| 1018 | ); |
| 1019 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1020 | |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1021 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1022 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1023 | "set environment variables", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1024 | "name value ...\n" |
| 1025 | " - set environment variable 'name' to 'value ...'\n" |
| 1026 | "setenv name\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1027 | " - delete environment variable 'name'", |
| 1028 | var_complete |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1029 | ); |
| 1030 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1031 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1032 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1033 | U_BOOT_CMD( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1034 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1035 | "get environment variables from stdin", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1036 | "name [message] [size]\n" |
| 1037 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 1038 | "askenv name\n" |
| 1039 | " - get environment variable 'name' from stdin\n" |
| 1040 | "askenv name size\n" |
| 1041 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 1042 | "askenv name [message] size\n" |
| 1043 | " - display 'message' string and get environment variable 'name'" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1044 | "from stdin (max 'size' chars)" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1045 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1046 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1047 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1048 | #if defined(CONFIG_CMD_RUN) |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1049 | U_BOOT_CMD_COMPLETE( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1050 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1051 | "run commands in an environment variable", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1052 | "var [...]\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1053 | " - run the commands in the environment variable(s) 'var'", |
| 1054 | var_complete |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1055 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1056 | #endif |