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