blob: fab8694621efd71f84d985c00e70f8df0442d7be [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>
wdenka68d3ed2002-10-11 08:38:32 +000050#include <linux/stddef.h>
51#include <asm/byteorder.h>
wdenka68d3ed2002-10-11 08:38:32 +000052
Wolfgang Denkd87080b2006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54
Macpaul Linf3c615b2011-04-26 16:16:45 +000055#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
56 !defined(CONFIG_ENV_IS_IN_FLASH) && \
57 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000058 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin57210c72012-03-12 23:57:50 +000059 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000060 !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 Gang0a85a9e2012-03-08 00:33:20 +000064 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000065 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kim75eb82e2009-02-25 11:31:24 +090066# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Marek Vasutb37d41a2012-03-04 15:11:32 +000067SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000068#endif
69
Wolfgang Denkea882ba2010-06-20 23:33:59 +020070/*
71 * Maximum expected input data size for import command
72 */
73#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000074
wdenka68d3ed2002-10-11 08:38:32 +000075/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076 * This variable is incremented on each do_env_set(), so it can
Heiko Schocherda954272009-04-28 08:36:11 +020077 * 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 Schocher2f70c492009-02-10 09:38:52 +010082static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000083
Macpaul Linf3c615b2011-04-26 16:16:45 +000084int get_env_id(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +010085{
86 return env_id;
87}
wdenka68d3ed2002-10-11 08:38:32 +000088
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000089#ifndef CONFIG_SPL_BUILD
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040090/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020091 * Command interface: print one or all environment variables
92 *
93 * Returns 0 in case of error, or length of printed string
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040094 */
Joe Hershbergerbe112352012-12-11 22:16:23 -060095static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000096{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020097 char *res = NULL;
98 size_t len;
wdenka68d3ed2002-10-11 08:38:32 +000099
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200100 if (name) { /* print a single name */
101 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000102
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200103 e.key = name;
104 e.data = NULL;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600105 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200106 if (ep == NULL)
107 return 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000108 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200109 return len;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400110 }
wdenka68d3ed2002-10-11 08:38:32 +0000111
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200112 /* print whole list */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200114
115 if (len > 0) {
116 puts(res);
117 free(res);
118 return len;
119 }
120
121 /* should never happen */
122 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400123}
124
Kim Phillips088f1b12012-10-29 13:34:31 +0000125static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
126 char * const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400127{
128 int i;
129 int rcode = 0;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600130 int env_flag = H_HIDE_DOT;
131
132 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
133 argc--;
134 argv++;
135 env_flag &= ~H_HIDE_DOT;
136 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400137
138 if (argc == 1) {
139 /* print all env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600140 rcode = env_print(NULL, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200141 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400142 return 1;
143 printf("\nEnvironment size: %d/%ld bytes\n",
144 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000145 return 0;
146 }
147
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400148 /* print selected env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600149 env_flag &= ~H_HIDE_DOT;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400150 for (i = 1; i < argc; ++i) {
Joe Hershbergerbe112352012-12-11 22:16:23 -0600151 int rc = env_print(argv[i], env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200152 if (!rc) {
153 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400154 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000155 }
156 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400157
wdenka68d3ed2002-10-11 08:38:32 +0000158 return rcode;
159}
160
Kim Phillipsa000b792011-04-05 07:15:14 +0000161#ifdef CONFIG_CMD_GREPENV
Igor Grinbergd09b1782011-11-07 01:13:59 +0000162static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
163 int argc, char * const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000164{
165 ENTRY *match;
166 unsigned char matched[env_htab.size / 8];
167 int rcode = 1, arg = 1, idx;
168
169 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000170 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000171
172 memset(matched, 0, env_htab.size / 8);
173
174 while (arg <= argc) {
175 idx = 0;
Kumar Gala068f1582011-11-23 09:48:02 +0000176 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
Kim Phillipsa000b792011-04-05 07:15:14 +0000177 if (!(matched[idx / 8] & (1 << (idx & 7)))) {
178 puts(match->key);
179 puts("=");
180 puts(match->data);
181 puts("\n");
182 }
183 matched[idx / 8] |= 1 << (idx & 7);
184 rcode = 0;
185 }
186 arg++;
187 }
188
189 return rcode;
190}
191#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000192#endif /* CONFIG_SPL_BUILD */
Kim Phillipsa000b792011-04-05 07:15:14 +0000193
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200194/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000195 * Set a new environment variable,
196 * or replace or delete an existing one.
Joe Hershberger25980902012-12-11 22:16:31 -0600197 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000198static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000199{
200 int i, len;
201 char *name, *value, *s;
202 ENTRY e, *ep;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600203 int env_flag = H_INTERACTIVE;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000204
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600205 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 Falautoc3f65252012-08-24 00:11:37 +0000221 name = argv[1];
222 value = argv[2];
223
224 if (strchr(name, '=')) {
225 printf("## Error: illegal character '='"
226 "in variable name \"%s\"\n", name);
227 return 1;
228 }
229
230 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000231
wdenka68d3ed2002-10-11 08:38:32 +0000232 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000233 if (argc < 3 || argv[2] == NULL) {
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600234 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200235 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000236 }
237
238 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200239 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000240 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000241 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000242 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000243
244 value = malloc(len);
245 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200246 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000247 return 1;
248 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000249 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200250 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000251
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000253 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000254 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000255 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256 if (s != value)
257 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000258
Igor Grinbergd09b1782011-11-07 01:13:59 +0000259 e.key = name;
260 e.data = value;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600261 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200262 free(value);
263 if (!ep) {
264 printf("## Error inserting \"%s\" variable, errno=%d\n",
265 name, errno);
266 return 1;
267 }
wdenka68d3ed2002-10-11 08:38:32 +0000268
wdenka68d3ed2002-10-11 08:38:32 +0000269 return 0;
270}
271
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200272int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000273{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200274 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
275
Joe Hershbergera7eb1d62013-04-08 10:32:50 +0000276 /* before import into hashtable */
277 if (!(gd->flags & GD_FLG_ENV_READY))
278 return 1;
279
Igor Grinbergd09b1782011-11-07 01:13:59 +0000280 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200281 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200282 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200283 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000284}
285
Simon Glassd67f10c2011-10-24 17:59:59 +0000286/**
287 * Set an environment variable to an integer value
288 *
289 * @param varname Environmet variable to set
290 * @param value Value to set it to
291 * @return 0 if ok, 1 on error
292 */
293int setenv_ulong(const char *varname, ulong value)
294{
295 /* TODO: this should be unsigned */
296 char *str = simple_itoa(value);
297
298 return setenv(varname, str);
299}
300
301/**
Simon Glassbfc59962013-02-24 17:33:21 +0000302 * Set an environment variable to an value in hex
Simon Glassd67f10c2011-10-24 17:59:59 +0000303 *
304 * @param varname Environmet variable to set
Simon Glassbfc59962013-02-24 17:33:21 +0000305 * @param value Value to set it to
Simon Glassd67f10c2011-10-24 17:59:59 +0000306 * @return 0 if ok, 1 on error
307 */
Simon Glassbfc59962013-02-24 17:33:21 +0000308int setenv_hex(const char *varname, ulong value)
Simon Glassd67f10c2011-10-24 17:59:59 +0000309{
310 char str[17];
311
Simon Glassbfc59962013-02-24 17:33:21 +0000312 sprintf(str, "%lx", value);
Simon Glassd67f10c2011-10-24 17:59:59 +0000313 return setenv(varname, str);
314}
315
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000316#ifndef CONFIG_SPL_BUILD
Kim Phillips088f1b12012-10-29 13:34:31 +0000317static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000318{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200319 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000320 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000321
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200322 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000323}
324
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200325/*
wdenka68d3ed2002-10-11 08:38:32 +0000326 * Prompt for environment variable
327 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500328#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000329int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000330{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200331 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000332 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000333 char *local_args[4];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000334 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000335
336 local_args[0] = argv[0];
337 local_args[1] = argv[1];
338 local_args[2] = NULL;
339 local_args[3] = NULL;
340
Wolfgang Denk7d855912013-02-20 04:53:16 +0000341 /*
342 * Check the syntax:
343 *
344 * env_ask envname [message1 ...] [size]
345 */
346 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000347 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000348
Wolfgang Denk7d855912013-02-20 04:53:16 +0000349 /*
350 * We test the last argument if it can be converted
351 * into a decimal number. If yes, we assume it's
352 * the size. Otherwise we echo it as part of the
353 * message.
354 */
355 i = simple_strtoul(argv[argc - 1], &endptr, 10);
356 if (*endptr != '\0') { /* no size */
357 size = CONFIG_SYS_CBSIZE - 1;
358 } else { /* size given */
359 size = i;
360 --argc;
361 }
wdenka68d3ed2002-10-11 08:38:32 +0000362
Wolfgang Denk7d855912013-02-20 04:53:16 +0000363 if (argc <= 2) {
364 sprintf(message, "Please enter '%s': ", argv[1]);
365 } else {
366 /* env_ask envname message1 ... messagen [size] */
367 for (i = 2, pos = 0; i < argc; i++) {
Macpaul Linf3c615b2011-04-26 16:16:45 +0000368 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000369 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000370
Igor Grinbergd09b1782011-11-07 01:13:59 +0000371 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000372 pos += strlen(argv[i]);
373 }
Wolfgang Denk7d855912013-02-20 04:53:16 +0000374 message[pos++] = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000375 message[pos] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000376 }
377
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200378 if (size >= CONFIG_SYS_CBSIZE)
379 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000380
381 if (size <= 0)
382 return 1;
383
384 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200385 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000386
387 if (size < len)
388 console_buffer[size] = '\0';
389
390 len = 2;
391 if (console_buffer[0] != '\0') {
392 local_args[2] = console_buffer;
393 len = 3;
394 }
395
396 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200397 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000398}
Jon Loeliger90253172007-07-10 11:02:44 -0500399#endif
wdenka68d3ed2002-10-11 08:38:32 +0000400
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600401#if defined(CONFIG_CMD_ENV_CALLBACK)
402static int print_static_binding(const char *var_name, const char *callback_name)
403{
404 printf("\t%-20s %-20s\n", var_name, callback_name);
405
406 return 0;
407}
408
409static int print_active_callback(ENTRY *entry)
410{
411 struct env_clbk_tbl *clbkp;
412 int i;
413 int num_callbacks;
414
415 if (entry->callback == NULL)
416 return 0;
417
418 /* look up the callback in the linker-list */
419 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
420 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
421 i < num_callbacks;
422 i++, clbkp++) {
423#if defined(CONFIG_NEEDS_MANUAL_RELOC)
424 if (entry->callback == clbkp->callback + gd->reloc_off)
425#else
426 if (entry->callback == clbkp->callback)
427#endif
428 break;
429 }
430
431 if (i == num_callbacks)
432 /* this should probably never happen, but just in case... */
433 printf("\t%-20s %p\n", entry->key, entry->callback);
434 else
435 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
436
437 return 0;
438}
439
440/*
441 * Print the callbacks available and what they are bound to
442 */
443int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
444{
445 struct env_clbk_tbl *clbkp;
446 int i;
447 int num_callbacks;
448
449 /* Print the available callbacks */
450 puts("Available callbacks:\n");
451 puts("\tCallback Name\n");
452 puts("\t-------------\n");
453 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
454 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
455 i < num_callbacks;
456 i++, clbkp++)
457 printf("\t%s\n", clbkp->name);
458 puts("\n");
459
460 /* Print the static bindings that may exist */
461 puts("Static callback bindings:\n");
462 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
463 printf("\t%-20s %-20s\n", "-------------", "-------------");
464 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
465 puts("\n");
466
467 /* walk through each variable and print the callback if it has one */
468 puts("Active callback bindings:\n");
469 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
470 printf("\t%-20s %-20s\n", "-------------", "-------------");
471 hwalk_r(&env_htab, print_active_callback);
472 return 0;
473}
474#endif
475
Joe Hershbergerfffad712012-12-11 22:16:33 -0600476#if defined(CONFIG_CMD_ENV_FLAGS)
477static int print_static_flags(const char *var_name, const char *flags)
478{
479 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600480 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600481
Joe Hershberger267541f2012-12-11 22:16:34 -0600482 printf("\t%-20s %-20s %-20s\n", var_name,
483 env_flags_get_vartype_name(type),
484 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600485
486 return 0;
487}
488
489static int print_active_flags(ENTRY *entry)
490{
491 enum env_flags_vartype type;
Joe Hershberger267541f2012-12-11 22:16:34 -0600492 enum env_flags_varaccess access;
Joe Hershbergerfffad712012-12-11 22:16:33 -0600493
494 if (entry->flags == 0)
495 return 0;
496
497 type = (enum env_flags_vartype)
498 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger267541f2012-12-11 22:16:34 -0600499 access = env_flags_parse_varaccess_from_binflags(entry->flags);
500 printf("\t%-20s %-20s %-20s\n", entry->key,
501 env_flags_get_vartype_name(type),
502 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600503
504 return 0;
505}
506
507/*
508 * Print the flags available and what variables have flags
509 */
510int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
511{
512 /* Print the available variable types */
513 printf("Available variable type flags (position %d):\n",
514 ENV_FLAGS_VARTYPE_LOC);
515 puts("\tFlag\tVariable Type Name\n");
516 puts("\t----\t------------------\n");
517 env_flags_print_vartypes();
518 puts("\n");
519
Joe Hershberger267541f2012-12-11 22:16:34 -0600520 /* Print the available variable access types */
521 printf("Available variable access flags (position %d):\n",
522 ENV_FLAGS_VARACCESS_LOC);
523 puts("\tFlag\tVariable Access Name\n");
524 puts("\t----\t--------------------\n");
525 env_flags_print_varaccess();
526 puts("\n");
527
Joe Hershbergerfffad712012-12-11 22:16:33 -0600528 /* Print the static flags that may exist */
529 puts("Static flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600530 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
531 "Variable Access");
532 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
533 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600534 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
535 puts("\n");
536
537 /* walk through each variable and print the flags if non-default */
538 puts("Active flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600539 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
540 "Variable Access");
541 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
542 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600543 hwalk_r(&env_htab, print_active_flags);
544 return 0;
545}
546#endif
547
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200548/*
Peter Tyser246c6922009-10-25 15:12:56 -0500549 * Interactively edit an environment variable
550 */
551#if defined(CONFIG_CMD_EDITENV)
Kim Phillips088f1b12012-10-29 13:34:31 +0000552static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
553 char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500554{
555 char buffer[CONFIG_SYS_CBSIZE];
556 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500557
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200558 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000559 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500560
561 /* Set read buffer to initial value or empty sting */
562 init_val = getenv(argv[1]);
563 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200564 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500565 else
566 buffer[0] = '\0';
567
Joe Hershberger18a3cce2013-02-08 10:12:34 +0000568 if (readline_into_buffer("edit: ", buffer, 0) < 0)
569 return 1;
Peter Tyser246c6922009-10-25 15:12:56 -0500570
571 return setenv(argv[1], buffer);
572}
573#endif /* CONFIG_CMD_EDITENV */
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000574#endif /* CONFIG_SPL_BUILD */
Peter Tyser246c6922009-10-25 15:12:56 -0500575
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200576/*
wdenka68d3ed2002-10-11 08:38:32 +0000577 * Look up variable from environment,
578 * return address of storage for that variable,
579 * or NULL if not found
580 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200581char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000582{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000583 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200584 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000585
Wolfgang Denk91a76752010-07-24 20:22:02 +0200586 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000587
Igor Grinbergd09b1782011-11-07 01:13:59 +0000588 e.key = name;
589 e.data = NULL;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600590 hsearch_r(e, FIND, &ep, &env_htab, 0);
wdenka68d3ed2002-10-11 08:38:32 +0000591
Macpaul Linf3c615b2011-04-26 16:16:45 +0000592 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000593 }
594
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200595 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200596 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
597 return (char *)(gd->env_buf);
598
599 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000600}
601
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200602/*
603 * Look up variable from environment for restricted C runtime env.
604 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200605int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000606{
607 int i, nxt;
608
Igor Grinbergd09b1782011-11-07 01:13:59 +0000609 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000610 int val, n;
611
Macpaul Linf3c615b2011-04-26 16:16:45 +0000612 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
613 if (nxt >= CONFIG_ENV_SIZE)
614 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000615 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000616
617 val = envmatch((uchar *)name, i);
618 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000619 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200620
wdenka68d3ed2002-10-11 08:38:32 +0000621 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000622 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000623 *buf = env_get_char(val++);
624 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200625 return n;
626 }
627
628 if (n)
629 *--buf = '\0';
630
Wolfgang Denka02a8842011-05-04 10:29:49 +0000631 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
632 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200633
634 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000635 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000636
Macpaul Linf3c615b2011-04-26 16:16:45 +0000637 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000638}
639
Simon Glass4a9b4132011-10-14 13:25:18 +0000640/**
641 * Decode the integer value of an environment variable and return it.
642 *
643 * @param name Name of environemnt variable
644 * @param base Number base to use (normally 10, or 16 for hex)
645 * @param default_val Default value to return if the variable is not
646 * found
647 * @return the decoded value, or default_val if not found
648 */
649ulong getenv_ulong(const char *name, int base, ulong default_val)
650{
651 /*
652 * We can use getenv() here, even before relocation, since the
653 * environment variable value is an integer and thus short.
654 */
655 const char *str = getenv(name);
656
657 return str ? simple_strtoul(str, NULL, base) : default_val;
658}
659
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000660#ifndef CONFIG_SPL_BUILD
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500661#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillips088f1b12012-10-29 13:34:31 +0000662static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
663 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000664{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000665 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000666
Macpaul Linf3c615b2011-04-26 16:16:45 +0000667 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000668}
wdenk8bde7f72003-06-27 21:31:46 +0000669
Mike Frysingerba69dc22008-12-30 02:59:25 -0500670U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200671 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600672 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200673 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500674);
wdenka68d3ed2002-10-11 08:38:32 +0000675#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000676#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000677
678
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200679/*
wdenka68d3ed2002-10-11 08:38:32 +0000680 * Match a name / name=value pair
681 *
682 * s1 is either a simple 'name', or a 'name=value' pair.
683 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000684 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000685 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000686int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000687{
Joe Hershberger586197d2012-10-03 09:38:50 +0000688 if (s1 == NULL)
689 return -1;
690
wdenka68d3ed2002-10-11 08:38:32 +0000691 while (*s1 == env_get_char(i2++))
692 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000693 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000694
wdenka68d3ed2002-10-11 08:38:32 +0000695 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000696 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000697
Macpaul Linf3c615b2011-04-26 16:16:45 +0000698 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000699}
wdenk8bde7f72003-06-27 21:31:46 +0000700
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000701#ifndef CONFIG_SPL_BUILD
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000702static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergd09b1782011-11-07 01:13:59 +0000703 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200704{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000705 int all = 0, flag = 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000706
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000707 debug("Initial value for argc=%d\n", argc);
708 while (--argc > 0 && **++argv == '-') {
709 char *arg = *argv;
710
711 while (*++arg) {
712 switch (*arg) {
713 case 'a': /* default all */
714 all = 1;
715 break;
716 case 'f': /* force */
717 flag |= H_FORCE;
718 break;
719 default:
720 return cmd_usage(cmdtp);
721 }
722 }
723 }
724 debug("Final value for argc=%d\n", argc);
725 if (all && (argc == 0)) {
726 /* Reset the whole environment */
727 set_default_env("## Resetting to default environment\n");
728 return 0;
729 }
730 if (!all && (argc > 0)) {
731 /* Reset individual variables */
732 set_default_vars(argc, argv);
733 return 0;
734 }
735
736 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200737}
wdenk8bde7f72003-06-27 21:31:46 +0000738
Igor Grinbergd09b1782011-11-07 01:13:59 +0000739static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
740 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200741{
Joe Hershberger9d8d6612012-12-11 22:16:36 -0600742 int env_flag = H_INTERACTIVE;
743 int ret = 0;
744
745 debug("Initial value for argc=%d\n", argc);
746 while (argc > 1 && **(argv + 1) == '-') {
747 char *arg = *++argv;
748
749 --argc;
750 while (*++arg) {
751 switch (*arg) {
752 case 'f': /* force */
753 env_flag |= H_FORCE;
754 break;
755 default:
756 return CMD_RET_USAGE;
757 }
758 }
759 }
760 debug("Final value for argc=%d\n", argc);
761
762 env_id++;
763
764 while (--argc > 0) {
765 char *name = *++argv;
766
767 if (!hdelete_r(name, &env_htab, env_flag))
768 ret = 1;
769 }
770
771 return ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200772}
773
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500774#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200775/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100776 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200777 * -t: export as text format; if size is given, data will be
778 * padded with '\0' bytes; if not, one terminating '\0'
779 * will be added (which is included in the "filesize"
780 * setting so you can for exmple copy this to flash and
781 * keep the termination).
782 * -b: export as binary format (name=value pairs separated by
783 * '\0', list end marked by double "\0\0")
784 * -c: export as checksum protected environment format as
785 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100786 * -s size:
787 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200788 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100789 * var... List of variable names that get included into the
790 * export. Without arguments, the whole environment gets
791 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200792 *
793 * With "-c" and size is NOT given, then the export command will
794 * format the data as currently used for the persistent storage,
795 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
796 * prepend a valid CRC32 checksum and, in case of resundant
797 * environment, a "current" redundancy flag. If size is given, this
798 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
799 * checksum and redundancy flag will be inserted.
800 *
801 * With "-b" and "-t", always only the real data (including a
802 * terminating '\0' byte) will be written; here the optional size
803 * argument will be used to make sure not to overflow the user
804 * provided buffer; the command will abort if the size is not
805 * sufficient. Any remainign space will be '\0' padded.
806 *
807 * On successful return, the variable "filesize" will be set.
808 * Note that filesize includes the trailing/terminating '\0' byte(s).
809 *
810 * Usage szenario: create a text snapshot/backup of the current settings:
811 *
812 * => env export -t 100000
813 * => era ${backup_addr} +${filesize}
814 * => cp.b 100000 ${backup_addr} ${filesize}
815 *
816 * Re-import this snapshot, deleting all other settings:
817 *
818 * => env import -d -t ${backup_addr}
819 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000820static int do_env_export(cmd_tbl_t *cmdtp, int flag,
821 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200822{
823 char buf[32];
824 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100825 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200826 ssize_t len;
827 env_t *envp;
828 char sep = '\n';
829 int chk = 0;
830 int fmt = 0;
831
832 cmd = *argv;
833
834 while (--argc > 0 && **++argv == '-') {
835 char *arg = *argv;
836 while (*++arg) {
837 switch (*arg) {
838 case 'b': /* raw binary format */
839 if (fmt++)
840 goto sep_err;
841 sep = '\0';
842 break;
843 case 'c': /* external checksum format */
844 if (fmt++)
845 goto sep_err;
846 sep = '\0';
847 chk = 1;
848 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100849 case 's': /* size given */
850 if (--argc <= 0)
851 return cmd_usage(cmdtp);
852 size = simple_strtoul(*++argv, NULL, 16);
853 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200854 case 't': /* text format */
855 if (fmt++)
856 goto sep_err;
857 sep = '\n';
858 break;
859 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000860 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200861 }
862 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100863NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200864 }
865
Macpaul Linf3c615b2011-04-26 16:16:45 +0000866 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000867 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200868
869 addr = (char *)simple_strtoul(argv[0], NULL, 16);
870
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100871 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200872 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100873
874 argc--;
875 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200876
877 if (sep) { /* export as text file */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600878 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200879 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000880 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200881 return 1;
882 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100883 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200884 setenv("filesize", buf);
885
886 return 0;
887 }
888
889 envp = (env_t *)addr;
890
891 if (chk) /* export as checksum protected block */
892 res = (char *)envp->data;
893 else /* export as raw binary data */
894 res = addr;
895
Joe Hershbergerbe112352012-12-11 22:16:23 -0600896 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200897 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000898 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200899 return 1;
900 }
901
902 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000903 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200904#ifdef CONFIG_ENV_ADDR_REDUND
905 envp->flags = ACTIVE_FLAG;
906#endif
907 }
Simon Glass41ef3722013-02-24 17:33:22 +0000908 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200909
910 return 0;
911
912sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000913 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200914 return 1;
915}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500916#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200917
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500918#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200919/*
920 * env import [-d] [-t | -b | -c] addr [size]
921 * -d: delete existing environment before importing;
922 * otherwise overwrite / append to existion definitions
923 * -t: assume text format; either "size" must be given or the
924 * text data must be '\0' terminated
925 * -b: assume binary format ('\0' separated, "\0\0" terminated)
926 * -c: assume checksum protected environment format
927 * addr: memory address to read from
928 * size: length of input data; if missing, proper '\0'
929 * termination is mandatory
930 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000931static int do_env_import(cmd_tbl_t *cmdtp, int flag,
932 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200933{
934 char *cmd, *addr;
935 char sep = '\n';
936 int chk = 0;
937 int fmt = 0;
938 int del = 0;
939 size_t size;
940
941 cmd = *argv;
942
943 while (--argc > 0 && **++argv == '-') {
944 char *arg = *argv;
945 while (*++arg) {
946 switch (*arg) {
947 case 'b': /* raw binary format */
948 if (fmt++)
949 goto sep_err;
950 sep = '\0';
951 break;
952 case 'c': /* external checksum format */
953 if (fmt++)
954 goto sep_err;
955 sep = '\0';
956 chk = 1;
957 break;
958 case 't': /* text format */
959 if (fmt++)
960 goto sep_err;
961 sep = '\n';
962 break;
963 case 'd':
964 del = 1;
965 break;
966 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000967 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200968 }
969 }
970 }
971
Macpaul Linf3c615b2011-04-26 16:16:45 +0000972 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000973 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200974
975 if (!fmt)
976 printf("## Warning: defaulting to text format\n");
977
978 addr = (char *)simple_strtoul(argv[0], NULL, 16);
979
980 if (argc == 2) {
981 size = simple_strtoul(argv[1], NULL, 16);
982 } else {
983 char *s = addr;
984
985 size = 0;
986
987 while (size < MAX_ENV_SIZE) {
988 if ((*s == sep) && (*(s+1) == '\0'))
989 break;
990 ++s;
991 ++size;
992 }
993 if (size == MAX_ENV_SIZE) {
994 printf("## Warning: Input data exceeds %d bytes"
995 " - truncated\n", MAX_ENV_SIZE);
996 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000997 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +0000998 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200999 }
1000
1001 if (chk) {
1002 uint32_t crc;
1003 env_t *ep = (env_t *)addr;
1004
1005 size -= offsetof(env_t, data);
1006 memcpy(&crc, &ep->crc, sizeof(crc));
1007
1008 if (crc32(0, ep->data, size) != crc) {
1009 puts("## Error: bad CRC, import failed\n");
1010 return 1;
1011 }
1012 addr = (char *)ep->data;
1013 }
1014
Gerlando Falauto348b1f12012-08-24 00:11:38 +00001015 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001016 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001017 error("Environment import failed: errno = %d\n", errno);
1018 return 1;
1019 }
1020 gd->flags |= GD_FLG_ENV_READY;
1021
1022 return 0;
1023
1024sep_err:
1025 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1026 cmd);
1027 return 1;
1028}
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001029#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001030
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001031/*
1032 * New command line interface: "env" command with subcommands
1033 */
1034static cmd_tbl_t cmd_env_sub[] = {
1035#if defined(CONFIG_CMD_ASKENV)
1036 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1037#endif
1038 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001039 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001040#if defined(CONFIG_CMD_EDITENV)
1041 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1042#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001043#if defined(CONFIG_CMD_ENV_CALLBACK)
1044 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1045#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001046#if defined(CONFIG_CMD_ENV_FLAGS)
1047 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1048#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001049#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001050 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001051#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001052#if defined(CONFIG_CMD_GREPENV)
1053 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1054#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001055#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001056 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001057#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001058 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1059#if defined(CONFIG_CMD_RUN)
1060 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1061#endif
1062#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1063 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1064#endif
1065 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1066};
1067
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001068#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +02001069void env_reloc(void)
1070{
1071 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1072}
1073#endif
1074
Macpaul Linf3c615b2011-04-26 16:16:45 +00001075static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001076{
1077 cmd_tbl_t *cp;
1078
Thomas Weber5904da02010-11-24 13:07:52 +01001079 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001080 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +01001081
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001082 /* drop initial "env" arg */
1083 argc--;
1084 argv++;
1085
1086 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1087
1088 if (cp)
1089 return cp->cmd(cmdtp, flag, argc, argv);
1090
Simon Glass4c12eeb2011-12-10 08:44:01 +00001091 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001092}
1093
Kim Phillips088f1b12012-10-29 13:34:31 +00001094#ifdef CONFIG_SYS_LONGHELP
1095static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001096#if defined(CONFIG_CMD_ASKENV)
1097 "ask name [message] [size] - ask for environment variable\nenv "
1098#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001099#if defined(CONFIG_CMD_ENV_CALLBACK)
1100 "callbacks - print callbacks and their associated variables\nenv "
1101#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +00001102 "default [-f] -a - [forcibly] reset default environment\n"
1103 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001104 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001105#if defined(CONFIG_CMD_EDITENV)
1106 "env edit name - edit environment variable\n"
1107#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001108#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001109 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001110#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001111#if defined(CONFIG_CMD_ENV_FLAGS)
1112 "env flags - print variables that have non-default flags\n"
1113#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001114#if defined(CONFIG_CMD_GREPENV)
1115 "env grep string [...] - search environment\n"
1116#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001117#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +00001118 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001119#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -06001120 "env print [-a | name ...] - print environment\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001121#if defined(CONFIG_CMD_RUN)
1122 "env run var [...] - run commands in an environment variable\n"
1123#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001124#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001125 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001126#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001127 "env set [-f] name [arg ...]\n";
1128#endif
1129
1130U_BOOT_CMD(
1131 env, CONFIG_SYS_MAXARGS, 1, do_env,
1132 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001133);
1134
1135/*
1136 * Old command line interface, kept for compatibility
1137 */
wdenk8bde7f72003-06-27 21:31:46 +00001138
Peter Tyser246c6922009-10-25 15:12:56 -05001139#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001140U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001141 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001142 "edit environment variable",
1143 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001144 " - edit environment variable 'name'",
1145 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001146);
1147#endif
1148
Mike Frysinger722b0612010-10-20 03:52:39 -04001149U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001150 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001151 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001152 "[-a]\n - print [all] values of all environment variables\n"
wdenk8bde7f72003-06-27 21:31:46 +00001153 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001154 " - print value of environment variable 'name'",
1155 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001156);
1157
Kim Phillipsa000b792011-04-05 07:15:14 +00001158#ifdef CONFIG_CMD_GREPENV
1159U_BOOT_CMD_COMPLETE(
1160 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1161 "search environment variables",
1162 "string ...\n"
1163 " - list environment name=value pairs matching 'string'",
1164 var_complete
1165);
1166#endif
1167
Mike Frysinger722b0612010-10-20 03:52:39 -04001168U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001169 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001170 "set environment variables",
Joe Hershberger24ab5a12012-12-11 22:16:35 -06001171 "[-f] name value ...\n"
1172 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1173 "setenv [-f] name\n"
1174 " - [forcibly] delete environment variable 'name'",
Mike Frysinger722b0612010-10-20 03:52:39 -04001175 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001176);
1177
Jon Loeligerc76fe472007-07-08 18:02:23 -05001178#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001179
wdenk0d498392003-07-01 21:06:45 +00001180U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001181 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001182 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001183 "name [message] [size]\n"
Wolfgang Denk7d855912013-02-20 04:53:16 +00001184 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001185);
Jon Loeliger90253172007-07-10 11:02:44 -05001186#endif
wdenk8bde7f72003-06-27 21:31:46 +00001187
Jon Loeligerc76fe472007-07-08 18:02:23 -05001188#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001189U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001190 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001191 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001192 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001193 " - run the commands in the environment variable(s) 'var'",
1194 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001195);
Jon Loeliger90253172007-07-10 11:02:44 -05001196#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001197#endif /* CONFIG_SPL_BUILD */