blob: afa128ece2d1ed3405f73ea1c9837adde882ddee [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) && \
Joe Hershberger2b744332013-04-08 10:32:51 +000065 !defined(CONFIG_ENV_IS_IN_UBI) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000066 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kim75eb82e2009-02-25 11:31:24 +090067# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Joe Hershberger2b744332013-04-08 10:32:51 +000068SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000069#endif
70
Wolfgang Denkea882ba2010-06-20 23:33:59 +020071/*
72 * Maximum expected input data size for import command
73 */
74#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000075
wdenka68d3ed2002-10-11 08:38:32 +000076/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077 * This variable is incremented on each do_env_set(), so it can
Heiko Schocherda954272009-04-28 08:36:11 +020078 * be used via get_env_id() as an indication, if the environment
79 * has changed or not. So it is possible to reread an environment
80 * variable only if the environment was changed ... done so for
81 * example in NetInitLoop()
82 */
Heiko Schocher2f70c492009-02-10 09:38:52 +010083static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000084
Macpaul Linf3c615b2011-04-26 16:16:45 +000085int get_env_id(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +010086{
87 return env_id;
88}
wdenka68d3ed2002-10-11 08:38:32 +000089
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000090#ifndef CONFIG_SPL_BUILD
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040091/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020092 * Command interface: print one or all environment variables
93 *
94 * Returns 0 in case of error, or length of printed string
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040095 */
Joe Hershbergerbe112352012-12-11 22:16:23 -060096static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000097{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020098 char *res = NULL;
99 size_t len;
wdenka68d3ed2002-10-11 08:38:32 +0000100
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200101 if (name) { /* print a single name */
102 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000103
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200104 e.key = name;
105 e.data = NULL;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600106 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200107 if (ep == NULL)
108 return 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000109 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200110 return len;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400111 }
wdenka68d3ed2002-10-11 08:38:32 +0000112
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200113 /* print whole list */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600114 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200115
116 if (len > 0) {
117 puts(res);
118 free(res);
119 return len;
120 }
121
122 /* should never happen */
123 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400124}
125
Kim Phillips088f1b12012-10-29 13:34:31 +0000126static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
127 char * const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400128{
129 int i;
130 int rcode = 0;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600131 int env_flag = H_HIDE_DOT;
132
133 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
134 argc--;
135 argv++;
136 env_flag &= ~H_HIDE_DOT;
137 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400138
139 if (argc == 1) {
140 /* print all env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600141 rcode = env_print(NULL, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200142 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400143 return 1;
144 printf("\nEnvironment size: %d/%ld bytes\n",
145 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000146 return 0;
147 }
148
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400149 /* print selected env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600150 env_flag &= ~H_HIDE_DOT;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400151 for (i = 1; i < argc; ++i) {
Joe Hershbergerbe112352012-12-11 22:16:23 -0600152 int rc = env_print(argv[i], env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 if (!rc) {
154 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400155 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000156 }
157 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400158
wdenka68d3ed2002-10-11 08:38:32 +0000159 return rcode;
160}
161
Kim Phillipsa000b792011-04-05 07:15:14 +0000162#ifdef CONFIG_CMD_GREPENV
Igor Grinbergd09b1782011-11-07 01:13:59 +0000163static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
164 int argc, char * const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000165{
166 ENTRY *match;
167 unsigned char matched[env_htab.size / 8];
168 int rcode = 1, arg = 1, idx;
169
170 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000171 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000172
173 memset(matched, 0, env_htab.size / 8);
174
175 while (arg <= argc) {
176 idx = 0;
Kumar Gala068f1582011-11-23 09:48:02 +0000177 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
Kim Phillipsa000b792011-04-05 07:15:14 +0000178 if (!(matched[idx / 8] & (1 << (idx & 7)))) {
179 puts(match->key);
180 puts("=");
181 puts(match->data);
182 puts("\n");
183 }
184 matched[idx / 8] |= 1 << (idx & 7);
185 rcode = 0;
186 }
187 arg++;
188 }
189
190 return rcode;
191}
192#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000193#endif /* CONFIG_SPL_BUILD */
Kim Phillipsa000b792011-04-05 07:15:14 +0000194
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200195/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000196 * Set a new environment variable,
197 * or replace or delete an existing one.
Joe Hershberger25980902012-12-11 22:16:31 -0600198 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000199static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000200{
201 int i, len;
202 char *name, *value, *s;
203 ENTRY e, *ep;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600204 int env_flag = H_INTERACTIVE;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000205
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600206 debug("Initial value for argc=%d\n", argc);
207 while (argc > 1 && **(argv + 1) == '-') {
208 char *arg = *++argv;
209
210 --argc;
211 while (*++arg) {
212 switch (*arg) {
213 case 'f': /* force */
214 env_flag |= H_FORCE;
215 break;
216 default:
217 return CMD_RET_USAGE;
218 }
219 }
220 }
221 debug("Final value for argc=%d\n", argc);
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000222 name = argv[1];
223 value = argv[2];
224
225 if (strchr(name, '=')) {
226 printf("## Error: illegal character '='"
227 "in variable name \"%s\"\n", name);
228 return 1;
229 }
230
231 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000232
wdenka68d3ed2002-10-11 08:38:32 +0000233 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000234 if (argc < 3 || argv[2] == NULL) {
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600235 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200236 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000237 }
238
239 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200240 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000241 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000242 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000243 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000244
245 value = malloc(len);
246 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000248 return 1;
249 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000250 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200251 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000252
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200253 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000254 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000255 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000256 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200257 if (s != value)
258 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000259
Igor Grinbergd09b1782011-11-07 01:13:59 +0000260 e.key = name;
261 e.data = value;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600262 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200263 free(value);
264 if (!ep) {
265 printf("## Error inserting \"%s\" variable, errno=%d\n",
266 name, errno);
267 return 1;
268 }
wdenka68d3ed2002-10-11 08:38:32 +0000269
wdenka68d3ed2002-10-11 08:38:32 +0000270 return 0;
271}
272
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200273int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000274{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200275 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
276
Joe Hershbergera7eb1d62013-04-08 10:32:50 +0000277 /* before import into hashtable */
278 if (!(gd->flags & GD_FLG_ENV_READY))
279 return 1;
280
Igor Grinbergd09b1782011-11-07 01:13:59 +0000281 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200282 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200283 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200284 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000285}
286
Simon Glassd67f10c2011-10-24 17:59:59 +0000287/**
288 * Set an environment variable to an integer value
289 *
290 * @param varname Environmet variable to set
291 * @param value Value to set it to
292 * @return 0 if ok, 1 on error
293 */
294int setenv_ulong(const char *varname, ulong value)
295{
296 /* TODO: this should be unsigned */
297 char *str = simple_itoa(value);
298
299 return setenv(varname, str);
300}
301
302/**
Simon Glassbfc59962013-02-24 17:33:21 +0000303 * Set an environment variable to an value in hex
Simon Glassd67f10c2011-10-24 17:59:59 +0000304 *
305 * @param varname Environmet variable to set
Simon Glassbfc59962013-02-24 17:33:21 +0000306 * @param value Value to set it to
Simon Glassd67f10c2011-10-24 17:59:59 +0000307 * @return 0 if ok, 1 on error
308 */
Simon Glassbfc59962013-02-24 17:33:21 +0000309int setenv_hex(const char *varname, ulong value)
Simon Glassd67f10c2011-10-24 17:59:59 +0000310{
311 char str[17];
312
Simon Glassbfc59962013-02-24 17:33:21 +0000313 sprintf(str, "%lx", value);
Simon Glassd67f10c2011-10-24 17:59:59 +0000314 return setenv(varname, str);
315}
316
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000317#ifndef CONFIG_SPL_BUILD
Kim Phillips088f1b12012-10-29 13:34:31 +0000318static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000319{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200320 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000321 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000322
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200323 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000324}
325
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200326/*
wdenka68d3ed2002-10-11 08:38:32 +0000327 * Prompt for environment variable
328 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500329#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000330int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000331{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200332 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000333 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000334 char *local_args[4];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000335 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000336
337 local_args[0] = argv[0];
338 local_args[1] = argv[1];
339 local_args[2] = NULL;
340 local_args[3] = NULL;
341
Wolfgang Denk7d855912013-02-20 04:53:16 +0000342 /*
343 * Check the syntax:
344 *
345 * env_ask envname [message1 ...] [size]
346 */
347 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000348 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000349
Wolfgang Denk7d855912013-02-20 04:53:16 +0000350 /*
351 * We test the last argument if it can be converted
352 * into a decimal number. If yes, we assume it's
353 * the size. Otherwise we echo it as part of the
354 * message.
355 */
356 i = simple_strtoul(argv[argc - 1], &endptr, 10);
357 if (*endptr != '\0') { /* no size */
358 size = CONFIG_SYS_CBSIZE - 1;
359 } else { /* size given */
360 size = i;
361 --argc;
362 }
wdenka68d3ed2002-10-11 08:38:32 +0000363
Wolfgang Denk7d855912013-02-20 04:53:16 +0000364 if (argc <= 2) {
365 sprintf(message, "Please enter '%s': ", argv[1]);
366 } else {
367 /* env_ask envname message1 ... messagen [size] */
368 for (i = 2, pos = 0; i < argc; i++) {
Macpaul Linf3c615b2011-04-26 16:16:45 +0000369 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000370 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000371
Igor Grinbergd09b1782011-11-07 01:13:59 +0000372 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000373 pos += strlen(argv[i]);
374 }
Wolfgang Denk7d855912013-02-20 04:53:16 +0000375 message[pos++] = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000376 message[pos] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000377 }
378
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200379 if (size >= CONFIG_SYS_CBSIZE)
380 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000381
382 if (size <= 0)
383 return 1;
384
385 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200386 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000387
388 if (size < len)
389 console_buffer[size] = '\0';
390
391 len = 2;
392 if (console_buffer[0] != '\0') {
393 local_args[2] = console_buffer;
394 len = 3;
395 }
396
397 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200398 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000399}
Jon Loeliger90253172007-07-10 11:02:44 -0500400#endif
wdenka68d3ed2002-10-11 08:38:32 +0000401
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600402#if defined(CONFIG_CMD_ENV_CALLBACK)
403static int print_static_binding(const char *var_name, const char *callback_name)
404{
405 printf("\t%-20s %-20s\n", var_name, callback_name);
406
407 return 0;
408}
409
410static int print_active_callback(ENTRY *entry)
411{
412 struct env_clbk_tbl *clbkp;
413 int i;
414 int num_callbacks;
415
416 if (entry->callback == NULL)
417 return 0;
418
419 /* look up the callback in the linker-list */
420 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
421 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
422 i < num_callbacks;
423 i++, clbkp++) {
424#if defined(CONFIG_NEEDS_MANUAL_RELOC)
425 if (entry->callback == clbkp->callback + gd->reloc_off)
426#else
427 if (entry->callback == clbkp->callback)
428#endif
429 break;
430 }
431
432 if (i == num_callbacks)
433 /* this should probably never happen, but just in case... */
434 printf("\t%-20s %p\n", entry->key, entry->callback);
435 else
436 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
437
438 return 0;
439}
440
441/*
442 * Print the callbacks available and what they are bound to
443 */
444int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
445{
446 struct env_clbk_tbl *clbkp;
447 int i;
448 int num_callbacks;
449
450 /* Print the available callbacks */
451 puts("Available callbacks:\n");
452 puts("\tCallback Name\n");
453 puts("\t-------------\n");
454 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
455 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
456 i < num_callbacks;
457 i++, clbkp++)
458 printf("\t%s\n", clbkp->name);
459 puts("\n");
460
461 /* Print the static bindings that may exist */
462 puts("Static callback bindings:\n");
463 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
464 printf("\t%-20s %-20s\n", "-------------", "-------------");
465 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
466 puts("\n");
467
468 /* walk through each variable and print the callback if it has one */
469 puts("Active callback bindings:\n");
470 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
471 printf("\t%-20s %-20s\n", "-------------", "-------------");
472 hwalk_r(&env_htab, print_active_callback);
473 return 0;
474}
475#endif
476
Joe Hershbergerfffad712012-12-11 22:16:33 -0600477#if defined(CONFIG_CMD_ENV_FLAGS)
478static int print_static_flags(const char *var_name, const char *flags)
479{
480 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600481 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600482
Joe Hershberger267541f2012-12-11 22:16:34 -0600483 printf("\t%-20s %-20s %-20s\n", var_name,
484 env_flags_get_vartype_name(type),
485 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600486
487 return 0;
488}
489
490static int print_active_flags(ENTRY *entry)
491{
492 enum env_flags_vartype type;
Joe Hershberger267541f2012-12-11 22:16:34 -0600493 enum env_flags_varaccess access;
Joe Hershbergerfffad712012-12-11 22:16:33 -0600494
495 if (entry->flags == 0)
496 return 0;
497
498 type = (enum env_flags_vartype)
499 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger267541f2012-12-11 22:16:34 -0600500 access = env_flags_parse_varaccess_from_binflags(entry->flags);
501 printf("\t%-20s %-20s %-20s\n", entry->key,
502 env_flags_get_vartype_name(type),
503 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600504
505 return 0;
506}
507
508/*
509 * Print the flags available and what variables have flags
510 */
511int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
512{
513 /* Print the available variable types */
514 printf("Available variable type flags (position %d):\n",
515 ENV_FLAGS_VARTYPE_LOC);
516 puts("\tFlag\tVariable Type Name\n");
517 puts("\t----\t------------------\n");
518 env_flags_print_vartypes();
519 puts("\n");
520
Joe Hershberger267541f2012-12-11 22:16:34 -0600521 /* Print the available variable access types */
522 printf("Available variable access flags (position %d):\n",
523 ENV_FLAGS_VARACCESS_LOC);
524 puts("\tFlag\tVariable Access Name\n");
525 puts("\t----\t--------------------\n");
526 env_flags_print_varaccess();
527 puts("\n");
528
Joe Hershbergerfffad712012-12-11 22:16:33 -0600529 /* Print the static flags that may exist */
530 puts("Static flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600531 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
532 "Variable Access");
533 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
534 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600535 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
536 puts("\n");
537
538 /* walk through each variable and print the flags if non-default */
539 puts("Active flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600540 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
541 "Variable Access");
542 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
543 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600544 hwalk_r(&env_htab, print_active_flags);
545 return 0;
546}
547#endif
548
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200549/*
Peter Tyser246c6922009-10-25 15:12:56 -0500550 * Interactively edit an environment variable
551 */
552#if defined(CONFIG_CMD_EDITENV)
Kim Phillips088f1b12012-10-29 13:34:31 +0000553static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
554 char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500555{
556 char buffer[CONFIG_SYS_CBSIZE];
557 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500558
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200559 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000560 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500561
562 /* Set read buffer to initial value or empty sting */
563 init_val = getenv(argv[1]);
564 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200565 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500566 else
567 buffer[0] = '\0';
568
Joe Hershberger18a3cce2013-02-08 10:12:34 +0000569 if (readline_into_buffer("edit: ", buffer, 0) < 0)
570 return 1;
Peter Tyser246c6922009-10-25 15:12:56 -0500571
572 return setenv(argv[1], buffer);
573}
574#endif /* CONFIG_CMD_EDITENV */
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000575#endif /* CONFIG_SPL_BUILD */
Peter Tyser246c6922009-10-25 15:12:56 -0500576
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200577/*
wdenka68d3ed2002-10-11 08:38:32 +0000578 * Look up variable from environment,
579 * return address of storage for that variable,
580 * or NULL if not found
581 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200582char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000583{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000584 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200585 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000586
Wolfgang Denk91a76752010-07-24 20:22:02 +0200587 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000588
Igor Grinbergd09b1782011-11-07 01:13:59 +0000589 e.key = name;
590 e.data = NULL;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600591 hsearch_r(e, FIND, &ep, &env_htab, 0);
wdenka68d3ed2002-10-11 08:38:32 +0000592
Macpaul Linf3c615b2011-04-26 16:16:45 +0000593 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000594 }
595
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200596 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200597 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
598 return (char *)(gd->env_buf);
599
600 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000601}
602
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200603/*
604 * Look up variable from environment for restricted C runtime env.
605 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200606int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000607{
608 int i, nxt;
609
Igor Grinbergd09b1782011-11-07 01:13:59 +0000610 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000611 int val, n;
612
Macpaul Linf3c615b2011-04-26 16:16:45 +0000613 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
614 if (nxt >= CONFIG_ENV_SIZE)
615 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000616 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000617
618 val = envmatch((uchar *)name, i);
619 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000620 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200621
wdenka68d3ed2002-10-11 08:38:32 +0000622 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000623 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000624 *buf = env_get_char(val++);
625 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200626 return n;
627 }
628
629 if (n)
630 *--buf = '\0';
631
Wolfgang Denka02a8842011-05-04 10:29:49 +0000632 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
633 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200634
635 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000636 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000637
Macpaul Linf3c615b2011-04-26 16:16:45 +0000638 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000639}
640
Simon Glass4a9b4132011-10-14 13:25:18 +0000641/**
642 * Decode the integer value of an environment variable and return it.
643 *
644 * @param name Name of environemnt variable
645 * @param base Number base to use (normally 10, or 16 for hex)
646 * @param default_val Default value to return if the variable is not
647 * found
648 * @return the decoded value, or default_val if not found
649 */
650ulong getenv_ulong(const char *name, int base, ulong default_val)
651{
652 /*
653 * We can use getenv() here, even before relocation, since the
654 * environment variable value is an integer and thus short.
655 */
656 const char *str = getenv(name);
657
658 return str ? simple_strtoul(str, NULL, base) : default_val;
659}
660
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000661#ifndef CONFIG_SPL_BUILD
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500662#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillips088f1b12012-10-29 13:34:31 +0000663static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
664 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000665{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000666 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000667
Macpaul Linf3c615b2011-04-26 16:16:45 +0000668 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000669}
wdenk8bde7f72003-06-27 21:31:46 +0000670
Mike Frysingerba69dc22008-12-30 02:59:25 -0500671U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200672 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600673 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200674 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500675);
wdenka68d3ed2002-10-11 08:38:32 +0000676#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000677#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000678
679
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200680/*
wdenka68d3ed2002-10-11 08:38:32 +0000681 * Match a name / name=value pair
682 *
683 * s1 is either a simple 'name', or a 'name=value' pair.
684 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000685 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000686 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000687int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000688{
Joe Hershberger586197d2012-10-03 09:38:50 +0000689 if (s1 == NULL)
690 return -1;
691
wdenka68d3ed2002-10-11 08:38:32 +0000692 while (*s1 == env_get_char(i2++))
693 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000694 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000695
wdenka68d3ed2002-10-11 08:38:32 +0000696 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000697 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000698
Macpaul Linf3c615b2011-04-26 16:16:45 +0000699 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000700}
wdenk8bde7f72003-06-27 21:31:46 +0000701
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000702#ifndef CONFIG_SPL_BUILD
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000703static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergd09b1782011-11-07 01:13:59 +0000704 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200705{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000706 int all = 0, flag = 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000707
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000708 debug("Initial value for argc=%d\n", argc);
709 while (--argc > 0 && **++argv == '-') {
710 char *arg = *argv;
711
712 while (*++arg) {
713 switch (*arg) {
714 case 'a': /* default all */
715 all = 1;
716 break;
717 case 'f': /* force */
718 flag |= H_FORCE;
719 break;
720 default:
721 return cmd_usage(cmdtp);
722 }
723 }
724 }
725 debug("Final value for argc=%d\n", argc);
726 if (all && (argc == 0)) {
727 /* Reset the whole environment */
728 set_default_env("## Resetting to default environment\n");
729 return 0;
730 }
731 if (!all && (argc > 0)) {
732 /* Reset individual variables */
733 set_default_vars(argc, argv);
734 return 0;
735 }
736
737 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200738}
wdenk8bde7f72003-06-27 21:31:46 +0000739
Igor Grinbergd09b1782011-11-07 01:13:59 +0000740static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
741 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200742{
Joe Hershberger9d8d6612012-12-11 22:16:36 -0600743 int env_flag = H_INTERACTIVE;
744 int ret = 0;
745
746 debug("Initial value for argc=%d\n", argc);
747 while (argc > 1 && **(argv + 1) == '-') {
748 char *arg = *++argv;
749
750 --argc;
751 while (*++arg) {
752 switch (*arg) {
753 case 'f': /* force */
754 env_flag |= H_FORCE;
755 break;
756 default:
757 return CMD_RET_USAGE;
758 }
759 }
760 }
761 debug("Final value for argc=%d\n", argc);
762
763 env_id++;
764
765 while (--argc > 0) {
766 char *name = *++argv;
767
768 if (!hdelete_r(name, &env_htab, env_flag))
769 ret = 1;
770 }
771
772 return ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200773}
774
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500775#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200776/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100777 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200778 * -t: export as text format; if size is given, data will be
779 * padded with '\0' bytes; if not, one terminating '\0'
780 * will be added (which is included in the "filesize"
781 * setting so you can for exmple copy this to flash and
782 * keep the termination).
783 * -b: export as binary format (name=value pairs separated by
784 * '\0', list end marked by double "\0\0")
785 * -c: export as checksum protected environment format as
786 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100787 * -s size:
788 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200789 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100790 * var... List of variable names that get included into the
791 * export. Without arguments, the whole environment gets
792 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200793 *
794 * With "-c" and size is NOT given, then the export command will
795 * format the data as currently used for the persistent storage,
796 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
797 * prepend a valid CRC32 checksum and, in case of resundant
798 * environment, a "current" redundancy flag. If size is given, this
799 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
800 * checksum and redundancy flag will be inserted.
801 *
802 * With "-b" and "-t", always only the real data (including a
803 * terminating '\0' byte) will be written; here the optional size
804 * argument will be used to make sure not to overflow the user
805 * provided buffer; the command will abort if the size is not
806 * sufficient. Any remainign space will be '\0' padded.
807 *
808 * On successful return, the variable "filesize" will be set.
809 * Note that filesize includes the trailing/terminating '\0' byte(s).
810 *
811 * Usage szenario: create a text snapshot/backup of the current settings:
812 *
813 * => env export -t 100000
814 * => era ${backup_addr} +${filesize}
815 * => cp.b 100000 ${backup_addr} ${filesize}
816 *
817 * Re-import this snapshot, deleting all other settings:
818 *
819 * => env import -d -t ${backup_addr}
820 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000821static int do_env_export(cmd_tbl_t *cmdtp, int flag,
822 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200823{
824 char buf[32];
825 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100826 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200827 ssize_t len;
828 env_t *envp;
829 char sep = '\n';
830 int chk = 0;
831 int fmt = 0;
832
833 cmd = *argv;
834
835 while (--argc > 0 && **++argv == '-') {
836 char *arg = *argv;
837 while (*++arg) {
838 switch (*arg) {
839 case 'b': /* raw binary format */
840 if (fmt++)
841 goto sep_err;
842 sep = '\0';
843 break;
844 case 'c': /* external checksum format */
845 if (fmt++)
846 goto sep_err;
847 sep = '\0';
848 chk = 1;
849 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100850 case 's': /* size given */
851 if (--argc <= 0)
852 return cmd_usage(cmdtp);
853 size = simple_strtoul(*++argv, NULL, 16);
854 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200855 case 't': /* text format */
856 if (fmt++)
857 goto sep_err;
858 sep = '\n';
859 break;
860 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000861 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200862 }
863 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100864NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200865 }
866
Macpaul Linf3c615b2011-04-26 16:16:45 +0000867 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000868 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200869
870 addr = (char *)simple_strtoul(argv[0], NULL, 16);
871
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100872 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200873 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100874
875 argc--;
876 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200877
878 if (sep) { /* export as text file */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600879 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200880 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000881 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200882 return 1;
883 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100884 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200885 setenv("filesize", buf);
886
887 return 0;
888 }
889
890 envp = (env_t *)addr;
891
892 if (chk) /* export as checksum protected block */
893 res = (char *)envp->data;
894 else /* export as raw binary data */
895 res = addr;
896
Joe Hershbergerbe112352012-12-11 22:16:23 -0600897 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200898 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000899 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200900 return 1;
901 }
902
903 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000904 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200905#ifdef CONFIG_ENV_ADDR_REDUND
906 envp->flags = ACTIVE_FLAG;
907#endif
908 }
Simon Glass41ef3722013-02-24 17:33:22 +0000909 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200910
911 return 0;
912
913sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000914 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200915 return 1;
916}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500917#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200918
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500919#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200920/*
921 * env import [-d] [-t | -b | -c] addr [size]
922 * -d: delete existing environment before importing;
923 * otherwise overwrite / append to existion definitions
924 * -t: assume text format; either "size" must be given or the
925 * text data must be '\0' terminated
926 * -b: assume binary format ('\0' separated, "\0\0" terminated)
927 * -c: assume checksum protected environment format
928 * addr: memory address to read from
929 * size: length of input data; if missing, proper '\0'
930 * termination is mandatory
931 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000932static int do_env_import(cmd_tbl_t *cmdtp, int flag,
933 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200934{
935 char *cmd, *addr;
936 char sep = '\n';
937 int chk = 0;
938 int fmt = 0;
939 int del = 0;
940 size_t size;
941
942 cmd = *argv;
943
944 while (--argc > 0 && **++argv == '-') {
945 char *arg = *argv;
946 while (*++arg) {
947 switch (*arg) {
948 case 'b': /* raw binary format */
949 if (fmt++)
950 goto sep_err;
951 sep = '\0';
952 break;
953 case 'c': /* external checksum format */
954 if (fmt++)
955 goto sep_err;
956 sep = '\0';
957 chk = 1;
958 break;
959 case 't': /* text format */
960 if (fmt++)
961 goto sep_err;
962 sep = '\n';
963 break;
964 case 'd':
965 del = 1;
966 break;
967 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000968 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200969 }
970 }
971 }
972
Macpaul Linf3c615b2011-04-26 16:16:45 +0000973 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000974 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200975
976 if (!fmt)
977 printf("## Warning: defaulting to text format\n");
978
979 addr = (char *)simple_strtoul(argv[0], NULL, 16);
980
981 if (argc == 2) {
982 size = simple_strtoul(argv[1], NULL, 16);
983 } else {
984 char *s = addr;
985
986 size = 0;
987
988 while (size < MAX_ENV_SIZE) {
989 if ((*s == sep) && (*(s+1) == '\0'))
990 break;
991 ++s;
992 ++size;
993 }
994 if (size == MAX_ENV_SIZE) {
995 printf("## Warning: Input data exceeds %d bytes"
996 " - truncated\n", MAX_ENV_SIZE);
997 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000998 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +0000999 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001000 }
1001
1002 if (chk) {
1003 uint32_t crc;
1004 env_t *ep = (env_t *)addr;
1005
1006 size -= offsetof(env_t, data);
1007 memcpy(&crc, &ep->crc, sizeof(crc));
1008
1009 if (crc32(0, ep->data, size) != crc) {
1010 puts("## Error: bad CRC, import failed\n");
1011 return 1;
1012 }
1013 addr = (char *)ep->data;
1014 }
1015
Gerlando Falauto348b1f12012-08-24 00:11:38 +00001016 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001017 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001018 error("Environment import failed: errno = %d\n", errno);
1019 return 1;
1020 }
1021 gd->flags |= GD_FLG_ENV_READY;
1022
1023 return 0;
1024
1025sep_err:
1026 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1027 cmd);
1028 return 1;
1029}
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001030#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001031
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001032/*
1033 * New command line interface: "env" command with subcommands
1034 */
1035static cmd_tbl_t cmd_env_sub[] = {
1036#if defined(CONFIG_CMD_ASKENV)
1037 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1038#endif
1039 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001040 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001041#if defined(CONFIG_CMD_EDITENV)
1042 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1043#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001044#if defined(CONFIG_CMD_ENV_CALLBACK)
1045 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1046#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001047#if defined(CONFIG_CMD_ENV_FLAGS)
1048 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1049#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001050#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001051 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001052#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001053#if defined(CONFIG_CMD_GREPENV)
1054 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1055#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001056#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001057 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001058#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001059 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1060#if defined(CONFIG_CMD_RUN)
1061 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1062#endif
1063#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1064 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1065#endif
1066 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1067};
1068
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001069#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +02001070void env_reloc(void)
1071{
1072 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1073}
1074#endif
1075
Macpaul Linf3c615b2011-04-26 16:16:45 +00001076static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001077{
1078 cmd_tbl_t *cp;
1079
Thomas Weber5904da02010-11-24 13:07:52 +01001080 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001081 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +01001082
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001083 /* drop initial "env" arg */
1084 argc--;
1085 argv++;
1086
1087 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1088
1089 if (cp)
1090 return cp->cmd(cmdtp, flag, argc, argv);
1091
Simon Glass4c12eeb2011-12-10 08:44:01 +00001092 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001093}
1094
Kim Phillips088f1b12012-10-29 13:34:31 +00001095#ifdef CONFIG_SYS_LONGHELP
1096static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001097#if defined(CONFIG_CMD_ASKENV)
1098 "ask name [message] [size] - ask for environment variable\nenv "
1099#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001100#if defined(CONFIG_CMD_ENV_CALLBACK)
1101 "callbacks - print callbacks and their associated variables\nenv "
1102#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +00001103 "default [-f] -a - [forcibly] reset default environment\n"
1104 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001105 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001106#if defined(CONFIG_CMD_EDITENV)
1107 "env edit name - edit environment variable\n"
1108#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001109#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001110 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001111#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001112#if defined(CONFIG_CMD_ENV_FLAGS)
1113 "env flags - print variables that have non-default flags\n"
1114#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001115#if defined(CONFIG_CMD_GREPENV)
1116 "env grep string [...] - search environment\n"
1117#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001118#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +00001119 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001120#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -06001121 "env print [-a | name ...] - print environment\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001122#if defined(CONFIG_CMD_RUN)
1123 "env run var [...] - run commands in an environment variable\n"
1124#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001125#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001126 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001127#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001128 "env set [-f] name [arg ...]\n";
1129#endif
1130
1131U_BOOT_CMD(
1132 env, CONFIG_SYS_MAXARGS, 1, do_env,
1133 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001134);
1135
1136/*
1137 * Old command line interface, kept for compatibility
1138 */
wdenk8bde7f72003-06-27 21:31:46 +00001139
Peter Tyser246c6922009-10-25 15:12:56 -05001140#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001141U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001142 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001143 "edit environment variable",
1144 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001145 " - edit environment variable 'name'",
1146 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001147);
1148#endif
1149
Mike Frysinger722b0612010-10-20 03:52:39 -04001150U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001151 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001152 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001153 "[-a]\n - print [all] values of all environment variables\n"
wdenk8bde7f72003-06-27 21:31:46 +00001154 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001155 " - print value of environment variable 'name'",
1156 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001157);
1158
Kim Phillipsa000b792011-04-05 07:15:14 +00001159#ifdef CONFIG_CMD_GREPENV
1160U_BOOT_CMD_COMPLETE(
1161 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1162 "search environment variables",
1163 "string ...\n"
1164 " - list environment name=value pairs matching 'string'",
1165 var_complete
1166);
1167#endif
1168
Mike Frysinger722b0612010-10-20 03:52:39 -04001169U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001170 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001171 "set environment variables",
Joe Hershberger24ab5a12012-12-11 22:16:35 -06001172 "[-f] name value ...\n"
1173 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1174 "setenv [-f] name\n"
1175 " - [forcibly] delete environment variable 'name'",
Mike Frysinger722b0612010-10-20 03:52:39 -04001176 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001177);
1178
Jon Loeligerc76fe472007-07-08 18:02:23 -05001179#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001180
wdenk0d498392003-07-01 21:06:45 +00001181U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001182 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001183 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001184 "name [message] [size]\n"
Wolfgang Denk7d855912013-02-20 04:53:16 +00001185 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001186);
Jon Loeliger90253172007-07-10 11:02:44 -05001187#endif
wdenk8bde7f72003-06-27 21:31:46 +00001188
Jon Loeligerc76fe472007-07-08 18:02:23 -05001189#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001190U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001191 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001192 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001193 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001194 " - run the commands in the environment variable(s) 'var'",
1195 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001196);
Jon Loeliger90253172007-07-10 11:02:44 -05001197#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001198#endif /* CONFIG_SPL_BUILD */