blob: c601b3ac272cbc9afa4e0079292f17d0e8213630 [file] [log] [blame]
wdenka68d3ed2002-10-11 08:38:32 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenka68d3ed2002-10-11 08:38:32 +00003 * 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 Phillipsa000b792011-04-05 07:15:14 +00007 *
8 * Copyright 2011 Freescale Semiconductor, Inc.
9 *
wdenka68d3ed2002-10-11 08:38:32 +000010 * 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 Denkea882ba2010-06-20 23:33:59 +020029/*
wdenka68d3ed2002-10-11 08:38:32 +000030 * Support for persistent environment data
31 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020032 * 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.
wdenka68d3ed2002-10-11 08:38:32 +000037 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020038 * 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.
wdenka68d3ed2002-10-11 08:38:32 +000041 */
42
43#include <common.h>
44#include <command.h>
45#include <environment.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020046#include <search.h>
47#include <errno.h>
Peter Tyser246c6922009-10-25 15:12:56 -050048#include <malloc.h>
wdenk2a3cb022002-11-05 21:01:48 +000049#include <watchdog.h>
wdenk281e00a2004-08-01 22:48:16 +000050#include <serial.h>
wdenka68d3ed2002-10-11 08:38:32 +000051#include <linux/stddef.h>
52#include <asm/byteorder.h>
Jon Loeligerc76fe472007-07-08 18:02:23 -050053#if defined(CONFIG_CMD_NET)
wdenka68d3ed2002-10-11 08:38:32 +000054#include <net.h>
55#endif
56
Wolfgang Denkd87080b2006-03-31 18:32:53 +020057DECLARE_GLOBAL_DATA_PTR;
58
Macpaul Linf3c615b2011-04-26 16:16:45 +000059#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
60 !defined(CONFIG_ENV_IS_IN_FLASH) && \
61 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000062 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin57210c72012-03-12 23:57:50 +000063 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000064 !defined(CONFIG_ENV_IS_IN_NAND) && \
65 !defined(CONFIG_ENV_IS_IN_NVRAM) && \
66 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
67 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
Liu Gang0a85a9e2012-03-08 00:33:20 +000068 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000069 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kim75eb82e2009-02-25 11:31:24 +090070# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Marek Vasutb37d41a2012-03-04 15:11:32 +000071SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000072#endif
73
74#define XMK_STR(x) #x
75#define MK_STR(x) XMK_STR(x)
76
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077/*
78 * Maximum expected input data size for import command
79 */
80#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000081
Mike Frysinger558605c2010-12-21 14:08:27 -050082ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
Simon Glass1aec2442011-10-21 18:51:38 +000083ulong save_addr; /* Default Save Address */
84ulong save_size; /* Default Save Size (in bytes) */
Mike Frysinger558605c2010-12-21 14:08:27 -050085
wdenka68d3ed2002-10-11 08:38:32 +000086/*
87 * Table with supported baudrates (defined in config_xyz.h)
88 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020089static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
wdenka68d3ed2002-10-11 08:38:32 +000090#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
91
Heiko Schocherda954272009-04-28 08:36:11 +020092/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020093 * This variable is incremented on each do_env_set(), so it can
Heiko Schocherda954272009-04-28 08:36:11 +020094 * be used via get_env_id() as an indication, if the environment
95 * has changed or not. So it is possible to reread an environment
96 * variable only if the environment was changed ... done so for
97 * example in NetInitLoop()
98 */
Heiko Schocher2f70c492009-02-10 09:38:52 +010099static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +0000100
Macpaul Linf3c615b2011-04-26 16:16:45 +0000101int get_env_id(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100102{
103 return env_id;
104}
wdenka68d3ed2002-10-11 08:38:32 +0000105
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400106/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200107 * Command interface: print one or all environment variables
108 *
109 * Returns 0 in case of error, or length of printed string
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400110 */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200111static int env_print(char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000112{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200113 char *res = NULL;
114 size_t len;
wdenka68d3ed2002-10-11 08:38:32 +0000115
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200116 if (name) { /* print a single name */
117 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000118
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200119 e.key = name;
120 e.data = NULL;
Mike Frysinger2eb15732010-12-08 06:26:04 -0500121 hsearch_r(e, FIND, &ep, &env_htab);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200122 if (ep == NULL)
123 return 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000124 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200125 return len;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400126 }
wdenka68d3ed2002-10-11 08:38:32 +0000127
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200128 /* print whole list */
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100129 len = hexport_r(&env_htab, '\n', &res, 0, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200130
131 if (len > 0) {
132 puts(res);
133 free(res);
134 return len;
135 }
136
137 /* should never happen */
138 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400139}
140
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200141int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400142{
143 int i;
144 int rcode = 0;
145
146 if (argc == 1) {
147 /* print all env vars */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200148 rcode = env_print(NULL);
149 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400150 return 1;
151 printf("\nEnvironment size: %d/%ld bytes\n",
152 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000153 return 0;
154 }
155
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400156 /* print selected env vars */
157 for (i = 1; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200158 int rc = env_print(argv[i]);
159 if (!rc) {
160 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400161 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000162 }
163 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400164
wdenka68d3ed2002-10-11 08:38:32 +0000165 return rcode;
166}
167
Kim Phillipsa000b792011-04-05 07:15:14 +0000168#ifdef CONFIG_CMD_GREPENV
Igor Grinbergd09b1782011-11-07 01:13:59 +0000169static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
170 int argc, char * const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000171{
172 ENTRY *match;
173 unsigned char matched[env_htab.size / 8];
174 int rcode = 1, arg = 1, idx;
175
176 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000177 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000178
179 memset(matched, 0, env_htab.size / 8);
180
181 while (arg <= argc) {
182 idx = 0;
Kumar Gala068f1582011-11-23 09:48:02 +0000183 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
Kim Phillipsa000b792011-04-05 07:15:14 +0000184 if (!(matched[idx / 8] & (1 << (idx & 7)))) {
185 puts(match->key);
186 puts("=");
187 puts(match->data);
188 puts("\n");
189 }
190 matched[idx / 8] |= 1 << (idx & 7);
191 rcode = 0;
192 }
193 arg++;
194 }
195
196 return rcode;
197}
198#endif
199
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200200/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000201 * Perform consistency checking before setting, replacing, or deleting an
202 * environment variable, then (if successful) apply the changes to internals so
203 * to make them effective. Code for this function was taken out of
204 * _do_env_set(), which now calls it instead.
205 * Returns 0 in case of success, 1 in case of failure.
206 * When (flag & H_FORCE) is set, do not print out any error message and force
207 * overwriting of write-once variables.
wdenka68d3ed2002-10-11 08:38:32 +0000208 */
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000209
210int env_check_apply(const char *name, const char *oldval,
211 const char *newval, int flag)
wdenka68d3ed2002-10-11 08:38:32 +0000212{
wdenka68d3ed2002-10-11 08:38:32 +0000213 int console = -1;
wdenka68d3ed2002-10-11 08:38:32 +0000214
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200215 /* Check for console redirection */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000216 if (strcmp(name, "stdin") == 0)
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200217 console = stdin;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000218 else if (strcmp(name, "stdout") == 0)
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200219 console = stdout;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000220 else if (strcmp(name, "stderr") == 0)
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200221 console = stderr;
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200222
223 if (console != -1) {
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000224 if ((newval == NULL) || (*newval == '\0')) {
225 /* We cannot delete stdin/stdout/stderr */
226 if ((flag & H_FORCE) == 0)
227 printf("Can't delete \"%s\"\n", name);
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200228 return 1;
229 }
230
231#ifdef CONFIG_CONSOLE_MUX
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000232 if (iomux_doenv(console, newval))
Gerlando Falautoc2ba2ff2012-08-24 00:11:36 +0000233 return 1;
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200234#else
235 /* Try assigning specified device */
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000236 if (console_assign(console, newval) < 0)
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200237 return 1;
238
239#ifdef CONFIG_SERIAL_MULTI
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000240 if (serial_assign(newval) < 0)
Alessandro Rubini9c5586a2009-10-08 14:29:14 +0200241 return 1;
242#endif
243#endif /* CONFIG_CONSOLE_MUX */
244 }
245
wdenka68d3ed2002-10-11 08:38:32 +0000246 /*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000247 * Some variables like "ethaddr" and "serial#" can be set only once and
248 * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
wdenka68d3ed2002-10-11 08:38:32 +0000249 */
wdenka68d3ed2002-10-11 08:38:32 +0000250#ifndef CONFIG_ENV_OVERWRITE
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000251 if (oldval != NULL && /* variable exists */
252 (flag & H_FORCE) == 0) { /* and we are not forced */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000253 if (strcmp(name, "serial#") == 0 ||
254 (strcmp(name, "ethaddr") == 0
wdenka68d3ed2002-10-11 08:38:32 +0000255#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000256 && strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0
wdenka68d3ed2002-10-11 08:38:32 +0000257#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000258 )) {
Macpaul Linf3c615b2011-04-26 16:16:45 +0000259 printf("Can't overwrite \"%s\"\n", name);
wdenka68d3ed2002-10-11 08:38:32 +0000260 return 1;
261 }
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000262 }
wdenka68d3ed2002-10-11 08:38:32 +0000263#endif
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000264 /*
265 * When we change baudrate, or we are doing an env default -a
266 * (which will erase all variables prior to calling this),
267 * we want the baudrate to actually change - for real.
268 */
269 if (oldval != NULL || /* variable exists */
270 (flag & H_NOCLEAR) == 0) { /* or env is clear */
wdenka68d3ed2002-10-11 08:38:32 +0000271 /*
272 * Switch to new baudrate if new baudrate is supported
273 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000274 if (strcmp(name, "baudrate") == 0) {
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000275 int baudrate = simple_strtoul(newval, NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000276 int i;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000277 for (i = 0; i < N_BAUDRATES; ++i) {
wdenka68d3ed2002-10-11 08:38:32 +0000278 if (baudrate == baudrate_table[i])
279 break;
280 }
281 if (i == N_BAUDRATES) {
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000282 if ((flag & H_FORCE) == 0)
283 printf("## Baudrate %d bps not "
284 "supported\n", baudrate);
wdenka68d3ed2002-10-11 08:38:32 +0000285 return 1;
286 }
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000287 if (gd->baudrate == baudrate) {
288 /* If unchanged, we just say it's OK */
289 return 0;
290 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000291 printf("## Switch baudrate to %d bps and"
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000292 "press ENTER ...\n", baudrate);
wdenka68d3ed2002-10-11 08:38:32 +0000293 udelay(50000);
294 gd->baudrate = baudrate;
Bartlomiej Siekac84bad02006-12-20 00:29:43 +0100295#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
wdenkd0fb80c2003-01-11 09:48:40 +0000296 gd->bd->bi_baudrate = baudrate;
297#endif
298
Macpaul Linf3c615b2011-04-26 16:16:45 +0000299 serial_setbrg();
wdenka68d3ed2002-10-11 08:38:32 +0000300 udelay(50000);
Igor Grinbergd09b1782011-11-07 01:13:59 +0000301 while (getc() != '\r')
302 ;
wdenka68d3ed2002-10-11 08:38:32 +0000303 }
wdenka68d3ed2002-10-11 08:38:32 +0000304 }
305
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000306 /*
307 * Some variables should be updated when the corresponding
308 * entry in the environment is changed
309 */
310 if (strcmp(name, "loadaddr") == 0) {
311 load_addr = simple_strtoul(newval, NULL, 16);
312 return 0;
313 }
314#if defined(CONFIG_CMD_NET)
315 else if (strcmp(name, "bootfile") == 0) {
316 copy_filename(BootFile, newval, sizeof(BootFile));
317 return 0;
318 }
319#endif
320 return 0;
321}
322
323/*
324 * Set a new environment variable,
325 * or replace or delete an existing one.
326*/
327int _do_env_set(int flag, int argc, char * const argv[])
328{
329 int i, len;
330 char *name, *value, *s;
331 ENTRY e, *ep;
332
333 name = argv[1];
334 value = argv[2];
335
336 if (strchr(name, '=')) {
337 printf("## Error: illegal character '='"
338 "in variable name \"%s\"\n", name);
339 return 1;
340 }
341
342 env_id++;
343 /*
344 * search if variable with this name already exists
345 */
346 e.key = name;
347 e.data = NULL;
348 hsearch_r(e, FIND, &ep, &env_htab);
349
350 /*
351 * Perform requested checks. Notice how since we are overwriting
352 * a single variable, we need to set H_NOCLEAR
353 */
354 if (env_check_apply(name, ep ? ep->data : NULL, value, H_NOCLEAR)) {
355 debug("check function did not approve, refusing\n");
356 return 1;
357 }
358
wdenka68d3ed2002-10-11 08:38:32 +0000359 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000360 if (argc < 3 || argv[2] == NULL) {
Mike Frysinger2eb15732010-12-08 06:26:04 -0500361 int rc = hdelete_r(name, &env_htab);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200362 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000363 }
364
365 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200366 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000367 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000368 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000369 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000370
371 value = malloc(len);
372 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200373 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000374 return 1;
375 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000376 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200377 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000378
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200379 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000380 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000381 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000382 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200383 if (s != value)
384 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000385
Igor Grinbergd09b1782011-11-07 01:13:59 +0000386 e.key = name;
387 e.data = value;
Mike Frysinger2eb15732010-12-08 06:26:04 -0500388 hsearch_r(e, ENTER, &ep, &env_htab);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200389 free(value);
390 if (!ep) {
391 printf("## Error inserting \"%s\" variable, errno=%d\n",
392 name, errno);
393 return 1;
394 }
wdenka68d3ed2002-10-11 08:38:32 +0000395
wdenka68d3ed2002-10-11 08:38:32 +0000396 return 0;
397}
398
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200399int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000400{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200401 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
402
Igor Grinbergd09b1782011-11-07 01:13:59 +0000403 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200404 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200405 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200406 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000407}
408
Simon Glassd67f10c2011-10-24 17:59:59 +0000409/**
410 * Set an environment variable to an integer value
411 *
412 * @param varname Environmet variable to set
413 * @param value Value to set it to
414 * @return 0 if ok, 1 on error
415 */
416int setenv_ulong(const char *varname, ulong value)
417{
418 /* TODO: this should be unsigned */
419 char *str = simple_itoa(value);
420
421 return setenv(varname, str);
422}
423
424/**
425 * Set an environment variable to an address in hex
426 *
427 * @param varname Environmet variable to set
428 * @param addr Value to set it to
429 * @return 0 if ok, 1 on error
430 */
431int setenv_addr(const char *varname, const void *addr)
432{
433 char str[17];
434
Simon Glass79afc882011-11-04 06:42:36 +0000435 sprintf(str, "%lx", (uintptr_t)addr);
Simon Glassd67f10c2011-10-24 17:59:59 +0000436 return setenv(varname, str);
437}
438
Macpaul Linf3c615b2011-04-26 16:16:45 +0000439int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000440{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200441 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000442 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000443
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200444 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000445}
446
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200447/*
wdenka68d3ed2002-10-11 08:38:32 +0000448 * Prompt for environment variable
449 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500450#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000451int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000452{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200453 char message[CONFIG_SYS_CBSIZE];
454 int size = CONFIG_SYS_CBSIZE - 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200455 int i, len, pos;
wdenka68d3ed2002-10-11 08:38:32 +0000456 char *local_args[4];
457
458 local_args[0] = argv[0];
459 local_args[1] = argv[1];
460 local_args[2] = NULL;
461 local_args[3] = NULL;
462
wdenka68d3ed2002-10-11 08:38:32 +0000463 /* Check the syntax */
464 switch (argc) {
465 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000466 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000467
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200468 case 2: /* env_ask envname */
469 sprintf(message, "Please enter '%s':", argv[1]);
wdenka68d3ed2002-10-11 08:38:32 +0000470 break;
471
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200472 case 3: /* env_ask envname size */
473 sprintf(message, "Please enter '%s':", argv[1]);
474 size = simple_strtoul(argv[2], NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000475 break;
476
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200477 default: /* env_ask envname message1 ... messagen size */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000478 for (i = 2, pos = 0; i < argc - 1; i++) {
479 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000480 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000481
Igor Grinbergd09b1782011-11-07 01:13:59 +0000482 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000483 pos += strlen(argv[i]);
484 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000485
wdenka68d3ed2002-10-11 08:38:32 +0000486 message[pos] = '\0';
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200487 size = simple_strtoul(argv[argc - 1], NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000488 break;
489 }
490
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200491 if (size >= CONFIG_SYS_CBSIZE)
492 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000493
494 if (size <= 0)
495 return 1;
496
497 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200498 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000499
500 if (size < len)
501 console_buffer[size] = '\0';
502
503 len = 2;
504 if (console_buffer[0] != '\0') {
505 local_args[2] = console_buffer;
506 len = 3;
507 }
508
509 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200510 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000511}
Jon Loeliger90253172007-07-10 11:02:44 -0500512#endif
wdenka68d3ed2002-10-11 08:38:32 +0000513
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200514/*
Peter Tyser246c6922009-10-25 15:12:56 -0500515 * Interactively edit an environment variable
516 */
517#if defined(CONFIG_CMD_EDITENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200518int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500519{
520 char buffer[CONFIG_SYS_CBSIZE];
521 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500522
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200523 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000524 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500525
526 /* Set read buffer to initial value or empty sting */
527 init_val = getenv(argv[1]);
528 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200529 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500530 else
531 buffer[0] = '\0';
532
Heiko Schocher9c348312012-01-16 21:13:05 +0000533 readline_into_buffer("edit: ", buffer, 0);
Peter Tyser246c6922009-10-25 15:12:56 -0500534
535 return setenv(argv[1], buffer);
536}
537#endif /* CONFIG_CMD_EDITENV */
538
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200539/*
wdenka68d3ed2002-10-11 08:38:32 +0000540 * Look up variable from environment,
541 * return address of storage for that variable,
542 * or NULL if not found
543 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200544char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000545{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000546 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200547 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000548
Wolfgang Denk91a76752010-07-24 20:22:02 +0200549 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000550
Igor Grinbergd09b1782011-11-07 01:13:59 +0000551 e.key = name;
552 e.data = NULL;
Mike Frysinger2eb15732010-12-08 06:26:04 -0500553 hsearch_r(e, FIND, &ep, &env_htab);
wdenka68d3ed2002-10-11 08:38:32 +0000554
Macpaul Linf3c615b2011-04-26 16:16:45 +0000555 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000556 }
557
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200558 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200559 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
560 return (char *)(gd->env_buf);
561
562 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000563}
564
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200565/*
566 * Look up variable from environment for restricted C runtime env.
567 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200568int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000569{
570 int i, nxt;
571
Igor Grinbergd09b1782011-11-07 01:13:59 +0000572 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000573 int val, n;
574
Macpaul Linf3c615b2011-04-26 16:16:45 +0000575 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
576 if (nxt >= CONFIG_ENV_SIZE)
577 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000578 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000579
580 val = envmatch((uchar *)name, i);
581 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000582 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200583
wdenka68d3ed2002-10-11 08:38:32 +0000584 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000585 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000586 *buf = env_get_char(val++);
587 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200588 return n;
589 }
590
591 if (n)
592 *--buf = '\0';
593
Wolfgang Denka02a8842011-05-04 10:29:49 +0000594 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
595 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200596
597 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000598 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000599
Macpaul Linf3c615b2011-04-26 16:16:45 +0000600 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000601}
602
Simon Glass4a9b4132011-10-14 13:25:18 +0000603/**
604 * Decode the integer value of an environment variable and return it.
605 *
606 * @param name Name of environemnt variable
607 * @param base Number base to use (normally 10, or 16 for hex)
608 * @param default_val Default value to return if the variable is not
609 * found
610 * @return the decoded value, or default_val if not found
611 */
612ulong getenv_ulong(const char *name, int base, ulong default_val)
613{
614 /*
615 * We can use getenv() here, even before relocation, since the
616 * environment variable value is an integer and thus short.
617 */
618 const char *str = getenv(name);
619
620 return str ? simple_strtoul(str, NULL, base) : default_val;
621}
622
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500623#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000624int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000625{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000626 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000627
Macpaul Linf3c615b2011-04-26 16:16:45 +0000628 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000629}
wdenk8bde7f72003-06-27 21:31:46 +0000630
Mike Frysingerba69dc22008-12-30 02:59:25 -0500631U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200632 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600633 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200634 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500635);
wdenka68d3ed2002-10-11 08:38:32 +0000636#endif
637
638
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200639/*
wdenka68d3ed2002-10-11 08:38:32 +0000640 * Match a name / name=value pair
641 *
642 * s1 is either a simple 'name', or a 'name=value' pair.
643 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000644 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000645 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000646int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000647{
wdenka68d3ed2002-10-11 08:38:32 +0000648 while (*s1 == env_get_char(i2++))
649 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000650 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000651
wdenka68d3ed2002-10-11 08:38:32 +0000652 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000653 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000654
Macpaul Linf3c615b2011-04-26 16:16:45 +0000655 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000656}
wdenk8bde7f72003-06-27 21:31:46 +0000657
Igor Grinbergd09b1782011-11-07 01:13:59 +0000658static int do_env_default(cmd_tbl_t *cmdtp, int flag,
659 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200660{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000661 if (argc != 2 || strcmp(argv[1], "-f") != 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000662 return CMD_RET_USAGE;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000663
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200664 set_default_env("## Resetting to default environment\n");
665 return 0;
666}
wdenk8bde7f72003-06-27 21:31:46 +0000667
Igor Grinbergd09b1782011-11-07 01:13:59 +0000668static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
669 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200670{
671 printf("Not implemented yet\n");
672 return 0;
673}
674
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500675#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200676/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100677 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200678 * -t: export as text format; if size is given, data will be
679 * padded with '\0' bytes; if not, one terminating '\0'
680 * will be added (which is included in the "filesize"
681 * setting so you can for exmple copy this to flash and
682 * keep the termination).
683 * -b: export as binary format (name=value pairs separated by
684 * '\0', list end marked by double "\0\0")
685 * -c: export as checksum protected environment format as
686 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100687 * -s size:
688 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200689 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100690 * var... List of variable names that get included into the
691 * export. Without arguments, the whole environment gets
692 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200693 *
694 * With "-c" and size is NOT given, then the export command will
695 * format the data as currently used for the persistent storage,
696 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
697 * prepend a valid CRC32 checksum and, in case of resundant
698 * environment, a "current" redundancy flag. If size is given, this
699 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
700 * checksum and redundancy flag will be inserted.
701 *
702 * With "-b" and "-t", always only the real data (including a
703 * terminating '\0' byte) will be written; here the optional size
704 * argument will be used to make sure not to overflow the user
705 * provided buffer; the command will abort if the size is not
706 * sufficient. Any remainign space will be '\0' padded.
707 *
708 * On successful return, the variable "filesize" will be set.
709 * Note that filesize includes the trailing/terminating '\0' byte(s).
710 *
711 * Usage szenario: create a text snapshot/backup of the current settings:
712 *
713 * => env export -t 100000
714 * => era ${backup_addr} +${filesize}
715 * => cp.b 100000 ${backup_addr} ${filesize}
716 *
717 * Re-import this snapshot, deleting all other settings:
718 *
719 * => env import -d -t ${backup_addr}
720 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000721static int do_env_export(cmd_tbl_t *cmdtp, int flag,
722 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200723{
724 char buf[32];
725 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100726 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200727 ssize_t len;
728 env_t *envp;
729 char sep = '\n';
730 int chk = 0;
731 int fmt = 0;
732
733 cmd = *argv;
734
735 while (--argc > 0 && **++argv == '-') {
736 char *arg = *argv;
737 while (*++arg) {
738 switch (*arg) {
739 case 'b': /* raw binary format */
740 if (fmt++)
741 goto sep_err;
742 sep = '\0';
743 break;
744 case 'c': /* external checksum format */
745 if (fmt++)
746 goto sep_err;
747 sep = '\0';
748 chk = 1;
749 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100750 case 's': /* size given */
751 if (--argc <= 0)
752 return cmd_usage(cmdtp);
753 size = simple_strtoul(*++argv, NULL, 16);
754 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200755 case 't': /* text format */
756 if (fmt++)
757 goto sep_err;
758 sep = '\n';
759 break;
760 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000761 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200762 }
763 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100764NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200765 }
766
Macpaul Linf3c615b2011-04-26 16:16:45 +0000767 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000768 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200769
770 addr = (char *)simple_strtoul(argv[0], NULL, 16);
771
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100772 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200773 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100774
775 argc--;
776 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200777
778 if (sep) { /* export as text file */
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100779 len = hexport_r(&env_htab, sep, &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200780 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000781 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200782 return 1;
783 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100784 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200785 setenv("filesize", buf);
786
787 return 0;
788 }
789
790 envp = (env_t *)addr;
791
792 if (chk) /* export as checksum protected block */
793 res = (char *)envp->data;
794 else /* export as raw binary data */
795 res = addr;
796
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100797 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200798 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000799 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200800 return 1;
801 }
802
803 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000804 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200805#ifdef CONFIG_ENV_ADDR_REDUND
806 envp->flags = ACTIVE_FLAG;
807#endif
808 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000809 sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200810 setenv("filesize", buf);
811
812 return 0;
813
814sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000815 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200816 return 1;
817}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500818#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200819
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500820#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200821/*
822 * env import [-d] [-t | -b | -c] addr [size]
823 * -d: delete existing environment before importing;
824 * otherwise overwrite / append to existion definitions
825 * -t: assume text format; either "size" must be given or the
826 * text data must be '\0' terminated
827 * -b: assume binary format ('\0' separated, "\0\0" terminated)
828 * -c: assume checksum protected environment format
829 * addr: memory address to read from
830 * size: length of input data; if missing, proper '\0'
831 * termination is mandatory
832 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000833static int do_env_import(cmd_tbl_t *cmdtp, int flag,
834 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200835{
836 char *cmd, *addr;
837 char sep = '\n';
838 int chk = 0;
839 int fmt = 0;
840 int del = 0;
841 size_t size;
842
843 cmd = *argv;
844
845 while (--argc > 0 && **++argv == '-') {
846 char *arg = *argv;
847 while (*++arg) {
848 switch (*arg) {
849 case 'b': /* raw binary format */
850 if (fmt++)
851 goto sep_err;
852 sep = '\0';
853 break;
854 case 'c': /* external checksum format */
855 if (fmt++)
856 goto sep_err;
857 sep = '\0';
858 chk = 1;
859 break;
860 case 't': /* text format */
861 if (fmt++)
862 goto sep_err;
863 sep = '\n';
864 break;
865 case 'd':
866 del = 1;
867 break;
868 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000869 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200870 }
871 }
872 }
873
Macpaul Linf3c615b2011-04-26 16:16:45 +0000874 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000875 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200876
877 if (!fmt)
878 printf("## Warning: defaulting to text format\n");
879
880 addr = (char *)simple_strtoul(argv[0], NULL, 16);
881
882 if (argc == 2) {
883 size = simple_strtoul(argv[1], NULL, 16);
884 } else {
885 char *s = addr;
886
887 size = 0;
888
889 while (size < MAX_ENV_SIZE) {
890 if ((*s == sep) && (*(s+1) == '\0'))
891 break;
892 ++s;
893 ++size;
894 }
895 if (size == MAX_ENV_SIZE) {
896 printf("## Warning: Input data exceeds %d bytes"
897 " - truncated\n", MAX_ENV_SIZE);
898 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000899 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +0000900 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200901 }
902
903 if (chk) {
904 uint32_t crc;
905 env_t *ep = (env_t *)addr;
906
907 size -= offsetof(env_t, data);
908 memcpy(&crc, &ep->crc, sizeof(crc));
909
910 if (crc32(0, ep->data, size) != crc) {
911 puts("## Error: bad CRC, import failed\n");
912 return 1;
913 }
914 addr = (char *)ep->data;
915 }
916
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000917 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
918 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200919 error("Environment import failed: errno = %d\n", errno);
920 return 1;
921 }
922 gd->flags |= GD_FLG_ENV_READY;
923
924 return 0;
925
926sep_err:
927 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
928 cmd);
929 return 1;
930}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500931#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200932
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200933/*
934 * New command line interface: "env" command with subcommands
935 */
936static cmd_tbl_t cmd_env_sub[] = {
937#if defined(CONFIG_CMD_ASKENV)
938 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
939#endif
940 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
941 U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
942#if defined(CONFIG_CMD_EDITENV)
943 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
944#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500945#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200946 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500947#endif
Kim Phillipsa000b792011-04-05 07:15:14 +0000948#if defined(CONFIG_CMD_GREPENV)
949 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
950#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500951#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200952 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500953#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200954 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
955#if defined(CONFIG_CMD_RUN)
956 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
957#endif
958#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
959 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
960#endif
961 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
962};
963
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200964#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +0200965void env_reloc(void)
966{
967 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
968}
969#endif
970
Macpaul Linf3c615b2011-04-26 16:16:45 +0000971static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200972{
973 cmd_tbl_t *cp;
974
Thomas Weber5904da02010-11-24 13:07:52 +0100975 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000976 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +0100977
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200978 /* drop initial "env" arg */
979 argc--;
980 argv++;
981
982 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
983
984 if (cp)
985 return cp->cmd(cmdtp, flag, argc, argv);
986
Simon Glass4c12eeb2011-12-10 08:44:01 +0000987 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200988}
989
990U_BOOT_CMD(
991 env, CONFIG_SYS_MAXARGS, 1, do_env,
992 "environment handling commands",
993#if defined(CONFIG_CMD_ASKENV)
994 "ask name [message] [size] - ask for environment variable\nenv "
995#endif
996 "default -f - reset default environment\n"
997#if defined(CONFIG_CMD_EDITENV)
998 "env edit name - edit environment variable\n"
999#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001000#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001001 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001002#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001003#if defined(CONFIG_CMD_GREPENV)
1004 "env grep string [...] - search environment\n"
1005#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001006#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +00001007 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001008#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001009 "env print [name ...] - print environment\n"
1010#if defined(CONFIG_CMD_RUN)
1011 "env run var [...] - run commands in an environment variable\n"
1012#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001013#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001014 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001015#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001016 "env set [-f] name [arg ...]\n"
1017);
1018
1019/*
1020 * Old command line interface, kept for compatibility
1021 */
wdenk8bde7f72003-06-27 21:31:46 +00001022
Peter Tyser246c6922009-10-25 15:12:56 -05001023#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001024U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001025 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001026 "edit environment variable",
1027 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001028 " - edit environment variable 'name'",
1029 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001030);
1031#endif
1032
Mike Frysinger722b0612010-10-20 03:52:39 -04001033U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001034 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001035 "print environment variables",
wdenk8bde7f72003-06-27 21:31:46 +00001036 "\n - print values of all environment variables\n"
1037 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001038 " - print value of environment variable 'name'",
1039 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001040);
1041
Kim Phillipsa000b792011-04-05 07:15:14 +00001042#ifdef CONFIG_CMD_GREPENV
1043U_BOOT_CMD_COMPLETE(
1044 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1045 "search environment variables",
1046 "string ...\n"
1047 " - list environment name=value pairs matching 'string'",
1048 var_complete
1049);
1050#endif
1051
Mike Frysinger722b0612010-10-20 03:52:39 -04001052U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001053 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001054 "set environment variables",
wdenk8bde7f72003-06-27 21:31:46 +00001055 "name value ...\n"
1056 " - set environment variable 'name' to 'value ...'\n"
1057 "setenv name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001058 " - delete environment variable 'name'",
1059 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001060);
1061
Jon Loeligerc76fe472007-07-08 18:02:23 -05001062#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001063
wdenk0d498392003-07-01 21:06:45 +00001064U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001065 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001066 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001067 "name [message] [size]\n"
1068 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1069 "askenv name\n"
1070 " - get environment variable 'name' from stdin\n"
1071 "askenv name size\n"
1072 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1073 "askenv name [message] size\n"
1074 " - display 'message' string and get environment variable 'name'"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001075 "from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001076);
Jon Loeliger90253172007-07-10 11:02:44 -05001077#endif
wdenk8bde7f72003-06-27 21:31:46 +00001078
Jon Loeligerc76fe472007-07-08 18:02:23 -05001079#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001080U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001081 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001082 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001083 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001084 " - run the commands in the environment variable(s) 'var'",
1085 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001086);
Jon Loeliger90253172007-07-10 11:02:44 -05001087#endif