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