wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 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 | a000b79 | 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 | ea882ba | 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 | ea882ba | 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 | ea882ba | 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 | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 46 | #include <search.h> |
| 47 | #include <errno.h> |
Peter Tyser | 246c692 | 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 | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 50 | #include <linux/stddef.h> |
| 51 | #include <asm/byteorder.h> |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 52 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 55 | #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \ |
| 56 | !defined(CONFIG_ENV_IS_IN_FLASH) && \ |
| 57 | !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 58 | !defined(CONFIG_ENV_IS_IN_MMC) && \ |
Maximilian Schwerin | 57210c7 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 59 | !defined(CONFIG_ENV_IS_IN_FAT) && \ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 60 | !defined(CONFIG_ENV_IS_IN_NAND) && \ |
| 61 | !defined(CONFIG_ENV_IS_IN_NVRAM) && \ |
| 62 | !defined(CONFIG_ENV_IS_IN_ONENAND) && \ |
| 63 | !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \ |
Liu Gang | 0a85a9e | 2012-03-08 00:33:20 +0000 | [diff] [blame] | 64 | !defined(CONFIG_ENV_IS_IN_REMOTE) && \ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 65 | !defined(CONFIG_ENV_IS_NOWHERE) |
unsik Kim | 75eb82e | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 66 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\ |
Marek Vasut | b37d41a | 2012-03-04 15:11:32 +0000 | [diff] [blame] | 67 | SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Maximum expected input data size for import command |
| 72 | */ |
| 73 | #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 74 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 75 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 76 | * This variable is incremented on each do_env_set(), so it can |
Heiko Schocher | da95427 | 2009-04-28 08:36:11 +0200 | [diff] [blame] | 77 | * be used via get_env_id() as an indication, if the environment |
| 78 | * has changed or not. So it is possible to reread an environment |
| 79 | * variable only if the environment was changed ... done so for |
| 80 | * example in NetInitLoop() |
| 81 | */ |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 82 | static int env_id = 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 83 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 84 | int get_env_id(void) |
Heiko Schocher | 2f70c49 | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 85 | { |
| 86 | return env_id; |
| 87 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 88 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 89 | #ifndef CONFIG_SPL_BUILD |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 90 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 91 | * Command interface: print one or all environment variables |
| 92 | * |
| 93 | * Returns 0 in case of error, or length of printed string |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 94 | */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 95 | static int env_print(char *name, int flag) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 96 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 97 | char *res = NULL; |
| 98 | size_t len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 99 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 100 | if (name) { /* print a single name */ |
| 101 | ENTRY e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 102 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | e.key = name; |
| 104 | e.data = NULL; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 105 | hsearch_r(e, FIND, &ep, &env_htab, flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 106 | if (ep == NULL) |
| 107 | return 0; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 108 | len = printf("%s=%s\n", ep->key, ep->data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 109 | return len; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 110 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 111 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 112 | /* print whole list */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 113 | len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | |
| 115 | if (len > 0) { |
| 116 | puts(res); |
| 117 | free(res); |
| 118 | return len; |
| 119 | } |
| 120 | |
| 121 | /* should never happen */ |
| 122 | return 0; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 125 | static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, |
| 126 | char * const argv[]) |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 127 | { |
| 128 | int i; |
| 129 | int rcode = 0; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 130 | int env_flag = H_HIDE_DOT; |
| 131 | |
| 132 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') { |
| 133 | argc--; |
| 134 | argv++; |
| 135 | env_flag &= ~H_HIDE_DOT; |
| 136 | } |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 137 | |
| 138 | if (argc == 1) { |
| 139 | /* print all env vars */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 140 | rcode = env_print(NULL, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 141 | if (!rcode) |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 142 | return 1; |
| 143 | printf("\nEnvironment size: %d/%ld bytes\n", |
| 144 | rcode, (ulong)ENV_SIZE); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 148 | /* print selected env vars */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 149 | env_flag &= ~H_HIDE_DOT; |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 150 | for (i = 1; i < argc; ++i) { |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 151 | int rc = env_print(argv[i], env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 152 | if (!rc) { |
| 153 | printf("## Error: \"%s\" not defined\n", argv[i]); |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 154 | ++rcode; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
Mike Frysinger | 4c94f6c | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 157 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 158 | return rcode; |
| 159 | } |
| 160 | |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 161 | #ifdef CONFIG_CMD_GREPENV |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 162 | static int do_env_grep(cmd_tbl_t *cmdtp, int flag, |
| 163 | int argc, char * const argv[]) |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 164 | { |
| 165 | ENTRY *match; |
| 166 | unsigned char matched[env_htab.size / 8]; |
| 167 | int rcode = 1, arg = 1, idx; |
| 168 | |
| 169 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 170 | return CMD_RET_USAGE; |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 171 | |
| 172 | memset(matched, 0, env_htab.size / 8); |
| 173 | |
| 174 | while (arg <= argc) { |
| 175 | idx = 0; |
Kumar Gala | 068f158 | 2011-11-23 09:48:02 +0000 | [diff] [blame] | 176 | while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) { |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 177 | if (!(matched[idx / 8] & (1 << (idx & 7)))) { |
| 178 | puts(match->key); |
| 179 | puts("="); |
| 180 | puts(match->data); |
| 181 | puts("\n"); |
| 182 | } |
| 183 | matched[idx / 8] |= 1 << (idx & 7); |
| 184 | rcode = 0; |
| 185 | } |
| 186 | arg++; |
| 187 | } |
| 188 | |
| 189 | return rcode; |
| 190 | } |
| 191 | #endif |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 192 | #endif /* CONFIG_SPL_BUILD */ |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 193 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 194 | /* |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 195 | * Set a new environment variable, |
| 196 | * or replace or delete an existing one. |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 197 | */ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 198 | static int _do_env_set(int flag, int argc, char * const argv[]) |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 199 | { |
| 200 | int i, len; |
| 201 | char *name, *value, *s; |
| 202 | ENTRY e, *ep; |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 203 | int env_flag = H_INTERACTIVE; |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 204 | |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 205 | debug("Initial value for argc=%d\n", argc); |
| 206 | while (argc > 1 && **(argv + 1) == '-') { |
| 207 | char *arg = *++argv; |
| 208 | |
| 209 | --argc; |
| 210 | while (*++arg) { |
| 211 | switch (*arg) { |
| 212 | case 'f': /* force */ |
| 213 | env_flag |= H_FORCE; |
| 214 | break; |
| 215 | default: |
| 216 | return CMD_RET_USAGE; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | debug("Final value for argc=%d\n", argc); |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 221 | name = argv[1]; |
| 222 | value = argv[2]; |
| 223 | |
| 224 | if (strchr(name, '=')) { |
| 225 | printf("## Error: illegal character '='" |
| 226 | "in variable name \"%s\"\n", name); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | env_id++; |
Gerlando Falauto | c3f6525 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 231 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 232 | /* Delete only ? */ |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 233 | if (argc < 3 || argv[2] == NULL) { |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 234 | int rc = hdelete_r(name, &env_htab, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 235 | return !rc; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 239 | * Insert / replace new value |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 240 | */ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 241 | for (i = 2, len = 0; i < argc; ++i) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 242 | len += strlen(argv[i]) + 1; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 243 | |
| 244 | value = malloc(len); |
| 245 | if (value == NULL) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 246 | printf("## Can't malloc %d bytes\n", len); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 247 | return 1; |
| 248 | } |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 249 | for (i = 2, s = value; i < argc; ++i) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 250 | char *v = argv[i]; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 251 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 252 | while ((*s++ = *v++) != '\0') |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 253 | ; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 254 | *(s - 1) = ' '; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 255 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 256 | if (s != value) |
| 257 | *--s = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 258 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 259 | e.key = name; |
| 260 | e.data = value; |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 261 | hsearch_r(e, ENTER, &ep, &env_htab, env_flag); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 262 | free(value); |
| 263 | if (!ep) { |
| 264 | printf("## Error inserting \"%s\" variable, errno=%d\n", |
| 265 | name, errno); |
| 266 | return 1; |
| 267 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 268 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 272 | int setenv(const char *varname, const char *varvalue) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 273 | { |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 274 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
| 275 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 276 | if (varvalue == NULL || varvalue[0] == '\0') |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 277 | return _do_env_set(0, 2, (char * const *)argv); |
Jeffrey Mann | 9ffd451 | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 278 | else |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 279 | return _do_env_set(0, 3, (char * const *)argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 282 | /** |
| 283 | * Set an environment variable to an integer value |
| 284 | * |
| 285 | * @param varname Environmet variable to set |
| 286 | * @param value Value to set it to |
| 287 | * @return 0 if ok, 1 on error |
| 288 | */ |
| 289 | int setenv_ulong(const char *varname, ulong value) |
| 290 | { |
| 291 | /* TODO: this should be unsigned */ |
| 292 | char *str = simple_itoa(value); |
| 293 | |
| 294 | return setenv(varname, str); |
| 295 | } |
| 296 | |
| 297 | /** |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 298 | * Set an environment variable to an value in hex |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 299 | * |
| 300 | * @param varname Environmet variable to set |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 301 | * @param value Value to set it to |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 302 | * @return 0 if ok, 1 on error |
| 303 | */ |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 304 | int setenv_hex(const char *varname, ulong value) |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 305 | { |
| 306 | char str[17]; |
| 307 | |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 308 | sprintf(str, "%lx", value); |
Simon Glass | d67f10c | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 309 | return setenv(varname, str); |
| 310 | } |
| 311 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 312 | #ifndef CONFIG_SPL_BUILD |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 313 | static 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] | 314 | { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 315 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 316 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 317 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 318 | return _do_env_set(flag, argc, argv); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 321 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 322 | * Prompt for environment variable |
| 323 | */ |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 324 | #if defined(CONFIG_CMD_ASKENV) |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 325 | 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] | 326 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 327 | char message[CONFIG_SYS_CBSIZE]; |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 328 | int i, len, pos, size; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 329 | char *local_args[4]; |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 330 | char *endptr; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 331 | |
| 332 | local_args[0] = argv[0]; |
| 333 | local_args[1] = argv[1]; |
| 334 | local_args[2] = NULL; |
| 335 | local_args[3] = NULL; |
| 336 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 337 | /* |
| 338 | * Check the syntax: |
| 339 | * |
| 340 | * env_ask envname [message1 ...] [size] |
| 341 | */ |
| 342 | if (argc == 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 343 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 344 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 345 | /* |
| 346 | * We test the last argument if it can be converted |
| 347 | * into a decimal number. If yes, we assume it's |
| 348 | * the size. Otherwise we echo it as part of the |
| 349 | * message. |
| 350 | */ |
| 351 | i = simple_strtoul(argv[argc - 1], &endptr, 10); |
| 352 | if (*endptr != '\0') { /* no size */ |
| 353 | size = CONFIG_SYS_CBSIZE - 1; |
| 354 | } else { /* size given */ |
| 355 | size = i; |
| 356 | --argc; |
| 357 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 358 | |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 359 | if (argc <= 2) { |
| 360 | sprintf(message, "Please enter '%s': ", argv[1]); |
| 361 | } else { |
| 362 | /* env_ask envname message1 ... messagen [size] */ |
| 363 | for (i = 2, pos = 0; i < argc; i++) { |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 364 | if (pos) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 365 | message[pos++] = ' '; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 366 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 367 | strcpy(message + pos, argv[i]); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 368 | pos += strlen(argv[i]); |
| 369 | } |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 370 | message[pos++] = ' '; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 371 | message[pos] = '\0'; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 374 | if (size >= CONFIG_SYS_CBSIZE) |
| 375 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 376 | |
| 377 | if (size <= 0) |
| 378 | return 1; |
| 379 | |
| 380 | /* prompt for input */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 381 | len = readline(message); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 382 | |
| 383 | if (size < len) |
| 384 | console_buffer[size] = '\0'; |
| 385 | |
| 386 | len = 2; |
| 387 | if (console_buffer[0] != '\0') { |
| 388 | local_args[2] = console_buffer; |
| 389 | len = 3; |
| 390 | } |
| 391 | |
| 392 | /* Continue calling setenv code */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 393 | return _do_env_set(flag, len, local_args); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 394 | } |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 395 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 396 | |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 397 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 398 | static int print_static_binding(const char *var_name, const char *callback_name) |
| 399 | { |
| 400 | printf("\t%-20s %-20s\n", var_name, callback_name); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int print_active_callback(ENTRY *entry) |
| 406 | { |
| 407 | struct env_clbk_tbl *clbkp; |
| 408 | int i; |
| 409 | int num_callbacks; |
| 410 | |
| 411 | if (entry->callback == NULL) |
| 412 | return 0; |
| 413 | |
| 414 | /* look up the callback in the linker-list */ |
| 415 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 416 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 417 | i < num_callbacks; |
| 418 | i++, clbkp++) { |
| 419 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 420 | if (entry->callback == clbkp->callback + gd->reloc_off) |
| 421 | #else |
| 422 | if (entry->callback == clbkp->callback) |
| 423 | #endif |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | if (i == num_callbacks) |
| 428 | /* this should probably never happen, but just in case... */ |
| 429 | printf("\t%-20s %p\n", entry->key, entry->callback); |
| 430 | else |
| 431 | printf("\t%-20s %-20s\n", entry->key, clbkp->name); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Print the callbacks available and what they are bound to |
| 438 | */ |
| 439 | int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 440 | { |
| 441 | struct env_clbk_tbl *clbkp; |
| 442 | int i; |
| 443 | int num_callbacks; |
| 444 | |
| 445 | /* Print the available callbacks */ |
| 446 | puts("Available callbacks:\n"); |
| 447 | puts("\tCallback Name\n"); |
| 448 | puts("\t-------------\n"); |
| 449 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 450 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 451 | i < num_callbacks; |
| 452 | i++, clbkp++) |
| 453 | printf("\t%s\n", clbkp->name); |
| 454 | puts("\n"); |
| 455 | |
| 456 | /* Print the static bindings that may exist */ |
| 457 | puts("Static callback bindings:\n"); |
| 458 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 459 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
| 460 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding); |
| 461 | puts("\n"); |
| 462 | |
| 463 | /* walk through each variable and print the callback if it has one */ |
| 464 | puts("Active callback bindings:\n"); |
| 465 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 466 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
| 467 | hwalk_r(&env_htab, print_active_callback); |
| 468 | return 0; |
| 469 | } |
| 470 | #endif |
| 471 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 472 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 473 | static int print_static_flags(const char *var_name, const char *flags) |
| 474 | { |
| 475 | enum env_flags_vartype type = env_flags_parse_vartype(flags); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 476 | enum env_flags_varaccess access = env_flags_parse_varaccess(flags); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 477 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 478 | printf("\t%-20s %-20s %-20s\n", var_name, |
| 479 | env_flags_get_vartype_name(type), |
| 480 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int print_active_flags(ENTRY *entry) |
| 486 | { |
| 487 | enum env_flags_vartype type; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 488 | enum env_flags_varaccess access; |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 489 | |
| 490 | if (entry->flags == 0) |
| 491 | return 0; |
| 492 | |
| 493 | type = (enum env_flags_vartype) |
| 494 | (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 495 | access = env_flags_parse_varaccess_from_binflags(entry->flags); |
| 496 | printf("\t%-20s %-20s %-20s\n", entry->key, |
| 497 | env_flags_get_vartype_name(type), |
| 498 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Print the flags available and what variables have flags |
| 505 | */ |
| 506 | int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 507 | { |
| 508 | /* Print the available variable types */ |
| 509 | printf("Available variable type flags (position %d):\n", |
| 510 | ENV_FLAGS_VARTYPE_LOC); |
| 511 | puts("\tFlag\tVariable Type Name\n"); |
| 512 | puts("\t----\t------------------\n"); |
| 513 | env_flags_print_vartypes(); |
| 514 | puts("\n"); |
| 515 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 516 | /* Print the available variable access types */ |
| 517 | printf("Available variable access flags (position %d):\n", |
| 518 | ENV_FLAGS_VARACCESS_LOC); |
| 519 | puts("\tFlag\tVariable Access Name\n"); |
| 520 | puts("\t----\t--------------------\n"); |
| 521 | env_flags_print_varaccess(); |
| 522 | puts("\n"); |
| 523 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 524 | /* Print the static flags that may exist */ |
| 525 | puts("Static flags:\n"); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 526 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 527 | "Variable Access"); |
| 528 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 529 | "---------------"); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 530 | env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags); |
| 531 | puts("\n"); |
| 532 | |
| 533 | /* walk through each variable and print the flags if non-default */ |
| 534 | puts("Active flags:\n"); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 535 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 536 | "Variable Access"); |
| 537 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 538 | "---------------"); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 539 | hwalk_r(&env_htab, print_active_flags); |
| 540 | return 0; |
| 541 | } |
| 542 | #endif |
| 543 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 544 | /* |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 545 | * Interactively edit an environment variable |
| 546 | */ |
| 547 | #if defined(CONFIG_CMD_EDITENV) |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 548 | static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, |
| 549 | char * const argv[]) |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 550 | { |
| 551 | char buffer[CONFIG_SYS_CBSIZE]; |
| 552 | char *init_val; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 553 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 554 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 555 | return CMD_RET_USAGE; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 556 | |
| 557 | /* Set read buffer to initial value or empty sting */ |
| 558 | init_val = getenv(argv[1]); |
| 559 | if (init_val) |
Marek Vasut | 7fcd9bb | 2011-09-26 02:26:03 +0200 | [diff] [blame] | 560 | sprintf(buffer, "%s", init_val); |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 561 | else |
| 562 | buffer[0] = '\0'; |
| 563 | |
Joe Hershberger | 18a3cce | 2013-02-08 10:12:34 +0000 | [diff] [blame] | 564 | if (readline_into_buffer("edit: ", buffer, 0) < 0) |
| 565 | return 1; |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 566 | |
| 567 | return setenv(argv[1], buffer); |
| 568 | } |
| 569 | #endif /* CONFIG_CMD_EDITENV */ |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 570 | #endif /* CONFIG_SPL_BUILD */ |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 571 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 572 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 573 | * Look up variable from environment, |
| 574 | * return address of storage for that variable, |
| 575 | * or NULL if not found |
| 576 | */ |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 577 | char *getenv(const char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 578 | { |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 579 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 580 | ENTRY e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 581 | |
Wolfgang Denk | 91a7675 | 2010-07-24 20:22:02 +0200 | [diff] [blame] | 582 | WATCHDOG_RESET(); |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 583 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 584 | e.key = name; |
| 585 | e.data = NULL; |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 586 | hsearch_r(e, FIND, &ep, &env_htab, 0); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 587 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 588 | return ep ? ep->data : NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 591 | /* restricted capabilities before import */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 592 | if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) |
| 593 | return (char *)(gd->env_buf); |
| 594 | |
| 595 | return NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 598 | /* |
| 599 | * Look up variable from environment for restricted C runtime env. |
| 600 | */ |
Wolfgang Denk | 84b5e80 | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 601 | int getenv_f(const char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 602 | { |
| 603 | int i, nxt; |
| 604 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 605 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 606 | int val, n; |
| 607 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 608 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) { |
| 609 | if (nxt >= CONFIG_ENV_SIZE) |
| 610 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 611 | } |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 612 | |
| 613 | val = envmatch((uchar *)name, i); |
| 614 | if (val < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 615 | continue; |
Wolfgang Denk | 9ed4a95 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 616 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 617 | /* found; copy out */ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 618 | for (n = 0; n < len; ++n, ++buf) { |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 619 | *buf = env_get_char(val++); |
| 620 | if (*buf == '\0') |
Wolfgang Denk | 9ed4a95 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 621 | return n; |
| 622 | } |
| 623 | |
| 624 | if (n) |
| 625 | *--buf = '\0'; |
| 626 | |
Wolfgang Denk | a02a884 | 2011-05-04 10:29:49 +0000 | [diff] [blame] | 627 | printf("env_buf [%d bytes] too small for value of \"%s\"\n", |
| 628 | len, name); |
Wolfgang Denk | 9ed4a95 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 629 | |
| 630 | return n; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 631 | } |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 632 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 633 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Simon Glass | 4a9b413 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 636 | /** |
| 637 | * Decode the integer value of an environment variable and return it. |
| 638 | * |
| 639 | * @param name Name of environemnt variable |
| 640 | * @param base Number base to use (normally 10, or 16 for hex) |
| 641 | * @param default_val Default value to return if the variable is not |
| 642 | * found |
| 643 | * @return the decoded value, or default_val if not found |
| 644 | */ |
| 645 | ulong getenv_ulong(const char *name, int base, ulong default_val) |
| 646 | { |
| 647 | /* |
| 648 | * We can use getenv() here, even before relocation, since the |
| 649 | * environment variable value is an integer and thus short. |
| 650 | */ |
| 651 | const char *str = getenv(name); |
| 652 | |
| 653 | return str ? simple_strtoul(str, NULL, base) : default_val; |
| 654 | } |
| 655 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 656 | #ifndef CONFIG_SPL_BUILD |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 657 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 658 | static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, |
| 659 | char * const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 660 | { |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 661 | printf("Saving Environment to %s...\n", env_name_spec); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 662 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 663 | return saveenv() ? 1 : 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 664 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 665 | |
Mike Frysinger | ba69dc2 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 666 | U_BOOT_CMD( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 667 | saveenv, 1, 0, do_env_save, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 668 | "save environment variables to persistent storage", |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 669 | "" |
Mike Frysinger | ba69dc2 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 670 | ); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 671 | #endif |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 672 | #endif /* CONFIG_SPL_BUILD */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 673 | |
| 674 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 675 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 676 | * Match a name / name=value pair |
| 677 | * |
| 678 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 679 | * i2 is the environment index for a 'name2=value2' pair. |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 680 | * If the names match, return the index for the value2, else -1. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 681 | */ |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 682 | int envmatch(uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 683 | { |
Joe Hershberger | 586197d | 2012-10-03 09:38:50 +0000 | [diff] [blame] | 684 | if (s1 == NULL) |
| 685 | return -1; |
| 686 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 687 | while (*s1 == env_get_char(i2++)) |
| 688 | if (*s1++ == '=') |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 689 | return i2; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 690 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 691 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 692 | return i2; |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 693 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 694 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 695 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 696 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 697 | #ifndef CONFIG_SPL_BUILD |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 698 | static int do_env_default(cmd_tbl_t *cmdtp, int __flag, |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 699 | int argc, char * const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 700 | { |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 701 | int all = 0, flag = 0; |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 702 | |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 703 | debug("Initial value for argc=%d\n", argc); |
| 704 | while (--argc > 0 && **++argv == '-') { |
| 705 | char *arg = *argv; |
| 706 | |
| 707 | while (*++arg) { |
| 708 | switch (*arg) { |
| 709 | case 'a': /* default all */ |
| 710 | all = 1; |
| 711 | break; |
| 712 | case 'f': /* force */ |
| 713 | flag |= H_FORCE; |
| 714 | break; |
| 715 | default: |
| 716 | return cmd_usage(cmdtp); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | debug("Final value for argc=%d\n", argc); |
| 721 | if (all && (argc == 0)) { |
| 722 | /* Reset the whole environment */ |
| 723 | set_default_env("## Resetting to default environment\n"); |
| 724 | return 0; |
| 725 | } |
| 726 | if (!all && (argc > 0)) { |
| 727 | /* Reset individual variables */ |
| 728 | set_default_vars(argc, argv); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | return cmd_usage(cmdtp); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 733 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 734 | |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 735 | static int do_env_delete(cmd_tbl_t *cmdtp, int flag, |
| 736 | int argc, char * const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 737 | { |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 738 | int env_flag = H_INTERACTIVE; |
| 739 | int ret = 0; |
| 740 | |
| 741 | debug("Initial value for argc=%d\n", argc); |
| 742 | while (argc > 1 && **(argv + 1) == '-') { |
| 743 | char *arg = *++argv; |
| 744 | |
| 745 | --argc; |
| 746 | while (*++arg) { |
| 747 | switch (*arg) { |
| 748 | case 'f': /* force */ |
| 749 | env_flag |= H_FORCE; |
| 750 | break; |
| 751 | default: |
| 752 | return CMD_RET_USAGE; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | debug("Final value for argc=%d\n", argc); |
| 757 | |
| 758 | env_id++; |
| 759 | |
| 760 | while (--argc > 0) { |
| 761 | char *name = *++argv; |
| 762 | |
| 763 | if (!hdelete_r(name, &env_htab, env_flag)) |
| 764 | ret = 1; |
| 765 | } |
| 766 | |
| 767 | return ret; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 770 | #ifdef CONFIG_CMD_EXPORTENV |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 771 | /* |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 772 | * env export [-t | -b | -c] [-s size] addr [var ...] |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 773 | * -t: export as text format; if size is given, data will be |
| 774 | * padded with '\0' bytes; if not, one terminating '\0' |
| 775 | * will be added (which is included in the "filesize" |
| 776 | * setting so you can for exmple copy this to flash and |
| 777 | * keep the termination). |
| 778 | * -b: export as binary format (name=value pairs separated by |
| 779 | * '\0', list end marked by double "\0\0") |
| 780 | * -c: export as checksum protected environment format as |
| 781 | * used for example by "saveenv" command |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 782 | * -s size: |
| 783 | * size of output buffer |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 784 | * addr: memory address where environment gets stored |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 785 | * var... List of variable names that get included into the |
| 786 | * export. Without arguments, the whole environment gets |
| 787 | * exported. |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 788 | * |
| 789 | * With "-c" and size is NOT given, then the export command will |
| 790 | * format the data as currently used for the persistent storage, |
| 791 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and |
| 792 | * prepend a valid CRC32 checksum and, in case of resundant |
| 793 | * environment, a "current" redundancy flag. If size is given, this |
| 794 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 |
| 795 | * checksum and redundancy flag will be inserted. |
| 796 | * |
| 797 | * With "-b" and "-t", always only the real data (including a |
| 798 | * terminating '\0' byte) will be written; here the optional size |
| 799 | * argument will be used to make sure not to overflow the user |
| 800 | * provided buffer; the command will abort if the size is not |
| 801 | * sufficient. Any remainign space will be '\0' padded. |
| 802 | * |
| 803 | * On successful return, the variable "filesize" will be set. |
| 804 | * Note that filesize includes the trailing/terminating '\0' byte(s). |
| 805 | * |
| 806 | * Usage szenario: create a text snapshot/backup of the current settings: |
| 807 | * |
| 808 | * => env export -t 100000 |
| 809 | * => era ${backup_addr} +${filesize} |
| 810 | * => cp.b 100000 ${backup_addr} ${filesize} |
| 811 | * |
| 812 | * Re-import this snapshot, deleting all other settings: |
| 813 | * |
| 814 | * => env import -d -t ${backup_addr} |
| 815 | */ |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 816 | static int do_env_export(cmd_tbl_t *cmdtp, int flag, |
| 817 | int argc, char * const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 818 | { |
| 819 | char buf[32]; |
| 820 | char *addr, *cmd, *res; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 821 | size_t size = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 822 | ssize_t len; |
| 823 | env_t *envp; |
| 824 | char sep = '\n'; |
| 825 | int chk = 0; |
| 826 | int fmt = 0; |
| 827 | |
| 828 | cmd = *argv; |
| 829 | |
| 830 | while (--argc > 0 && **++argv == '-') { |
| 831 | char *arg = *argv; |
| 832 | while (*++arg) { |
| 833 | switch (*arg) { |
| 834 | case 'b': /* raw binary format */ |
| 835 | if (fmt++) |
| 836 | goto sep_err; |
| 837 | sep = '\0'; |
| 838 | break; |
| 839 | case 'c': /* external checksum format */ |
| 840 | if (fmt++) |
| 841 | goto sep_err; |
| 842 | sep = '\0'; |
| 843 | chk = 1; |
| 844 | break; |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 845 | case 's': /* size given */ |
| 846 | if (--argc <= 0) |
| 847 | return cmd_usage(cmdtp); |
| 848 | size = simple_strtoul(*++argv, NULL, 16); |
| 849 | goto NXTARG; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 850 | case 't': /* text format */ |
| 851 | if (fmt++) |
| 852 | goto sep_err; |
| 853 | sep = '\n'; |
| 854 | break; |
| 855 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 856 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 857 | } |
| 858 | } |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 859 | NXTARG: ; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 860 | } |
| 861 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 862 | if (argc < 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 863 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 864 | |
| 865 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
| 866 | |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 867 | if (size) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 868 | memset(addr, '\0', size); |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 869 | |
| 870 | argc--; |
| 871 | argv++; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 872 | |
| 873 | if (sep) { /* export as text file */ |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 874 | len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 875 | if (len < 0) { |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 876 | error("Cannot export environment: errno = %d\n", errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 877 | return 1; |
| 878 | } |
Andreas Bießmann | 8c3aff5 | 2011-02-09 15:10:29 +0100 | [diff] [blame] | 879 | sprintf(buf, "%zX", (size_t)len); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 880 | setenv("filesize", buf); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | envp = (env_t *)addr; |
| 886 | |
| 887 | if (chk) /* export as checksum protected block */ |
| 888 | res = (char *)envp->data; |
| 889 | else /* export as raw binary data */ |
| 890 | res = addr; |
| 891 | |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 892 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 893 | if (len < 0) { |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 894 | error("Cannot export environment: errno = %d\n", errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 895 | return 1; |
| 896 | } |
| 897 | |
| 898 | if (chk) { |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 899 | envp->crc = crc32(0, envp->data, ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 900 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 901 | envp->flags = ACTIVE_FLAG; |
| 902 | #endif |
| 903 | } |
Simon Glass | 41ef372 | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 904 | setenv_hex("filesize", len + offsetof(env_t, data)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 905 | |
| 906 | return 0; |
| 907 | |
| 908 | sep_err: |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 909 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 910 | return 1; |
| 911 | } |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 912 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 913 | |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 914 | #ifdef CONFIG_CMD_IMPORTENV |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 915 | /* |
| 916 | * env import [-d] [-t | -b | -c] addr [size] |
| 917 | * -d: delete existing environment before importing; |
| 918 | * otherwise overwrite / append to existion definitions |
| 919 | * -t: assume text format; either "size" must be given or the |
| 920 | * text data must be '\0' terminated |
| 921 | * -b: assume binary format ('\0' separated, "\0\0" terminated) |
| 922 | * -c: assume checksum protected environment format |
| 923 | * addr: memory address to read from |
| 924 | * size: length of input data; if missing, proper '\0' |
| 925 | * termination is mandatory |
| 926 | */ |
Igor Grinberg | d09b178 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 927 | static int do_env_import(cmd_tbl_t *cmdtp, int flag, |
| 928 | int argc, char * const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 929 | { |
| 930 | char *cmd, *addr; |
| 931 | char sep = '\n'; |
| 932 | int chk = 0; |
| 933 | int fmt = 0; |
| 934 | int del = 0; |
| 935 | size_t size; |
| 936 | |
| 937 | cmd = *argv; |
| 938 | |
| 939 | while (--argc > 0 && **++argv == '-') { |
| 940 | char *arg = *argv; |
| 941 | while (*++arg) { |
| 942 | switch (*arg) { |
| 943 | case 'b': /* raw binary format */ |
| 944 | if (fmt++) |
| 945 | goto sep_err; |
| 946 | sep = '\0'; |
| 947 | break; |
| 948 | case 'c': /* external checksum format */ |
| 949 | if (fmt++) |
| 950 | goto sep_err; |
| 951 | sep = '\0'; |
| 952 | chk = 1; |
| 953 | break; |
| 954 | case 't': /* text format */ |
| 955 | if (fmt++) |
| 956 | goto sep_err; |
| 957 | sep = '\n'; |
| 958 | break; |
| 959 | case 'd': |
| 960 | del = 1; |
| 961 | break; |
| 962 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 963 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 968 | if (argc < 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 969 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 970 | |
| 971 | if (!fmt) |
| 972 | printf("## Warning: defaulting to text format\n"); |
| 973 | |
| 974 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
| 975 | |
| 976 | if (argc == 2) { |
| 977 | size = simple_strtoul(argv[1], NULL, 16); |
| 978 | } else { |
| 979 | char *s = addr; |
| 980 | |
| 981 | size = 0; |
| 982 | |
| 983 | while (size < MAX_ENV_SIZE) { |
| 984 | if ((*s == sep) && (*(s+1) == '\0')) |
| 985 | break; |
| 986 | ++s; |
| 987 | ++size; |
| 988 | } |
| 989 | if (size == MAX_ENV_SIZE) { |
| 990 | printf("## Warning: Input data exceeds %d bytes" |
| 991 | " - truncated\n", MAX_ENV_SIZE); |
| 992 | } |
Horst Kronstorfer | d3f80c7 | 2011-12-16 23:33:10 +0000 | [diff] [blame] | 993 | size += 2; |
Simon Glass | 79afc88 | 2011-11-04 06:42:36 +0000 | [diff] [blame] | 994 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | if (chk) { |
| 998 | uint32_t crc; |
| 999 | env_t *ep = (env_t *)addr; |
| 1000 | |
| 1001 | size -= offsetof(env_t, data); |
| 1002 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 1003 | |
| 1004 | if (crc32(0, ep->data, size) != crc) { |
| 1005 | puts("## Error: bad CRC, import failed\n"); |
| 1006 | return 1; |
| 1007 | } |
| 1008 | addr = (char *)ep->data; |
| 1009 | } |
| 1010 | |
Gerlando Falauto | 348b1f1 | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 1011 | if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 1012 | 0, NULL) == 0) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1013 | error("Environment import failed: errno = %d\n", errno); |
| 1014 | return 1; |
| 1015 | } |
| 1016 | gd->flags |= GD_FLG_ENV_READY; |
| 1017 | |
| 1018 | return 0; |
| 1019 | |
| 1020 | sep_err: |
| 1021 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 1022 | cmd); |
| 1023 | return 1; |
| 1024 | } |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1025 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1026 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1027 | /* |
| 1028 | * New command line interface: "env" command with subcommands |
| 1029 | */ |
| 1030 | static cmd_tbl_t cmd_env_sub[] = { |
| 1031 | #if defined(CONFIG_CMD_ASKENV) |
| 1032 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), |
| 1033 | #endif |
| 1034 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1035 | U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1036 | #if defined(CONFIG_CMD_EDITENV) |
| 1037 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), |
| 1038 | #endif |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1039 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1040 | U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), |
| 1041 | #endif |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1042 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1043 | U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), |
| 1044 | #endif |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1045 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1046 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1047 | #endif |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1048 | #if defined(CONFIG_CMD_GREPENV) |
| 1049 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), |
| 1050 | #endif |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1051 | #if defined(CONFIG_CMD_IMPORTENV) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1052 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
Mike Frysinger | 0c79cda | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1053 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1054 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
| 1055 | #if defined(CONFIG_CMD_RUN) |
| 1056 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), |
| 1057 | #endif |
| 1058 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
| 1059 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), |
| 1060 | #endif |
| 1061 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), |
| 1062 | }; |
| 1063 | |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 1064 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 60f7da1 | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 1065 | void env_reloc(void) |
| 1066 | { |
| 1067 | fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1068 | } |
| 1069 | #endif |
| 1070 | |
Macpaul Lin | f3c615b | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 1071 | static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1072 | { |
| 1073 | cmd_tbl_t *cp; |
| 1074 | |
Thomas Weber | 5904da0 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1075 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1076 | return CMD_RET_USAGE; |
Thomas Weber | 5904da0 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1077 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1078 | /* drop initial "env" arg */ |
| 1079 | argc--; |
| 1080 | argv++; |
| 1081 | |
| 1082 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1083 | |
| 1084 | if (cp) |
| 1085 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1086 | |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1087 | return CMD_RET_USAGE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1090 | #ifdef CONFIG_SYS_LONGHELP |
| 1091 | static char env_help_text[] = |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1092 | #if defined(CONFIG_CMD_ASKENV) |
| 1093 | "ask name [message] [size] - ask for environment variable\nenv " |
| 1094 | #endif |
Joe Hershberger | 5e2b3e0 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1095 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1096 | "callbacks - print callbacks and their associated variables\nenv " |
| 1097 | #endif |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 1098 | "default [-f] -a - [forcibly] reset default environment\n" |
| 1099 | "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" |
Joe Hershberger | 9d8d661 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1100 | "env delete [-f] var [...] - [forcibly] delete variable(s)\n" |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1101 | #if defined(CONFIG_CMD_EDITENV) |
| 1102 | "env edit name - edit environment variable\n" |
| 1103 | #endif |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1104 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | 37f2fe7 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 1105 | "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1106 | #endif |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1107 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1108 | "env flags - print variables that have non-default flags\n" |
| 1109 | #endif |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1110 | #if defined(CONFIG_CMD_GREPENV) |
| 1111 | "env grep string [...] - search environment\n" |
| 1112 | #endif |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1113 | #if defined(CONFIG_CMD_IMPORTENV) |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1114 | "env import [-d] [-t | -b | -c] addr [size] - import environment\n" |
Benoît Thébaudeau | 4796bc4 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1115 | #endif |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1116 | "env print [-a | name ...] - print environment\n" |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1117 | #if defined(CONFIG_CMD_RUN) |
| 1118 | "env run var [...] - run commands in an environment variable\n" |
| 1119 | #endif |
Horst Kronstorfer | d798a9b | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 1120 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1121 | "env save - save environment\n" |
Horst Kronstorfer | d798a9b | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 1122 | #endif |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1123 | "env set [-f] name [arg ...]\n"; |
| 1124 | #endif |
| 1125 | |
| 1126 | U_BOOT_CMD( |
| 1127 | env, CONFIG_SYS_MAXARGS, 1, do_env, |
| 1128 | "environment handling commands", env_help_text |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1129 | ); |
| 1130 | |
| 1131 | /* |
| 1132 | * Old command line interface, kept for compatibility |
| 1133 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1134 | |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1135 | #if defined(CONFIG_CMD_EDITENV) |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1136 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1137 | editenv, 2, 0, do_env_edit, |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1138 | "edit environment variable", |
| 1139 | "name\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1140 | " - edit environment variable 'name'", |
| 1141 | var_complete |
Peter Tyser | 246c692 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1142 | ); |
| 1143 | #endif |
| 1144 | |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1145 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1146 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1147 | "print environment variables", |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1148 | "[-a]\n - print [all] values of all environment variables\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1149 | "printenv name ...\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1150 | " - print value of environment variable 'name'", |
| 1151 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1152 | ); |
| 1153 | |
Kim Phillips | a000b79 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1154 | #ifdef CONFIG_CMD_GREPENV |
| 1155 | U_BOOT_CMD_COMPLETE( |
| 1156 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, |
| 1157 | "search environment variables", |
| 1158 | "string ...\n" |
| 1159 | " - list environment name=value pairs matching 'string'", |
| 1160 | var_complete |
| 1161 | ); |
| 1162 | #endif |
| 1163 | |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1164 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1165 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1166 | "set environment variables", |
Joe Hershberger | 24ab5a1 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 1167 | "[-f] name value ...\n" |
| 1168 | " - [forcibly] set environment variable 'name' to 'value ...'\n" |
| 1169 | "setenv [-f] name\n" |
| 1170 | " - [forcibly] delete environment variable 'name'", |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1171 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1172 | ); |
| 1173 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1174 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1175 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1176 | U_BOOT_CMD( |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1177 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1178 | "get environment variables from stdin", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1179 | "name [message] [size]\n" |
Wolfgang Denk | 7d85591 | 2013-02-20 04:53:16 +0000 | [diff] [blame] | 1180 | " - get environment variable 'name' from stdin (max 'size' chars)" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1181 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1182 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1183 | |
Jon Loeliger | c76fe47 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1184 | #if defined(CONFIG_CMD_RUN) |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1185 | U_BOOT_CMD_COMPLETE( |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1186 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1187 | "run commands in an environment variable", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1188 | "var [...]\n" |
Mike Frysinger | 722b061 | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1189 | " - run the commands in the environment variable(s) 'var'", |
| 1190 | var_complete |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1191 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1192 | #endif |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 1193 | #endif /* CONFIG_SPL_BUILD */ |