blob: 68b0f4f6d809b36e8e8b91a87ad07e4c21473c09 [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;
Maxime Larocque22a4a6c2012-09-28 05:00:13 +000099 ssize_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 */
Maxime Larocque22a4a6c2012-09-28 05:00:13 +0000123 printf("## Error: cannot export environment\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200124 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400125}
126
Kim Phillips088f1b12012-10-29 13:34:31 +0000127static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
128 char * const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400129{
130 int i;
131 int rcode = 0;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600132 int env_flag = H_HIDE_DOT;
133
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
135 argc--;
136 argv++;
137 env_flag &= ~H_HIDE_DOT;
138 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400139
140 if (argc == 1) {
141 /* print all env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600142 rcode = env_print(NULL, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200143 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400144 return 1;
145 printf("\nEnvironment size: %d/%ld bytes\n",
146 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000147 return 0;
148 }
149
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400150 /* print selected env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600151 env_flag &= ~H_HIDE_DOT;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400152 for (i = 1; i < argc; ++i) {
Joe Hershbergerbe112352012-12-11 22:16:23 -0600153 int rc = env_print(argv[i], env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200154 if (!rc) {
155 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400156 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000157 }
158 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400159
wdenka68d3ed2002-10-11 08:38:32 +0000160 return rcode;
161}
162
Kim Phillipsa000b792011-04-05 07:15:14 +0000163#ifdef CONFIG_CMD_GREPENV
Igor Grinbergd09b1782011-11-07 01:13:59 +0000164static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165 int argc, char * const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000166{
167 ENTRY *match;
168 unsigned char matched[env_htab.size / 8];
169 int rcode = 1, arg = 1, idx;
170
171 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000172 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000173
174 memset(matched, 0, env_htab.size / 8);
175
176 while (arg <= argc) {
177 idx = 0;
Kumar Gala068f1582011-11-23 09:48:02 +0000178 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
Kim Phillipsa000b792011-04-05 07:15:14 +0000179 if (!(matched[idx / 8] & (1 << (idx & 7)))) {
180 puts(match->key);
181 puts("=");
182 puts(match->data);
183 puts("\n");
184 }
185 matched[idx / 8] |= 1 << (idx & 7);
186 rcode = 0;
187 }
188 arg++;
189 }
190
191 return rcode;
192}
193#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000194#endif /* CONFIG_SPL_BUILD */
Kim Phillipsa000b792011-04-05 07:15:14 +0000195
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200196/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000197 * Set a new environment variable,
198 * or replace or delete an existing one.
Joe Hershberger25980902012-12-11 22:16:31 -0600199 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000200static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000201{
202 int i, len;
203 char *name, *value, *s;
204 ENTRY e, *ep;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600205 int env_flag = H_INTERACTIVE;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000206
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600207 debug("Initial value for argc=%d\n", argc);
208 while (argc > 1 && **(argv + 1) == '-') {
209 char *arg = *++argv;
210
211 --argc;
212 while (*++arg) {
213 switch (*arg) {
214 case 'f': /* force */
215 env_flag |= H_FORCE;
216 break;
217 default:
218 return CMD_RET_USAGE;
219 }
220 }
221 }
222 debug("Final value for argc=%d\n", argc);
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000223 name = argv[1];
224 value = argv[2];
225
226 if (strchr(name, '=')) {
227 printf("## Error: illegal character '='"
228 "in variable name \"%s\"\n", name);
229 return 1;
230 }
231
232 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000233
wdenka68d3ed2002-10-11 08:38:32 +0000234 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000235 if (argc < 3 || argv[2] == NULL) {
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600236 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200237 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000238 }
239
240 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200241 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000242 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000243 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000244 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000245
246 value = malloc(len);
247 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200248 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000249 return 1;
250 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000251 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000253
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200254 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000255 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000256 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000257 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200258 if (s != value)
259 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000260
Igor Grinbergd09b1782011-11-07 01:13:59 +0000261 e.key = name;
262 e.data = value;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600263 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 free(value);
265 if (!ep) {
266 printf("## Error inserting \"%s\" variable, errno=%d\n",
267 name, errno);
268 return 1;
269 }
wdenka68d3ed2002-10-11 08:38:32 +0000270
wdenka68d3ed2002-10-11 08:38:32 +0000271 return 0;
272}
273
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200274int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000275{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200276 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
277
Joe Hershbergera7eb1d62013-04-08 10:32:50 +0000278 /* before import into hashtable */
279 if (!(gd->flags & GD_FLG_ENV_READY))
280 return 1;
281
Igor Grinbergd09b1782011-11-07 01:13:59 +0000282 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200283 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200284 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200285 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000286}
287
Simon Glassd67f10c2011-10-24 17:59:59 +0000288/**
289 * Set an environment variable to an integer value
290 *
291 * @param varname Environmet variable to set
292 * @param value Value to set it to
293 * @return 0 if ok, 1 on error
294 */
295int setenv_ulong(const char *varname, ulong value)
296{
297 /* TODO: this should be unsigned */
298 char *str = simple_itoa(value);
299
300 return setenv(varname, str);
301}
302
303/**
Simon Glassbfc59962013-02-24 17:33:21 +0000304 * Set an environment variable to an value in hex
Simon Glassd67f10c2011-10-24 17:59:59 +0000305 *
306 * @param varname Environmet variable to set
Simon Glassbfc59962013-02-24 17:33:21 +0000307 * @param value Value to set it to
Simon Glassd67f10c2011-10-24 17:59:59 +0000308 * @return 0 if ok, 1 on error
309 */
Simon Glassbfc59962013-02-24 17:33:21 +0000310int setenv_hex(const char *varname, ulong value)
Simon Glassd67f10c2011-10-24 17:59:59 +0000311{
312 char str[17];
313
Simon Glassbfc59962013-02-24 17:33:21 +0000314 sprintf(str, "%lx", value);
Simon Glassd67f10c2011-10-24 17:59:59 +0000315 return setenv(varname, str);
316}
317
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000318#ifndef CONFIG_SPL_BUILD
Kim Phillips088f1b12012-10-29 13:34:31 +0000319static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000320{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200321 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000322 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000323
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200324 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000325}
326
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200327/*
wdenka68d3ed2002-10-11 08:38:32 +0000328 * Prompt for environment variable
329 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500330#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000331int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000332{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200333 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000334 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000335 char *local_args[4];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000336 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000337
338 local_args[0] = argv[0];
339 local_args[1] = argv[1];
340 local_args[2] = NULL;
341 local_args[3] = NULL;
342
Wolfgang Denk7d855912013-02-20 04:53:16 +0000343 /*
344 * Check the syntax:
345 *
346 * env_ask envname [message1 ...] [size]
347 */
348 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000349 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000350
Wolfgang Denk7d855912013-02-20 04:53:16 +0000351 /*
352 * We test the last argument if it can be converted
353 * into a decimal number. If yes, we assume it's
354 * the size. Otherwise we echo it as part of the
355 * message.
356 */
357 i = simple_strtoul(argv[argc - 1], &endptr, 10);
358 if (*endptr != '\0') { /* no size */
359 size = CONFIG_SYS_CBSIZE - 1;
360 } else { /* size given */
361 size = i;
362 --argc;
363 }
wdenka68d3ed2002-10-11 08:38:32 +0000364
Wolfgang Denk7d855912013-02-20 04:53:16 +0000365 if (argc <= 2) {
366 sprintf(message, "Please enter '%s': ", argv[1]);
367 } else {
368 /* env_ask envname message1 ... messagen [size] */
369 for (i = 2, pos = 0; i < argc; i++) {
Macpaul Linf3c615b2011-04-26 16:16:45 +0000370 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000371 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000372
Igor Grinbergd09b1782011-11-07 01:13:59 +0000373 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000374 pos += strlen(argv[i]);
375 }
Wolfgang Denk7d855912013-02-20 04:53:16 +0000376 message[pos++] = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000377 message[pos] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000378 }
379
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200380 if (size >= CONFIG_SYS_CBSIZE)
381 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000382
383 if (size <= 0)
384 return 1;
385
386 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200387 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000388
389 if (size < len)
390 console_buffer[size] = '\0';
391
392 len = 2;
393 if (console_buffer[0] != '\0') {
394 local_args[2] = console_buffer;
395 len = 3;
396 }
397
398 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200399 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000400}
Jon Loeliger90253172007-07-10 11:02:44 -0500401#endif
wdenka68d3ed2002-10-11 08:38:32 +0000402
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600403#if defined(CONFIG_CMD_ENV_CALLBACK)
404static int print_static_binding(const char *var_name, const char *callback_name)
405{
406 printf("\t%-20s %-20s\n", var_name, callback_name);
407
408 return 0;
409}
410
411static int print_active_callback(ENTRY *entry)
412{
413 struct env_clbk_tbl *clbkp;
414 int i;
415 int num_callbacks;
416
417 if (entry->callback == NULL)
418 return 0;
419
420 /* look up the callback in the linker-list */
421 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
422 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
423 i < num_callbacks;
424 i++, clbkp++) {
425#if defined(CONFIG_NEEDS_MANUAL_RELOC)
426 if (entry->callback == clbkp->callback + gd->reloc_off)
427#else
428 if (entry->callback == clbkp->callback)
429#endif
430 break;
431 }
432
433 if (i == num_callbacks)
434 /* this should probably never happen, but just in case... */
435 printf("\t%-20s %p\n", entry->key, entry->callback);
436 else
437 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
438
439 return 0;
440}
441
442/*
443 * Print the callbacks available and what they are bound to
444 */
445int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
446{
447 struct env_clbk_tbl *clbkp;
448 int i;
449 int num_callbacks;
450
451 /* Print the available callbacks */
452 puts("Available callbacks:\n");
453 puts("\tCallback Name\n");
454 puts("\t-------------\n");
455 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
456 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
457 i < num_callbacks;
458 i++, clbkp++)
459 printf("\t%s\n", clbkp->name);
460 puts("\n");
461
462 /* Print the static bindings that may exist */
463 puts("Static callback bindings:\n");
464 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
465 printf("\t%-20s %-20s\n", "-------------", "-------------");
466 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
467 puts("\n");
468
469 /* walk through each variable and print the callback if it has one */
470 puts("Active callback bindings:\n");
471 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
472 printf("\t%-20s %-20s\n", "-------------", "-------------");
473 hwalk_r(&env_htab, print_active_callback);
474 return 0;
475}
476#endif
477
Joe Hershbergerfffad712012-12-11 22:16:33 -0600478#if defined(CONFIG_CMD_ENV_FLAGS)
479static int print_static_flags(const char *var_name, const char *flags)
480{
481 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600482 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600483
Joe Hershberger267541f2012-12-11 22:16:34 -0600484 printf("\t%-20s %-20s %-20s\n", var_name,
485 env_flags_get_vartype_name(type),
486 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600487
488 return 0;
489}
490
491static int print_active_flags(ENTRY *entry)
492{
493 enum env_flags_vartype type;
Joe Hershberger267541f2012-12-11 22:16:34 -0600494 enum env_flags_varaccess access;
Joe Hershbergerfffad712012-12-11 22:16:33 -0600495
496 if (entry->flags == 0)
497 return 0;
498
499 type = (enum env_flags_vartype)
500 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger267541f2012-12-11 22:16:34 -0600501 access = env_flags_parse_varaccess_from_binflags(entry->flags);
502 printf("\t%-20s %-20s %-20s\n", entry->key,
503 env_flags_get_vartype_name(type),
504 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600505
506 return 0;
507}
508
509/*
510 * Print the flags available and what variables have flags
511 */
512int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
513{
514 /* Print the available variable types */
515 printf("Available variable type flags (position %d):\n",
516 ENV_FLAGS_VARTYPE_LOC);
517 puts("\tFlag\tVariable Type Name\n");
518 puts("\t----\t------------------\n");
519 env_flags_print_vartypes();
520 puts("\n");
521
Joe Hershberger267541f2012-12-11 22:16:34 -0600522 /* Print the available variable access types */
523 printf("Available variable access flags (position %d):\n",
524 ENV_FLAGS_VARACCESS_LOC);
525 puts("\tFlag\tVariable Access Name\n");
526 puts("\t----\t--------------------\n");
527 env_flags_print_varaccess();
528 puts("\n");
529
Joe Hershbergerfffad712012-12-11 22:16:33 -0600530 /* Print the static flags that may exist */
531 puts("Static flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600532 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
533 "Variable Access");
534 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
535 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600536 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
537 puts("\n");
538
539 /* walk through each variable and print the flags if non-default */
540 puts("Active flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600541 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
542 "Variable Access");
543 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
544 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600545 hwalk_r(&env_htab, print_active_flags);
546 return 0;
547}
548#endif
549
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200550/*
Peter Tyser246c6922009-10-25 15:12:56 -0500551 * Interactively edit an environment variable
552 */
553#if defined(CONFIG_CMD_EDITENV)
Kim Phillips088f1b12012-10-29 13:34:31 +0000554static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
555 char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500556{
557 char buffer[CONFIG_SYS_CBSIZE];
558 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500559
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200560 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000561 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500562
563 /* Set read buffer to initial value or empty sting */
564 init_val = getenv(argv[1]);
565 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200566 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500567 else
568 buffer[0] = '\0';
569
Joe Hershberger18a3cce2013-02-08 10:12:34 +0000570 if (readline_into_buffer("edit: ", buffer, 0) < 0)
571 return 1;
Peter Tyser246c6922009-10-25 15:12:56 -0500572
573 return setenv(argv[1], buffer);
574}
575#endif /* CONFIG_CMD_EDITENV */
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000576#endif /* CONFIG_SPL_BUILD */
Peter Tyser246c6922009-10-25 15:12:56 -0500577
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200578/*
wdenka68d3ed2002-10-11 08:38:32 +0000579 * Look up variable from environment,
580 * return address of storage for that variable,
581 * or NULL if not found
582 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200583char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000584{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000585 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200586 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000587
Wolfgang Denk91a76752010-07-24 20:22:02 +0200588 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000589
Igor Grinbergd09b1782011-11-07 01:13:59 +0000590 e.key = name;
591 e.data = NULL;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600592 hsearch_r(e, FIND, &ep, &env_htab, 0);
wdenka68d3ed2002-10-11 08:38:32 +0000593
Macpaul Linf3c615b2011-04-26 16:16:45 +0000594 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000595 }
596
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200597 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200598 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
599 return (char *)(gd->env_buf);
600
601 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000602}
603
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200604/*
605 * Look up variable from environment for restricted C runtime env.
606 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200607int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000608{
609 int i, nxt;
610
Igor Grinbergd09b1782011-11-07 01:13:59 +0000611 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000612 int val, n;
613
Macpaul Linf3c615b2011-04-26 16:16:45 +0000614 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
615 if (nxt >= CONFIG_ENV_SIZE)
616 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000617 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000618
619 val = envmatch((uchar *)name, i);
620 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000621 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200622
wdenka68d3ed2002-10-11 08:38:32 +0000623 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000624 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000625 *buf = env_get_char(val++);
626 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200627 return n;
628 }
629
630 if (n)
631 *--buf = '\0';
632
Wolfgang Denka02a8842011-05-04 10:29:49 +0000633 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
634 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200635
636 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000637 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000638
Macpaul Linf3c615b2011-04-26 16:16:45 +0000639 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000640}
641
Simon Glass4a9b4132011-10-14 13:25:18 +0000642/**
643 * Decode the integer value of an environment variable and return it.
644 *
645 * @param name Name of environemnt variable
646 * @param base Number base to use (normally 10, or 16 for hex)
647 * @param default_val Default value to return if the variable is not
648 * found
649 * @return the decoded value, or default_val if not found
650 */
651ulong getenv_ulong(const char *name, int base, ulong default_val)
652{
653 /*
654 * We can use getenv() here, even before relocation, since the
655 * environment variable value is an integer and thus short.
656 */
657 const char *str = getenv(name);
658
659 return str ? simple_strtoul(str, NULL, base) : default_val;
660}
661
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000662#ifndef CONFIG_SPL_BUILD
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500663#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillips088f1b12012-10-29 13:34:31 +0000664static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
665 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000666{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000667 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000668
Macpaul Linf3c615b2011-04-26 16:16:45 +0000669 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000670}
wdenk8bde7f72003-06-27 21:31:46 +0000671
Mike Frysingerba69dc22008-12-30 02:59:25 -0500672U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200673 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600674 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200675 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500676);
wdenka68d3ed2002-10-11 08:38:32 +0000677#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000678#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000679
680
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200681/*
wdenka68d3ed2002-10-11 08:38:32 +0000682 * Match a name / name=value pair
683 *
684 * s1 is either a simple 'name', or a 'name=value' pair.
685 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000686 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000687 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000688int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000689{
Joe Hershberger586197d2012-10-03 09:38:50 +0000690 if (s1 == NULL)
691 return -1;
692
wdenka68d3ed2002-10-11 08:38:32 +0000693 while (*s1 == env_get_char(i2++))
694 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000695 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000696
wdenka68d3ed2002-10-11 08:38:32 +0000697 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000698 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000699
Macpaul Linf3c615b2011-04-26 16:16:45 +0000700 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000701}
wdenk8bde7f72003-06-27 21:31:46 +0000702
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000703#ifndef CONFIG_SPL_BUILD
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000704static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergd09b1782011-11-07 01:13:59 +0000705 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200706{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000707 int all = 0, flag = 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000708
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000709 debug("Initial value for argc=%d\n", argc);
710 while (--argc > 0 && **++argv == '-') {
711 char *arg = *argv;
712
713 while (*++arg) {
714 switch (*arg) {
715 case 'a': /* default all */
716 all = 1;
717 break;
718 case 'f': /* force */
719 flag |= H_FORCE;
720 break;
721 default:
722 return cmd_usage(cmdtp);
723 }
724 }
725 }
726 debug("Final value for argc=%d\n", argc);
727 if (all && (argc == 0)) {
728 /* Reset the whole environment */
729 set_default_env("## Resetting to default environment\n");
730 return 0;
731 }
732 if (!all && (argc > 0)) {
733 /* Reset individual variables */
734 set_default_vars(argc, argv);
735 return 0;
736 }
737
738 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200739}
wdenk8bde7f72003-06-27 21:31:46 +0000740
Igor Grinbergd09b1782011-11-07 01:13:59 +0000741static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
742 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200743{
Joe Hershberger9d8d6612012-12-11 22:16:36 -0600744 int env_flag = H_INTERACTIVE;
745 int ret = 0;
746
747 debug("Initial value for argc=%d\n", argc);
748 while (argc > 1 && **(argv + 1) == '-') {
749 char *arg = *++argv;
750
751 --argc;
752 while (*++arg) {
753 switch (*arg) {
754 case 'f': /* force */
755 env_flag |= H_FORCE;
756 break;
757 default:
758 return CMD_RET_USAGE;
759 }
760 }
761 }
762 debug("Final value for argc=%d\n", argc);
763
764 env_id++;
765
766 while (--argc > 0) {
767 char *name = *++argv;
768
769 if (!hdelete_r(name, &env_htab, env_flag))
770 ret = 1;
771 }
772
773 return ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200774}
775
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500776#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200777/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100778 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200779 * -t: export as text format; if size is given, data will be
780 * padded with '\0' bytes; if not, one terminating '\0'
781 * will be added (which is included in the "filesize"
782 * setting so you can for exmple copy this to flash and
783 * keep the termination).
784 * -b: export as binary format (name=value pairs separated by
785 * '\0', list end marked by double "\0\0")
786 * -c: export as checksum protected environment format as
787 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100788 * -s size:
789 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200790 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100791 * var... List of variable names that get included into the
792 * export. Without arguments, the whole environment gets
793 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200794 *
795 * With "-c" and size is NOT given, then the export command will
796 * format the data as currently used for the persistent storage,
797 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
798 * prepend a valid CRC32 checksum and, in case of resundant
799 * environment, a "current" redundancy flag. If size is given, this
800 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
801 * checksum and redundancy flag will be inserted.
802 *
803 * With "-b" and "-t", always only the real data (including a
804 * terminating '\0' byte) will be written; here the optional size
805 * argument will be used to make sure not to overflow the user
806 * provided buffer; the command will abort if the size is not
807 * sufficient. Any remainign space will be '\0' padded.
808 *
809 * On successful return, the variable "filesize" will be set.
810 * Note that filesize includes the trailing/terminating '\0' byte(s).
811 *
812 * Usage szenario: create a text snapshot/backup of the current settings:
813 *
814 * => env export -t 100000
815 * => era ${backup_addr} +${filesize}
816 * => cp.b 100000 ${backup_addr} ${filesize}
817 *
818 * Re-import this snapshot, deleting all other settings:
819 *
820 * => env import -d -t ${backup_addr}
821 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000822static int do_env_export(cmd_tbl_t *cmdtp, int flag,
823 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200824{
825 char buf[32];
826 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100827 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200828 ssize_t len;
829 env_t *envp;
830 char sep = '\n';
831 int chk = 0;
832 int fmt = 0;
833
834 cmd = *argv;
835
836 while (--argc > 0 && **++argv == '-') {
837 char *arg = *argv;
838 while (*++arg) {
839 switch (*arg) {
840 case 'b': /* raw binary format */
841 if (fmt++)
842 goto sep_err;
843 sep = '\0';
844 break;
845 case 'c': /* external checksum format */
846 if (fmt++)
847 goto sep_err;
848 sep = '\0';
849 chk = 1;
850 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100851 case 's': /* size given */
852 if (--argc <= 0)
853 return cmd_usage(cmdtp);
854 size = simple_strtoul(*++argv, NULL, 16);
855 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200856 case 't': /* text format */
857 if (fmt++)
858 goto sep_err;
859 sep = '\n';
860 break;
861 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000862 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200863 }
864 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100865NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200866 }
867
Macpaul Linf3c615b2011-04-26 16:16:45 +0000868 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000869 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200870
871 addr = (char *)simple_strtoul(argv[0], NULL, 16);
872
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100873 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200874 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100875
876 argc--;
877 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200878
879 if (sep) { /* export as text file */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600880 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200881 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000882 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200883 return 1;
884 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100885 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200886 setenv("filesize", buf);
887
888 return 0;
889 }
890
891 envp = (env_t *)addr;
892
893 if (chk) /* export as checksum protected block */
894 res = (char *)envp->data;
895 else /* export as raw binary data */
896 res = addr;
897
Joe Hershbergerbe112352012-12-11 22:16:23 -0600898 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200899 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000900 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200901 return 1;
902 }
903
904 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000905 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200906#ifdef CONFIG_ENV_ADDR_REDUND
907 envp->flags = ACTIVE_FLAG;
908#endif
909 }
Simon Glass41ef3722013-02-24 17:33:22 +0000910 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200911
912 return 0;
913
914sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000915 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200916 return 1;
917}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500918#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200919
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500920#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200921/*
922 * env import [-d] [-t | -b | -c] addr [size]
923 * -d: delete existing environment before importing;
924 * otherwise overwrite / append to existion definitions
925 * -t: assume text format; either "size" must be given or the
926 * text data must be '\0' terminated
927 * -b: assume binary format ('\0' separated, "\0\0" terminated)
928 * -c: assume checksum protected environment format
929 * addr: memory address to read from
930 * size: length of input data; if missing, proper '\0'
931 * termination is mandatory
932 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000933static int do_env_import(cmd_tbl_t *cmdtp, int flag,
934 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200935{
936 char *cmd, *addr;
937 char sep = '\n';
938 int chk = 0;
939 int fmt = 0;
940 int del = 0;
941 size_t size;
942
943 cmd = *argv;
944
945 while (--argc > 0 && **++argv == '-') {
946 char *arg = *argv;
947 while (*++arg) {
948 switch (*arg) {
949 case 'b': /* raw binary format */
950 if (fmt++)
951 goto sep_err;
952 sep = '\0';
953 break;
954 case 'c': /* external checksum format */
955 if (fmt++)
956 goto sep_err;
957 sep = '\0';
958 chk = 1;
959 break;
960 case 't': /* text format */
961 if (fmt++)
962 goto sep_err;
963 sep = '\n';
964 break;
965 case 'd':
966 del = 1;
967 break;
968 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000969 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200970 }
971 }
972 }
973
Macpaul Linf3c615b2011-04-26 16:16:45 +0000974 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000975 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200976
977 if (!fmt)
978 printf("## Warning: defaulting to text format\n");
979
980 addr = (char *)simple_strtoul(argv[0], NULL, 16);
981
982 if (argc == 2) {
983 size = simple_strtoul(argv[1], NULL, 16);
984 } else {
985 char *s = addr;
986
987 size = 0;
988
989 while (size < MAX_ENV_SIZE) {
990 if ((*s == sep) && (*(s+1) == '\0'))
991 break;
992 ++s;
993 ++size;
994 }
995 if (size == MAX_ENV_SIZE) {
996 printf("## Warning: Input data exceeds %d bytes"
997 " - truncated\n", MAX_ENV_SIZE);
998 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000999 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +00001000 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001001 }
1002
1003 if (chk) {
1004 uint32_t crc;
1005 env_t *ep = (env_t *)addr;
1006
1007 size -= offsetof(env_t, data);
1008 memcpy(&crc, &ep->crc, sizeof(crc));
1009
1010 if (crc32(0, ep->data, size) != crc) {
1011 puts("## Error: bad CRC, import failed\n");
1012 return 1;
1013 }
1014 addr = (char *)ep->data;
1015 }
1016
Gerlando Falauto348b1f12012-08-24 00:11:38 +00001017 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001018 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001019 error("Environment import failed: errno = %d\n", errno);
1020 return 1;
1021 }
1022 gd->flags |= GD_FLG_ENV_READY;
1023
1024 return 0;
1025
1026sep_err:
1027 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1028 cmd);
1029 return 1;
1030}
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001031#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001032
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001033/*
1034 * New command line interface: "env" command with subcommands
1035 */
1036static cmd_tbl_t cmd_env_sub[] = {
1037#if defined(CONFIG_CMD_ASKENV)
1038 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1039#endif
1040 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001041 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001042#if defined(CONFIG_CMD_EDITENV)
1043 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1044#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001045#if defined(CONFIG_CMD_ENV_CALLBACK)
1046 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1047#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001048#if defined(CONFIG_CMD_ENV_FLAGS)
1049 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1050#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001051#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001052 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001053#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001054#if defined(CONFIG_CMD_GREPENV)
1055 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1056#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001057#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001058 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001059#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001060 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1061#if defined(CONFIG_CMD_RUN)
1062 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1063#endif
1064#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1065 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1066#endif
1067 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1068};
1069
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001070#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +02001071void env_reloc(void)
1072{
1073 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1074}
1075#endif
1076
Macpaul Linf3c615b2011-04-26 16:16:45 +00001077static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001078{
1079 cmd_tbl_t *cp;
1080
Thomas Weber5904da02010-11-24 13:07:52 +01001081 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001082 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +01001083
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001084 /* drop initial "env" arg */
1085 argc--;
1086 argv++;
1087
1088 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1089
1090 if (cp)
1091 return cp->cmd(cmdtp, flag, argc, argv);
1092
Simon Glass4c12eeb2011-12-10 08:44:01 +00001093 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001094}
1095
Kim Phillips088f1b12012-10-29 13:34:31 +00001096#ifdef CONFIG_SYS_LONGHELP
1097static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001098#if defined(CONFIG_CMD_ASKENV)
1099 "ask name [message] [size] - ask for environment variable\nenv "
1100#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001101#if defined(CONFIG_CMD_ENV_CALLBACK)
1102 "callbacks - print callbacks and their associated variables\nenv "
1103#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +00001104 "default [-f] -a - [forcibly] reset default environment\n"
1105 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001106 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001107#if defined(CONFIG_CMD_EDITENV)
1108 "env edit name - edit environment variable\n"
1109#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001110#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001111 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001112#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001113#if defined(CONFIG_CMD_ENV_FLAGS)
1114 "env flags - print variables that have non-default flags\n"
1115#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001116#if defined(CONFIG_CMD_GREPENV)
1117 "env grep string [...] - search environment\n"
1118#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001119#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +00001120 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001121#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -06001122 "env print [-a | name ...] - print environment\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001123#if defined(CONFIG_CMD_RUN)
1124 "env run var [...] - run commands in an environment variable\n"
1125#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001126#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001127 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001128#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001129 "env set [-f] name [arg ...]\n";
1130#endif
1131
1132U_BOOT_CMD(
1133 env, CONFIG_SYS_MAXARGS, 1, do_env,
1134 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001135);
1136
1137/*
1138 * Old command line interface, kept for compatibility
1139 */
wdenk8bde7f72003-06-27 21:31:46 +00001140
Peter Tyser246c6922009-10-25 15:12:56 -05001141#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001142U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001143 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001144 "edit environment variable",
1145 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001146 " - edit environment variable 'name'",
1147 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001148);
1149#endif
1150
Mike Frysinger722b0612010-10-20 03:52:39 -04001151U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001152 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001153 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001154 "[-a]\n - print [all] values of all environment variables\n"
wdenk8bde7f72003-06-27 21:31:46 +00001155 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001156 " - print value of environment variable 'name'",
1157 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001158);
1159
Kim Phillipsa000b792011-04-05 07:15:14 +00001160#ifdef CONFIG_CMD_GREPENV
1161U_BOOT_CMD_COMPLETE(
1162 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1163 "search environment variables",
1164 "string ...\n"
1165 " - list environment name=value pairs matching 'string'",
1166 var_complete
1167);
1168#endif
1169
Mike Frysinger722b0612010-10-20 03:52:39 -04001170U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001171 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001172 "set environment variables",
Joe Hershberger24ab5a12012-12-11 22:16:35 -06001173 "[-f] name value ...\n"
1174 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1175 "setenv [-f] name\n"
1176 " - [forcibly] delete environment variable 'name'",
Mike Frysinger722b0612010-10-20 03:52:39 -04001177 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001178);
1179
Jon Loeligerc76fe472007-07-08 18:02:23 -05001180#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001181
wdenk0d498392003-07-01 21:06:45 +00001182U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001183 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001184 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001185 "name [message] [size]\n"
Wolfgang Denk7d855912013-02-20 04:53:16 +00001186 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001187);
Jon Loeliger90253172007-07-10 11:02:44 -05001188#endif
wdenk8bde7f72003-06-27 21:31:46 +00001189
Jon Loeligerc76fe472007-07-08 18:02:23 -05001190#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001191U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001192 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001193 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001194 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001195 " - run the commands in the environment variable(s) 'var'",
1196 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001197);
Jon Loeliger90253172007-07-10 11:02:44 -05001198#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001199#endif /* CONFIG_SPL_BUILD */