wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 7 | * |
| 8 | * Copyright 2011 Freescale Semiconductor, Inc. |
| 9 | * |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 29 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 30 | * Support for persistent environment data |
| 31 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | * The "environment" is stored on external storage as a list of '\0' |
| 33 | * terminated "name=value" strings. The end of the list is marked by |
| 34 | * a double '\0'. The environment is preceeded by a 32 bit CRC over |
| 35 | * the data part and, in case of redundant environment, a byte of |
| 36 | * flags. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 37 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 38 | * This linearized representation will also be used before |
| 39 | * relocation, i. e. as long as we don't have a full C runtime |
| 40 | * environment. After that, we use a hash table. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 41 | */ |
| 42 | |
| 43 | #include <common.h> |
| 44 | #include <command.h> |
| 45 | #include <environment.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 46 | #include <search.h> |
| 47 | #include <errno.h> |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 48 | #include <malloc.h> |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 49 | #include <watchdog.h> |
wdenk | 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 | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 53 | DECLARE_GLOBAL_DATA_PTR; |
| 54 | |
Macpaul Lin | ff2a9f3 | 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 | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 58 | !defined(CONFIG_ENV_IS_IN_MMC) && \ |
Maximilian Schwerin | 9765c08 | 2012-03-12 23:57:50 +0000 | [diff] [blame] | 59 | !defined(CONFIG_ENV_IS_IN_FAT) && \ |
Macpaul Lin | ff2a9f3 | 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 | 85bcd73 | 2012-03-08 00:33:20 +0000 | [diff] [blame] | 64 | !defined(CONFIG_ENV_IS_IN_REMOTE) && \ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 65 | !defined(CONFIG_ENV_IS_NOWHERE) |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 66 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\ |
Marek Vasut | 36437bc | 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 | 460a9ff | 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 | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 76 | * This variable is incremented on each do_env_set(), so it can |
Heiko Schocher | 3b195ff | 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 | 0c303fa | 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 | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 84 | int get_env_id(void) |
Heiko Schocher | 0c303fa | 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 | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 89 | #ifndef CONFIG_SPL_BUILD |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 90 | /* |
Wolfgang Denk | 460a9ff | 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 | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 94 | */ |
Joe Hershberger | 79a905e | 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 | 460a9ff | 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 | 460a9ff | 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 | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | e.key = name; |
| 104 | e.data = NULL; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 105 | hsearch_r(e, FIND, &ep, &env_htab, flag); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 106 | if (ep == NULL) |
| 107 | return 0; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 108 | len = printf("%s=%s\n", ep->key, ep->data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 109 | return len; |
| 110 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 111 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 112 | /* print whole list */ |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 113 | len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 114 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 115 | if (len > 0) { |
| 116 | puts(res); |
| 117 | free(res); |
| 118 | return len; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 121 | /* should never happen */ |
| 122 | return 0; |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 123 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 124 | |
Kim Phillips | dc00a68 | 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 | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 127 | { |
| 128 | int i; |
| 129 | int rcode = 0; |
Joe Hershberger | 79a905e | 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 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 137 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 138 | if (argc == 1) { |
| 139 | /* print all env vars */ |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 140 | rcode = env_print(NULL, env_flag); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 141 | if (!rcode) |
Mike Frysinger | 8794492 | 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); |
| 145 | return 0; |
| 146 | } |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 147 | |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 148 | /* print selected env vars */ |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 149 | env_flag &= ~H_HIDE_DOT; |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 150 | for (i = 1; i < argc; ++i) { |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 151 | int rc = env_print(argv[i], env_flag); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 152 | if (!rc) { |
| 153 | printf("## Error: \"%s\" not defined\n", argv[i]); |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 154 | ++rcode; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 155 | } |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return rcode; |
| 159 | } |
| 160 | |
| 161 | #ifdef CONFIG_CMD_GREPENV |
Igor Grinberg | de83256 | 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 | f0c9d53 | 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 | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 170 | return CMD_RET_USAGE; |
Kim Phillips | f0c9d53 | 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 | e7c338b | 2011-11-23 09:48:02 +0000 | [diff] [blame] | 176 | while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) { |
Kim Phillips | f0c9d53 | 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++; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 187 | } |
Mike Frysinger | 8794492 | 2009-05-24 02:26:19 -0400 | [diff] [blame] | 188 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 189 | return rcode; |
| 190 | } |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 191 | #endif |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 192 | #endif /* CONFIG_SPL_BUILD */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 193 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 194 | /* |
Gerlando Falauto | dc88d59 | 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 | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 197 | */ |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 198 | static int _do_env_set(int flag, int argc, char * const argv[]) |
Gerlando Falauto | dc88d59 | 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 | f64bb77 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 203 | int env_flag = H_INTERACTIVE; |
Gerlando Falauto | dc88d59 | 2012-08-24 00:11:37 +0000 | [diff] [blame] | 204 | |
Joe Hershberger | f64bb77 | 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 | dc88d59 | 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++; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 231 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 232 | /* Delete only ? */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 233 | if (argc < 3 || argv[2] == NULL) { |
Joe Hershberger | f64bb77 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 234 | int rc = hdelete_r(name, &env_htab, env_flag); |
Wolfgang Denk | 460a9ff | 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 | 460a9ff | 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 | ff2a9f3 | 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 | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 243 | |
| 244 | value = malloc(len); |
| 245 | if (value == NULL) { |
Wolfgang Denk | 460a9ff | 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 | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 249 | for (i = 2, s = value; i < argc; ++i) { |
Wolfgang Denk | 460a9ff | 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 | 460a9ff | 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 | de83256 | 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 | 460a9ff | 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 | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 259 | e.key = name; |
| 260 | e.data = value; |
Joe Hershberger | f64bb77 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 261 | hsearch_r(e, ENTER, &ep, &env_htab, env_flag); |
Wolfgang Denk | 460a9ff | 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 | d67abff | 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 | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 274 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
| 275 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 276 | if (varvalue == NULL || varvalue[0] == '\0') |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 277 | return _do_env_set(0, 2, (char * const *)argv); |
Jeffrey Mann | 62b896f | 2007-04-23 14:00:11 +0200 | [diff] [blame] | 278 | else |
Wolfgang Denk | d67abff | 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 | b8e926e | 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 | 044dc9e | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 298 | * Set an environment variable to an value in hex |
Simon Glass | b8e926e | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 299 | * |
| 300 | * @param varname Environmet variable to set |
Simon Glass | 044dc9e | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 301 | * @param value Value to set it to |
Simon Glass | b8e926e | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 302 | * @return 0 if ok, 1 on error |
| 303 | */ |
Simon Glass | 044dc9e | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 304 | int setenv_hex(const char *varname, ulong value) |
Simon Glass | b8e926e | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 305 | { |
| 306 | char str[17]; |
| 307 | |
Simon Glass | 044dc9e | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 308 | sprintf(str, "%lx", value); |
Simon Glass | b8e926e | 2011-10-24 17:59:59 +0000 | [diff] [blame] | 309 | return setenv(varname, str); |
| 310 | } |
| 311 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 312 | #ifndef CONFIG_SPL_BUILD |
Kim Phillips | dc00a68 | 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 | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 315 | if (argc < 2) |
Simon Glass | a06dfc7 | 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 | 460a9ff | 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 | 460a9ff | 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 | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 324 | #if defined(CONFIG_CMD_ASKENV) |
Macpaul Lin | ff2a9f3 | 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 | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 327 | char message[CONFIG_SYS_CBSIZE]; |
| 328 | int size = CONFIG_SYS_CBSIZE - 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 329 | int i, len, pos; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 330 | char *local_args[4]; |
| 331 | |
| 332 | local_args[0] = argv[0]; |
| 333 | local_args[1] = argv[1]; |
| 334 | local_args[2] = NULL; |
| 335 | local_args[3] = NULL; |
| 336 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 337 | /* Check the syntax */ |
| 338 | switch (argc) { |
| 339 | case 1: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 340 | return CMD_RET_USAGE; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 341 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 342 | case 2: /* env_ask envname */ |
| 343 | sprintf(message, "Please enter '%s':", argv[1]); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 344 | break; |
| 345 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 346 | case 3: /* env_ask envname size */ |
| 347 | sprintf(message, "Please enter '%s':", argv[1]); |
| 348 | size = simple_strtoul(argv[2], NULL, 10); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 351 | default: /* env_ask envname message1 ... messagen size */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 352 | for (i = 2, pos = 0; i < argc - 1; i++) { |
| 353 | if (pos) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 354 | message[pos++] = ' '; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 355 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 356 | strcpy(message + pos, argv[i]); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 357 | pos += strlen(argv[i]); |
| 358 | } |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 359 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 360 | message[pos] = '\0'; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 361 | size = simple_strtoul(argv[argc - 1], NULL, 10); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 365 | if (size >= CONFIG_SYS_CBSIZE) |
| 366 | size = CONFIG_SYS_CBSIZE - 1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 367 | |
| 368 | if (size <= 0) |
| 369 | return 1; |
| 370 | |
| 371 | /* prompt for input */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 372 | len = readline(message); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 373 | |
| 374 | if (size < len) |
| 375 | console_buffer[size] = '\0'; |
| 376 | |
| 377 | len = 2; |
| 378 | if (console_buffer[0] != '\0') { |
| 379 | local_args[2] = console_buffer; |
| 380 | len = 3; |
| 381 | } |
| 382 | |
| 383 | /* Continue calling setenv code */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 384 | return _do_env_set(flag, len, local_args); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 385 | } |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 386 | #endif |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 387 | |
Joe Hershberger | 1b0d551 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 388 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 389 | static int print_static_binding(const char *var_name, const char *callback_name) |
| 390 | { |
| 391 | printf("\t%-20s %-20s\n", var_name, callback_name); |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int print_active_callback(ENTRY *entry) |
| 397 | { |
| 398 | struct env_clbk_tbl *clbkp; |
| 399 | int i; |
| 400 | int num_callbacks; |
| 401 | |
| 402 | if (entry->callback == NULL) |
| 403 | return 0; |
| 404 | |
| 405 | /* look up the callback in the linker-list */ |
| 406 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 407 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 408 | i < num_callbacks; |
| 409 | i++, clbkp++) { |
| 410 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 411 | if (entry->callback == clbkp->callback + gd->reloc_off) |
| 412 | #else |
| 413 | if (entry->callback == clbkp->callback) |
| 414 | #endif |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | if (i == num_callbacks) |
| 419 | /* this should probably never happen, but just in case... */ |
| 420 | printf("\t%-20s %p\n", entry->key, entry->callback); |
| 421 | else |
| 422 | printf("\t%-20s %-20s\n", entry->key, clbkp->name); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Print the callbacks available and what they are bound to |
| 429 | */ |
| 430 | int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 431 | { |
| 432 | struct env_clbk_tbl *clbkp; |
| 433 | int i; |
| 434 | int num_callbacks; |
| 435 | |
| 436 | /* Print the available callbacks */ |
| 437 | puts("Available callbacks:\n"); |
| 438 | puts("\tCallback Name\n"); |
| 439 | puts("\t-------------\n"); |
| 440 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); |
| 441 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); |
| 442 | i < num_callbacks; |
| 443 | i++, clbkp++) |
| 444 | printf("\t%s\n", clbkp->name); |
| 445 | puts("\n"); |
| 446 | |
| 447 | /* Print the static bindings that may exist */ |
| 448 | puts("Static callback bindings:\n"); |
| 449 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 450 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
| 451 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding); |
| 452 | puts("\n"); |
| 453 | |
| 454 | /* walk through each variable and print the callback if it has one */ |
| 455 | puts("Active callback bindings:\n"); |
| 456 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); |
| 457 | printf("\t%-20s %-20s\n", "-------------", "-------------"); |
| 458 | hwalk_r(&env_htab, print_active_callback); |
| 459 | return 0; |
| 460 | } |
| 461 | #endif |
| 462 | |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 463 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 464 | static int print_static_flags(const char *var_name, const char *flags) |
| 465 | { |
| 466 | enum env_flags_vartype type = env_flags_parse_vartype(flags); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 467 | enum env_flags_varaccess access = env_flags_parse_varaccess(flags); |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 468 | |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 469 | printf("\t%-20s %-20s %-20s\n", var_name, |
| 470 | env_flags_get_vartype_name(type), |
| 471 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int print_active_flags(ENTRY *entry) |
| 477 | { |
| 478 | enum env_flags_vartype type; |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 479 | enum env_flags_varaccess access; |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 480 | |
| 481 | if (entry->flags == 0) |
| 482 | return 0; |
| 483 | |
| 484 | type = (enum env_flags_vartype) |
| 485 | (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 486 | access = env_flags_parse_varaccess_from_binflags(entry->flags); |
| 487 | printf("\t%-20s %-20s %-20s\n", entry->key, |
| 488 | env_flags_get_vartype_name(type), |
| 489 | env_flags_get_varaccess_name(access)); |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Print the flags available and what variables have flags |
| 496 | */ |
| 497 | int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 498 | { |
| 499 | /* Print the available variable types */ |
| 500 | printf("Available variable type flags (position %d):\n", |
| 501 | ENV_FLAGS_VARTYPE_LOC); |
| 502 | puts("\tFlag\tVariable Type Name\n"); |
| 503 | puts("\t----\t------------------\n"); |
| 504 | env_flags_print_vartypes(); |
| 505 | puts("\n"); |
| 506 | |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 507 | /* Print the available variable access types */ |
| 508 | printf("Available variable access flags (position %d):\n", |
| 509 | ENV_FLAGS_VARACCESS_LOC); |
| 510 | puts("\tFlag\tVariable Access Name\n"); |
| 511 | puts("\t----\t--------------------\n"); |
| 512 | env_flags_print_varaccess(); |
| 513 | puts("\n"); |
| 514 | |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 515 | /* Print the static flags that may exist */ |
| 516 | puts("Static flags:\n"); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 517 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
| 518 | "Variable Access"); |
| 519 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", |
| 520 | "---------------"); |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 521 | env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags); |
| 522 | puts("\n"); |
| 523 | |
| 524 | /* walk through each variable and print the flags if non-default */ |
| 525 | puts("Active flags:\n"); |
Joe Hershberger | 6fe26c9 | 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 | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 530 | hwalk_r(&env_htab, print_active_flags); |
| 531 | return 0; |
| 532 | } |
| 533 | #endif |
| 534 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 535 | /* |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 536 | * Interactively edit an environment variable |
| 537 | */ |
| 538 | #if defined(CONFIG_CMD_EDITENV) |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 539 | static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, |
| 540 | char * const argv[]) |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 541 | { |
| 542 | char buffer[CONFIG_SYS_CBSIZE]; |
| 543 | char *init_val; |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 544 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 545 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 546 | return CMD_RET_USAGE; |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 547 | |
| 548 | /* Set read buffer to initial value or empty sting */ |
| 549 | init_val = getenv(argv[1]); |
| 550 | if (init_val) |
Marek Vasut | 305e02a | 2011-09-26 02:26:03 +0200 | [diff] [blame] | 551 | sprintf(buffer, "%s", init_val); |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 552 | else |
| 553 | buffer[0] = '\0'; |
| 554 | |
Joe Hershberger | 91d1b5f | 2013-02-08 10:12:34 +0000 | [diff] [blame] | 555 | if (readline_into_buffer("edit: ", buffer, 0) < 0) |
| 556 | return 1; |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 557 | |
| 558 | return setenv(argv[1], buffer); |
| 559 | } |
| 560 | #endif /* CONFIG_CMD_EDITENV */ |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 561 | #endif /* CONFIG_SPL_BUILD */ |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 562 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 563 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 564 | * Look up variable from environment, |
| 565 | * return address of storage for that variable, |
| 566 | * or NULL if not found |
| 567 | */ |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 568 | char *getenv(const char *name) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 569 | { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 570 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 571 | ENTRY e, *ep; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 572 | |
Wolfgang Denk | f710efd | 2010-07-24 20:22:02 +0200 | [diff] [blame] | 573 | WATCHDOG_RESET(); |
wdenk | 2a3cb02 | 2002-11-05 21:01:48 +0000 | [diff] [blame] | 574 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 575 | e.key = name; |
| 576 | e.data = NULL; |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 577 | hsearch_r(e, FIND, &ep, &env_htab, 0); |
Wolfgang Denk | f710efd | 2010-07-24 20:22:02 +0200 | [diff] [blame] | 578 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 579 | return ep ? ep->data : NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 582 | /* restricted capabilities before import */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 583 | if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) |
| 584 | return (char *)(gd->env_buf); |
| 585 | |
| 586 | return NULL; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 589 | /* |
| 590 | * Look up variable from environment for restricted C runtime env. |
| 591 | */ |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 592 | int getenv_f(const char *name, char *buf, unsigned len) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 593 | { |
| 594 | int i, nxt; |
| 595 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 596 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 597 | int val, n; |
| 598 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 599 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) { |
| 600 | if (nxt >= CONFIG_ENV_SIZE) |
| 601 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 602 | } |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 603 | |
| 604 | val = envmatch((uchar *)name, i); |
| 605 | if (val < 0) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 606 | continue; |
Wolfgang Denk | 7a060a7 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 607 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 608 | /* found; copy out */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 609 | for (n = 0; n < len; ++n, ++buf) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 610 | *buf = env_get_char(val++); |
| 611 | if (*buf == '\0') |
Wolfgang Denk | 7a060a7 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 612 | return n; |
| 613 | } |
| 614 | |
| 615 | if (n) |
| 616 | *--buf = '\0'; |
| 617 | |
Wolfgang Denk | 4328547 | 2011-05-04 10:29:49 +0000 | [diff] [blame] | 618 | printf("env_buf [%d bytes] too small for value of \"%s\"\n", |
| 619 | len, name); |
Wolfgang Denk | 7a060a7 | 2010-07-24 22:16:20 +0200 | [diff] [blame] | 620 | |
| 621 | return n; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 622 | } |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 623 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 624 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Simon Glass | bf690f8 | 2011-10-14 13:25:18 +0000 | [diff] [blame] | 627 | /** |
| 628 | * Decode the integer value of an environment variable and return it. |
| 629 | * |
| 630 | * @param name Name of environemnt variable |
| 631 | * @param base Number base to use (normally 10, or 16 for hex) |
| 632 | * @param default_val Default value to return if the variable is not |
| 633 | * found |
| 634 | * @return the decoded value, or default_val if not found |
| 635 | */ |
| 636 | ulong getenv_ulong(const char *name, int base, ulong default_val) |
| 637 | { |
| 638 | /* |
| 639 | * We can use getenv() here, even before relocation, since the |
| 640 | * environment variable value is an integer and thus short. |
| 641 | */ |
| 642 | const char *str = getenv(name); |
| 643 | |
| 644 | return str ? simple_strtoul(str, NULL, base) : default_val; |
| 645 | } |
| 646 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 647 | #ifndef CONFIG_SPL_BUILD |
Mike Frysinger | 78dcaf4 | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 648 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 649 | static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, |
| 650 | char * const argv[]) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 651 | { |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 652 | printf("Saving Environment to %s...\n", env_name_spec); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 653 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 654 | return saveenv() ? 1 : 0; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 655 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 656 | |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 657 | U_BOOT_CMD( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 658 | saveenv, 1, 0, do_env_save, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 659 | "save environment variables to persistent storage", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 660 | "" |
Mike Frysinger | 6158393 | 2008-12-30 02:59:25 -0500 | [diff] [blame] | 661 | ); |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 662 | #endif |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 663 | #endif /* CONFIG_SPL_BUILD */ |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 664 | |
| 665 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 666 | /* |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 667 | * Match a name / name=value pair |
| 668 | * |
| 669 | * s1 is either a simple 'name', or a 'name=value' pair. |
| 670 | * i2 is the environment index for a 'name2=value2' pair. |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 671 | * If the names match, return the index for the value2, else -1. |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 672 | */ |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 673 | int envmatch(uchar *s1, int i2) |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 674 | { |
Joe Hershberger | dd000dd | 2012-10-03 09:38:50 +0000 | [diff] [blame] | 675 | if (s1 == NULL) |
| 676 | return -1; |
| 677 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 678 | while (*s1 == env_get_char(i2++)) |
| 679 | if (*s1++ == '=') |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 680 | return i2; |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 681 | |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 682 | if (*s1 == '\0' && env_get_char(i2-1) == '=') |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 683 | return i2; |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 684 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 685 | return -1; |
wdenk | a68d3ed | 2002-10-11 08:38:32 +0000 | [diff] [blame] | 686 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 687 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 688 | #ifndef CONFIG_SPL_BUILD |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 689 | static int do_env_default(cmd_tbl_t *cmdtp, int __flag, |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 690 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 691 | { |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 692 | int all = 0, flag = 0; |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 693 | |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 694 | debug("Initial value for argc=%d\n", argc); |
| 695 | while (--argc > 0 && **++argv == '-') { |
| 696 | char *arg = *argv; |
| 697 | |
| 698 | while (*++arg) { |
| 699 | switch (*arg) { |
| 700 | case 'a': /* default all */ |
| 701 | all = 1; |
| 702 | break; |
| 703 | case 'f': /* force */ |
| 704 | flag |= H_FORCE; |
| 705 | break; |
| 706 | default: |
| 707 | return cmd_usage(cmdtp); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | debug("Final value for argc=%d\n", argc); |
| 712 | if (all && (argc == 0)) { |
| 713 | /* Reset the whole environment */ |
| 714 | set_default_env("## Resetting to default environment\n"); |
| 715 | return 0; |
| 716 | } |
| 717 | if (!all && (argc > 0)) { |
| 718 | /* Reset individual variables */ |
| 719 | set_default_vars(argc, argv); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | return cmd_usage(cmdtp); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 726 | static int do_env_delete(cmd_tbl_t *cmdtp, int flag, |
| 727 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 728 | { |
Joe Hershberger | 8335ce7 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 729 | int env_flag = H_INTERACTIVE; |
| 730 | int ret = 0; |
| 731 | |
| 732 | debug("Initial value for argc=%d\n", argc); |
| 733 | while (argc > 1 && **(argv + 1) == '-') { |
| 734 | char *arg = *++argv; |
| 735 | |
| 736 | --argc; |
| 737 | while (*++arg) { |
| 738 | switch (*arg) { |
| 739 | case 'f': /* force */ |
| 740 | env_flag |= H_FORCE; |
| 741 | break; |
| 742 | default: |
| 743 | return CMD_RET_USAGE; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | debug("Final value for argc=%d\n", argc); |
| 748 | |
| 749 | env_id++; |
| 750 | |
| 751 | while (--argc > 0) { |
| 752 | char *name = *++argv; |
| 753 | |
| 754 | if (!hdelete_r(name, &env_htab, env_flag)) |
| 755 | ret = 1; |
| 756 | } |
| 757 | |
| 758 | return ret; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 761 | #ifdef CONFIG_CMD_EXPORTENV |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 762 | /* |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 763 | * env export [-t | -b | -c] [-s size] addr [var ...] |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 764 | * -t: export as text format; if size is given, data will be |
| 765 | * padded with '\0' bytes; if not, one terminating '\0' |
| 766 | * will be added (which is included in the "filesize" |
| 767 | * setting so you can for exmple copy this to flash and |
| 768 | * keep the termination). |
| 769 | * -b: export as binary format (name=value pairs separated by |
| 770 | * '\0', list end marked by double "\0\0") |
| 771 | * -c: export as checksum protected environment format as |
| 772 | * used for example by "saveenv" command |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 773 | * -s size: |
| 774 | * size of output buffer |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 775 | * addr: memory address where environment gets stored |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 776 | * var... List of variable names that get included into the |
| 777 | * export. Without arguments, the whole environment gets |
| 778 | * exported. |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 779 | * |
| 780 | * With "-c" and size is NOT given, then the export command will |
| 781 | * format the data as currently used for the persistent storage, |
| 782 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and |
| 783 | * prepend a valid CRC32 checksum and, in case of resundant |
| 784 | * environment, a "current" redundancy flag. If size is given, this |
| 785 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 |
| 786 | * checksum and redundancy flag will be inserted. |
| 787 | * |
| 788 | * With "-b" and "-t", always only the real data (including a |
| 789 | * terminating '\0' byte) will be written; here the optional size |
| 790 | * argument will be used to make sure not to overflow the user |
| 791 | * provided buffer; the command will abort if the size is not |
| 792 | * sufficient. Any remainign space will be '\0' padded. |
| 793 | * |
| 794 | * On successful return, the variable "filesize" will be set. |
| 795 | * Note that filesize includes the trailing/terminating '\0' byte(s). |
| 796 | * |
| 797 | * Usage szenario: create a text snapshot/backup of the current settings: |
| 798 | * |
| 799 | * => env export -t 100000 |
| 800 | * => era ${backup_addr} +${filesize} |
| 801 | * => cp.b 100000 ${backup_addr} ${filesize} |
| 802 | * |
| 803 | * Re-import this snapshot, deleting all other settings: |
| 804 | * |
| 805 | * => env import -d -t ${backup_addr} |
| 806 | */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 807 | static int do_env_export(cmd_tbl_t *cmdtp, int flag, |
| 808 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 809 | { |
| 810 | char buf[32]; |
| 811 | char *addr, *cmd, *res; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 812 | size_t size = 0; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 813 | ssize_t len; |
| 814 | env_t *envp; |
| 815 | char sep = '\n'; |
| 816 | int chk = 0; |
| 817 | int fmt = 0; |
| 818 | |
| 819 | cmd = *argv; |
| 820 | |
| 821 | while (--argc > 0 && **++argv == '-') { |
| 822 | char *arg = *argv; |
| 823 | while (*++arg) { |
| 824 | switch (*arg) { |
| 825 | case 'b': /* raw binary format */ |
| 826 | if (fmt++) |
| 827 | goto sep_err; |
| 828 | sep = '\0'; |
| 829 | break; |
| 830 | case 'c': /* external checksum format */ |
| 831 | if (fmt++) |
| 832 | goto sep_err; |
| 833 | sep = '\0'; |
| 834 | chk = 1; |
| 835 | break; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 836 | case 's': /* size given */ |
| 837 | if (--argc <= 0) |
| 838 | return cmd_usage(cmdtp); |
| 839 | size = simple_strtoul(*++argv, NULL, 16); |
| 840 | goto NXTARG; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 841 | case 't': /* text format */ |
| 842 | if (fmt++) |
| 843 | goto sep_err; |
| 844 | sep = '\n'; |
| 845 | break; |
| 846 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 847 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 848 | } |
| 849 | } |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 850 | NXTARG: ; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 853 | if (argc < 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 854 | return CMD_RET_USAGE; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 855 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 856 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
| 857 | |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 858 | if (size) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 859 | memset(addr, '\0', size); |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 860 | |
| 861 | argc--; |
| 862 | argv++; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 863 | |
| 864 | if (sep) { /* export as text file */ |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 865 | len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 866 | if (len < 0) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 867 | error("Cannot export environment: errno = %d\n", errno); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 868 | return 1; |
| 869 | } |
Andreas Bießmann | 92b9b2e | 2011-02-09 15:10:29 +0100 | [diff] [blame] | 870 | sprintf(buf, "%zX", (size_t)len); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 871 | setenv("filesize", buf); |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | envp = (env_t *)addr; |
| 877 | |
| 878 | if (chk) /* export as checksum protected block */ |
| 879 | res = (char *)envp->data; |
| 880 | else /* export as raw binary data */ |
| 881 | res = addr; |
| 882 | |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 883 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 884 | if (len < 0) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 885 | error("Cannot export environment: errno = %d\n", errno); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 886 | return 1; |
| 887 | } |
| 888 | |
| 889 | if (chk) { |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 890 | envp->crc = crc32(0, envp->data, ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 891 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 892 | envp->flags = ACTIVE_FLAG; |
| 893 | #endif |
| 894 | } |
Simon Glass | 103789d | 2013-02-24 17:33:22 +0000 | [diff] [blame] | 895 | setenv_hex("filesize", len + offsetof(env_t, data)); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 896 | |
| 897 | return 0; |
| 898 | |
| 899 | sep_err: |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 900 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 901 | return 1; |
| 902 | } |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 903 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 904 | |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 905 | #ifdef CONFIG_CMD_IMPORTENV |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 906 | /* |
| 907 | * env import [-d] [-t | -b | -c] addr [size] |
| 908 | * -d: delete existing environment before importing; |
| 909 | * otherwise overwrite / append to existion definitions |
| 910 | * -t: assume text format; either "size" must be given or the |
| 911 | * text data must be '\0' terminated |
| 912 | * -b: assume binary format ('\0' separated, "\0\0" terminated) |
| 913 | * -c: assume checksum protected environment format |
| 914 | * addr: memory address to read from |
| 915 | * size: length of input data; if missing, proper '\0' |
| 916 | * termination is mandatory |
| 917 | */ |
Igor Grinberg | de83256 | 2011-11-07 01:13:59 +0000 | [diff] [blame] | 918 | static int do_env_import(cmd_tbl_t *cmdtp, int flag, |
| 919 | int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 920 | { |
| 921 | char *cmd, *addr; |
| 922 | char sep = '\n'; |
| 923 | int chk = 0; |
| 924 | int fmt = 0; |
| 925 | int del = 0; |
| 926 | size_t size; |
| 927 | |
| 928 | cmd = *argv; |
| 929 | |
| 930 | while (--argc > 0 && **++argv == '-') { |
| 931 | char *arg = *argv; |
| 932 | while (*++arg) { |
| 933 | switch (*arg) { |
| 934 | case 'b': /* raw binary format */ |
| 935 | if (fmt++) |
| 936 | goto sep_err; |
| 937 | sep = '\0'; |
| 938 | break; |
| 939 | case 'c': /* external checksum format */ |
| 940 | if (fmt++) |
| 941 | goto sep_err; |
| 942 | sep = '\0'; |
| 943 | chk = 1; |
| 944 | break; |
| 945 | case 't': /* text format */ |
| 946 | if (fmt++) |
| 947 | goto sep_err; |
| 948 | sep = '\n'; |
| 949 | break; |
| 950 | case 'd': |
| 951 | del = 1; |
| 952 | break; |
| 953 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 954 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 959 | if (argc < 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 960 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 961 | |
| 962 | if (!fmt) |
| 963 | printf("## Warning: defaulting to text format\n"); |
| 964 | |
| 965 | addr = (char *)simple_strtoul(argv[0], NULL, 16); |
| 966 | |
| 967 | if (argc == 2) { |
| 968 | size = simple_strtoul(argv[1], NULL, 16); |
| 969 | } else { |
| 970 | char *s = addr; |
| 971 | |
| 972 | size = 0; |
| 973 | |
| 974 | while (size < MAX_ENV_SIZE) { |
| 975 | if ((*s == sep) && (*(s+1) == '\0')) |
| 976 | break; |
| 977 | ++s; |
| 978 | ++size; |
| 979 | } |
| 980 | if (size == MAX_ENV_SIZE) { |
| 981 | printf("## Warning: Input data exceeds %d bytes" |
| 982 | " - truncated\n", MAX_ENV_SIZE); |
| 983 | } |
Horst Kronstorfer | 295da64 | 2011-12-16 23:33:10 +0000 | [diff] [blame] | 984 | size += 2; |
Simon Glass | c6f4e12 | 2011-11-04 06:42:36 +0000 | [diff] [blame] | 985 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | if (chk) { |
| 989 | uint32_t crc; |
| 990 | env_t *ep = (env_t *)addr; |
| 991 | |
| 992 | size -= offsetof(env_t, data); |
| 993 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 994 | |
| 995 | if (crc32(0, ep->data, size) != crc) { |
| 996 | puts("## Error: bad CRC, import failed\n"); |
| 997 | return 1; |
| 998 | } |
| 999 | addr = (char *)ep->data; |
| 1000 | } |
| 1001 | |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 1002 | if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR, |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 1003 | 0, NULL) == 0) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1004 | error("Environment import failed: errno = %d\n", errno); |
| 1005 | return 1; |
| 1006 | } |
| 1007 | gd->flags |= GD_FLG_ENV_READY; |
| 1008 | |
| 1009 | return 0; |
| 1010 | |
| 1011 | sep_err: |
| 1012 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
| 1013 | cmd); |
| 1014 | return 1; |
| 1015 | } |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1016 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1017 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1018 | /* |
| 1019 | * New command line interface: "env" command with subcommands |
| 1020 | */ |
| 1021 | static cmd_tbl_t cmd_env_sub[] = { |
| 1022 | #if defined(CONFIG_CMD_ASKENV) |
| 1023 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), |
| 1024 | #endif |
| 1025 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), |
Joe Hershberger | 8335ce7 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1026 | U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1027 | #if defined(CONFIG_CMD_EDITENV) |
| 1028 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), |
| 1029 | #endif |
Joe Hershberger | 1b0d551 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1030 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1031 | U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), |
| 1032 | #endif |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1033 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1034 | U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), |
| 1035 | #endif |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1036 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1037 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1038 | #endif |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1039 | #if defined(CONFIG_CMD_GREPENV) |
| 1040 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), |
| 1041 | #endif |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1042 | #if defined(CONFIG_CMD_IMPORTENV) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1043 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
Mike Frysinger | f3ddf20 | 2010-12-26 23:09:45 -0500 | [diff] [blame] | 1044 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1045 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
| 1046 | #if defined(CONFIG_CMD_RUN) |
| 1047 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), |
| 1048 | #endif |
| 1049 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
| 1050 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), |
| 1051 | #endif |
| 1052 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), |
| 1053 | }; |
| 1054 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 1055 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | faa0d2e | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 1056 | void env_reloc(void) |
| 1057 | { |
| 1058 | fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
Macpaul Lin | ff2a9f3 | 2011-04-26 16:16:45 +0000 | [diff] [blame] | 1062 | static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1063 | { |
| 1064 | cmd_tbl_t *cp; |
| 1065 | |
Thomas Weber | 4d28265 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1066 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1067 | return CMD_RET_USAGE; |
Thomas Weber | 4d28265 | 2010-11-24 13:07:52 +0100 | [diff] [blame] | 1068 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1069 | /* drop initial "env" arg */ |
| 1070 | argc--; |
| 1071 | argv++; |
| 1072 | |
| 1073 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); |
| 1074 | |
| 1075 | if (cp) |
| 1076 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1077 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1078 | return CMD_RET_USAGE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1081 | #ifdef CONFIG_SYS_LONGHELP |
| 1082 | static char env_help_text[] = |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1083 | #if defined(CONFIG_CMD_ASKENV) |
| 1084 | "ask name [message] [size] - ask for environment variable\nenv " |
| 1085 | #endif |
Joe Hershberger | 1b0d551 | 2012-12-11 22:16:25 -0600 | [diff] [blame] | 1086 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
| 1087 | "callbacks - print callbacks and their associated variables\nenv " |
| 1088 | #endif |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 1089 | "default [-f] -a - [forcibly] reset default environment\n" |
| 1090 | "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" |
Joe Hershberger | 8335ce7 | 2012-12-11 22:16:36 -0600 | [diff] [blame] | 1091 | "env delete [-f] var [...] - [forcibly] delete variable(s)\n" |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1092 | #if defined(CONFIG_CMD_EDITENV) |
| 1093 | "env edit name - edit environment variable\n" |
| 1094 | #endif |
Benoît Thébaudeau | f712897 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1095 | #if defined(CONFIG_CMD_EXPORTENV) |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 1096 | "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" |
Benoît Thébaudeau | f712897 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1097 | #endif |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 1098 | #if defined(CONFIG_CMD_ENV_FLAGS) |
| 1099 | "env flags - print variables that have non-default flags\n" |
| 1100 | #endif |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1101 | #if defined(CONFIG_CMD_GREPENV) |
| 1102 | "env grep string [...] - search environment\n" |
| 1103 | #endif |
Benoît Thébaudeau | f712897 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1104 | #if defined(CONFIG_CMD_IMPORTENV) |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1105 | "env import [-d] [-t | -b | -c] addr [size] - import environment\n" |
Benoît Thébaudeau | f712897 | 2012-08-10 07:45:44 +0000 | [diff] [blame] | 1106 | #endif |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1107 | "env print [-a | name ...] - print environment\n" |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1108 | #if defined(CONFIG_CMD_RUN) |
| 1109 | "env run var [...] - run commands in an environment variable\n" |
| 1110 | #endif |
Horst Kronstorfer | c63e900 | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 1111 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1112 | "env save - save environment\n" |
Horst Kronstorfer | c63e900 | 2011-12-10 02:25:19 +0000 | [diff] [blame] | 1113 | #endif |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1114 | "env set [-f] name [arg ...]\n"; |
| 1115 | #endif |
| 1116 | |
| 1117 | U_BOOT_CMD( |
| 1118 | env, CONFIG_SYS_MAXARGS, 1, do_env, |
| 1119 | "environment handling commands", env_help_text |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1120 | ); |
| 1121 | |
| 1122 | /* |
| 1123 | * Old command line interface, kept for compatibility |
| 1124 | */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1125 | |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1126 | #if defined(CONFIG_CMD_EDITENV) |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1127 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1128 | editenv, 2, 0, do_env_edit, |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1129 | "edit environment variable", |
| 1130 | "name\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1131 | " - edit environment variable 'name'", |
| 1132 | var_complete |
Peter Tyser | 0deafa2 | 2009-10-25 15:12:56 -0500 | [diff] [blame] | 1133 | ); |
| 1134 | #endif |
| 1135 | |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1136 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1137 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1138 | "print environment variables", |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 1139 | "[-a]\n - print [all] values of all environment variables\n" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1140 | "printenv name ...\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1141 | " - print value of environment variable 'name'", |
| 1142 | var_complete |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1143 | ); |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 1144 | |
| 1145 | #ifdef CONFIG_CMD_GREPENV |
| 1146 | U_BOOT_CMD_COMPLETE( |
| 1147 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, |
| 1148 | "search environment variables", |
| 1149 | "string ...\n" |
| 1150 | " - list environment name=value pairs matching 'string'", |
| 1151 | var_complete |
| 1152 | ); |
| 1153 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1154 | |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1155 | U_BOOT_CMD_COMPLETE( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1156 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1157 | "set environment variables", |
Joe Hershberger | f64bb77 | 2012-12-11 22:16:35 -0600 | [diff] [blame] | 1158 | "[-f] name value ...\n" |
| 1159 | " - [forcibly] set environment variable 'name' to 'value ...'\n" |
| 1160 | "setenv [-f] name\n" |
| 1161 | " - [forcibly] delete environment variable 'name'", |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1162 | var_complete |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1163 | ); |
| 1164 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1165 | #if defined(CONFIG_CMD_ASKENV) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1166 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1167 | U_BOOT_CMD( |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 1168 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1169 | "get environment variables from stdin", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1170 | "name [message] [size]\n" |
| 1171 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 1172 | "askenv name\n" |
| 1173 | " - get environment variable 'name' from stdin\n" |
| 1174 | "askenv name size\n" |
| 1175 | " - get environment variable 'name' from stdin (max 'size' chars)\n" |
| 1176 | "askenv name [message] size\n" |
| 1177 | " - display 'message' string and get environment variable 'name'" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1178 | "from stdin (max 'size' chars)" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1179 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1180 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1181 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 1182 | #if defined(CONFIG_CMD_RUN) |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1183 | U_BOOT_CMD_COMPLETE( |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1184 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1185 | "run commands in an environment variable", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1186 | "var [...]\n" |
Mike Frysinger | b5bc05e | 2010-10-20 03:52:39 -0400 | [diff] [blame] | 1187 | " - run the commands in the environment variable(s) 'var'", |
| 1188 | var_complete |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1189 | ); |
Jon Loeliger | d704d91 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 1190 | #endif |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 1191 | #endif /* CONFIG_SPL_BUILD */ |