blob: 9158b96224a2c87657a52fad4d68e989abb109ac [file] [log] [blame]
wdenka68d3ed2002-10-11 08:38:32 +00001/*
Wolfgang Denk565af272013-03-23 23:50:28 +00002 * (C) Copyright 2000-2013
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 Phillipsf0c9d532011-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 Denk460a9ff2010-06-20 23:33:59 +020029/*
wdenka68d3ed2002-10-11 08:38:32 +000030 * Support for persistent environment data
31 *
Wolfgang Denk460a9ff2010-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 Denk460a9ff2010-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 Denk460a9ff2010-06-20 23:33:59 +020046#include <search.h>
47#include <errno.h>
Peter Tyser0deafa22009-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 Denk6405a152006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54
Macpaul Linff2a9f32011-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 Linff2a9f32011-04-26 16:16:45 +000058 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin9765c082012-03-12 23:57:50 +000059 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linff2a9f32011-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 Gang85bcd732012-03-08 00:33:20 +000064 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Joe Hershberger0c5faa82013-04-08 10:32:51 +000065 !defined(CONFIG_ENV_IS_IN_UBI) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000066 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kimf0b1bdd2009-02-25 11:31:24 +090067# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Joe Hershberger0c5faa82013-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 Denk460a9ff2010-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 Denk460a9ff2010-06-20 23:33:59 +020077 * This variable is incremented on each do_env_set(), so it can
Heiko Schocher3b195ff2009-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 Schocher0c303fa2009-02-10 09:38:52 +010083static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000084
Macpaul Linff2a9f32011-04-26 16:16:45 +000085int get_env_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010086{
87 return env_id;
88}
wdenka68d3ed2002-10-11 08:38:32 +000089
Ilya Yanokf7a2c552012-09-18 00:22:50 +000090#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040091/*
Wolfgang Denk460a9ff2010-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 Frysinger87944922009-05-24 02:26:19 -040095 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060096static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000097{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000099 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +0000100
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101 if (name) { /* print a single name */
102 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000103
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200104 e.key = name;
105 e.data = NULL;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600106 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200107 if (ep == NULL)
108 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000109 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 return len;
111 }
wdenka68d3ed2002-10-11 08:38:32 +0000112
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200113 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600114 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 if (len > 0) {
117 puts(res);
118 free(res);
119 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000120 }
121
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200122 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000123 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200124 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400125}
wdenka68d3ed2002-10-11 08:38:32 +0000126
Kim Phillipsdc00a682012-10-29 13:34:31 +0000127static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
128 char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400129{
130 int i;
131 int rcode = 0;
Joe Hershberger79a905e2012-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 }
wdenka68d3ed2002-10-11 08:38:32 +0000139
Mike Frysinger87944922009-05-24 02:26:19 -0400140 if (argc == 1) {
141 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600142 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200143 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400144 return 1;
145 printf("\nEnvironment size: %d/%ld bytes\n",
146 rcode, (ulong)ENV_SIZE);
147 return 0;
148 }
wdenka68d3ed2002-10-11 08:38:32 +0000149
Mike Frysinger87944922009-05-24 02:26:19 -0400150 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600151 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400152 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600153 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200154 if (!rc) {
155 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400156 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000157 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000158 }
159
160 return rcode;
161}
162
163#ifdef CONFIG_CMD_GREPENV
Igor Grinbergde832562011-11-07 01:13:59 +0000164static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165 int argc, char * const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000166{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000167 char *res = NULL;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000168 int len, grep_flags;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000169
170 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000171 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000172
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000173 grep_flags = H_MATCH_BOTH;
174
175 while (argc > 1 && **(argv + 1) == '-') {
176 char *arg = *++argv;
177
178 --argc;
179 while (*++arg) {
180 switch (*arg) {
181 case 'n': /* grep for name */
182 grep_flags = H_MATCH_KEY;
183 break;
184 case 'v': /* grep for value */
185 grep_flags = H_MATCH_DATA;
186 break;
187 case 'b': /* grep for both */
188 grep_flags = H_MATCH_BOTH;
189 break;
190 case '-':
191 goto DONE;
192 default:
193 return CMD_RET_USAGE;
194 }
195 }
196 }
197
198DONE:
Wolfgang Denke27fc122013-03-23 23:50:29 +0000199 len = hexport_r(&env_htab, '\n',
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000200 flag | grep_flags | H_MATCH_SUBSTR,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000201 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000202
Wolfgang Denke27fc122013-03-23 23:50:29 +0000203 if (len > 0) {
204 puts(res);
205 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000206 }
Mike Frysinger87944922009-05-24 02:26:19 -0400207
Wolfgang Denke27fc122013-03-23 23:50:29 +0000208 if (len < 2)
209 return 1;
210
211 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000212}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000213#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000214#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000215
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200216/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000217 * Set a new environment variable,
218 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600219 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000220static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautodc88d592012-08-24 00:11:37 +0000221{
222 int i, len;
223 char *name, *value, *s;
224 ENTRY e, *ep;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600225 int env_flag = H_INTERACTIVE;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000226
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600227 debug("Initial value for argc=%d\n", argc);
228 while (argc > 1 && **(argv + 1) == '-') {
229 char *arg = *++argv;
230
231 --argc;
232 while (*++arg) {
233 switch (*arg) {
234 case 'f': /* force */
235 env_flag |= H_FORCE;
236 break;
237 default:
238 return CMD_RET_USAGE;
239 }
240 }
241 }
242 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000243 name = argv[1];
244 value = argv[2];
245
246 if (strchr(name, '=')) {
247 printf("## Error: illegal character '='"
248 "in variable name \"%s\"\n", name);
249 return 1;
250 }
251
252 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000253
wdenka68d3ed2002-10-11 08:38:32 +0000254 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000255 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600256 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200257 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000258 }
259
260 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200261 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000262 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000263 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000264 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000265
266 value = malloc(len);
267 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200268 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000269 return 1;
270 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000271 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200272 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000273
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200274 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000275 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000276 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000277 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200278 if (s != value)
279 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000280
Igor Grinbergde832562011-11-07 01:13:59 +0000281 e.key = name;
282 e.data = value;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600283 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200284 free(value);
285 if (!ep) {
286 printf("## Error inserting \"%s\" variable, errno=%d\n",
287 name, errno);
288 return 1;
289 }
wdenka68d3ed2002-10-11 08:38:32 +0000290
wdenka68d3ed2002-10-11 08:38:32 +0000291 return 0;
292}
293
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200294int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000295{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200296 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
297
Joe Hershberger3902c602013-04-08 10:32:50 +0000298 /* before import into hashtable */
299 if (!(gd->flags & GD_FLG_ENV_READY))
300 return 1;
301
Igor Grinbergde832562011-11-07 01:13:59 +0000302 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200303 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200304 else
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200305 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000306}
307
Simon Glassb8e926e2011-10-24 17:59:59 +0000308/**
309 * Set an environment variable to an integer value
310 *
311 * @param varname Environmet variable to set
312 * @param value Value to set it to
313 * @return 0 if ok, 1 on error
314 */
315int setenv_ulong(const char *varname, ulong value)
316{
317 /* TODO: this should be unsigned */
318 char *str = simple_itoa(value);
319
320 return setenv(varname, str);
321}
322
323/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000324 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000325 *
326 * @param varname Environmet variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000327 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000328 * @return 0 if ok, 1 on error
329 */
Simon Glass044dc9e2013-02-24 17:33:21 +0000330int setenv_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000331{
332 char str[17];
333
Simon Glass044dc9e2013-02-24 17:33:21 +0000334 sprintf(str, "%lx", value);
Simon Glassb8e926e2011-10-24 17:59:59 +0000335 return setenv(varname, str);
336}
337
Simon Glassabbd5122013-04-20 08:42:43 +0000338ulong getenv_hex(const char *varname, ulong default_val)
339{
340 const char *s;
341 ulong value;
342 char *endp;
343
344 s = getenv(varname);
345 if (s)
346 value = simple_strtoul(s, &endp, 16);
347 if (!s || endp == s)
348 return default_val;
349
350 return value;
351}
352
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000353#ifndef CONFIG_SPL_BUILD
Kim Phillipsdc00a682012-10-29 13:34:31 +0000354static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000355{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200356 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000357 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000358
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200359 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000360}
361
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200362/*
wdenka68d3ed2002-10-11 08:38:32 +0000363 * Prompt for environment variable
364 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500365#if defined(CONFIG_CMD_ASKENV)
Macpaul Linff2a9f32011-04-26 16:16:45 +0000366int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000367{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200368 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000369 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000370 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000371 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000372
373 local_args[0] = argv[0];
374 local_args[1] = argv[1];
375 local_args[2] = NULL;
376 local_args[3] = NULL;
377
Wolfgang Denk586895d2013-02-20 04:53:16 +0000378 /*
379 * Check the syntax:
380 *
381 * env_ask envname [message1 ...] [size]
382 */
383 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000384 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000385
Wolfgang Denk586895d2013-02-20 04:53:16 +0000386 /*
387 * We test the last argument if it can be converted
388 * into a decimal number. If yes, we assume it's
389 * the size. Otherwise we echo it as part of the
390 * message.
391 */
392 i = simple_strtoul(argv[argc - 1], &endptr, 10);
393 if (*endptr != '\0') { /* no size */
394 size = CONFIG_SYS_CBSIZE - 1;
395 } else { /* size given */
396 size = i;
397 --argc;
398 }
wdenka68d3ed2002-10-11 08:38:32 +0000399
Wolfgang Denk586895d2013-02-20 04:53:16 +0000400 if (argc <= 2) {
401 sprintf(message, "Please enter '%s': ", argv[1]);
402 } else {
403 /* env_ask envname message1 ... messagen [size] */
404 for (i = 2, pos = 0; i < argc; i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000405 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000406 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000407
Igor Grinbergde832562011-11-07 01:13:59 +0000408 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000409 pos += strlen(argv[i]);
410 }
Wolfgang Denk586895d2013-02-20 04:53:16 +0000411 message[pos++] = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000412 message[pos] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000413 }
414
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200415 if (size >= CONFIG_SYS_CBSIZE)
416 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000417
418 if (size <= 0)
419 return 1;
420
421 /* prompt for input */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200422 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000423
424 if (size < len)
425 console_buffer[size] = '\0';
426
427 len = 2;
428 if (console_buffer[0] != '\0') {
429 local_args[2] = console_buffer;
430 len = 3;
431 }
432
433 /* Continue calling setenv code */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200434 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000435}
Jon Loeligerd704d912007-07-10 11:02:44 -0500436#endif
wdenka68d3ed2002-10-11 08:38:32 +0000437
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600438#if defined(CONFIG_CMD_ENV_CALLBACK)
439static int print_static_binding(const char *var_name, const char *callback_name)
440{
441 printf("\t%-20s %-20s\n", var_name, callback_name);
442
443 return 0;
444}
445
446static int print_active_callback(ENTRY *entry)
447{
448 struct env_clbk_tbl *clbkp;
449 int i;
450 int num_callbacks;
451
452 if (entry->callback == NULL)
453 return 0;
454
455 /* look up the callback in the linker-list */
456 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
457 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
458 i < num_callbacks;
459 i++, clbkp++) {
460#if defined(CONFIG_NEEDS_MANUAL_RELOC)
461 if (entry->callback == clbkp->callback + gd->reloc_off)
462#else
463 if (entry->callback == clbkp->callback)
464#endif
465 break;
466 }
467
468 if (i == num_callbacks)
469 /* this should probably never happen, but just in case... */
470 printf("\t%-20s %p\n", entry->key, entry->callback);
471 else
472 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
473
474 return 0;
475}
476
477/*
478 * Print the callbacks available and what they are bound to
479 */
480int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
481{
482 struct env_clbk_tbl *clbkp;
483 int i;
484 int num_callbacks;
485
486 /* Print the available callbacks */
487 puts("Available callbacks:\n");
488 puts("\tCallback Name\n");
489 puts("\t-------------\n");
490 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
491 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
492 i < num_callbacks;
493 i++, clbkp++)
494 printf("\t%s\n", clbkp->name);
495 puts("\n");
496
497 /* Print the static bindings that may exist */
498 puts("Static callback bindings:\n");
499 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
500 printf("\t%-20s %-20s\n", "-------------", "-------------");
501 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
502 puts("\n");
503
504 /* walk through each variable and print the callback if it has one */
505 puts("Active callback bindings:\n");
506 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
507 printf("\t%-20s %-20s\n", "-------------", "-------------");
508 hwalk_r(&env_htab, print_active_callback);
509 return 0;
510}
511#endif
512
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600513#if defined(CONFIG_CMD_ENV_FLAGS)
514static int print_static_flags(const char *var_name, const char *flags)
515{
516 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600517 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600518
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600519 printf("\t%-20s %-20s %-20s\n", var_name,
520 env_flags_get_vartype_name(type),
521 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600522
523 return 0;
524}
525
526static int print_active_flags(ENTRY *entry)
527{
528 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600529 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600530
531 if (entry->flags == 0)
532 return 0;
533
534 type = (enum env_flags_vartype)
535 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600536 access = env_flags_parse_varaccess_from_binflags(entry->flags);
537 printf("\t%-20s %-20s %-20s\n", entry->key,
538 env_flags_get_vartype_name(type),
539 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600540
541 return 0;
542}
543
544/*
545 * Print the flags available and what variables have flags
546 */
547int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
548{
549 /* Print the available variable types */
550 printf("Available variable type flags (position %d):\n",
551 ENV_FLAGS_VARTYPE_LOC);
552 puts("\tFlag\tVariable Type Name\n");
553 puts("\t----\t------------------\n");
554 env_flags_print_vartypes();
555 puts("\n");
556
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600557 /* Print the available variable access types */
558 printf("Available variable access flags (position %d):\n",
559 ENV_FLAGS_VARACCESS_LOC);
560 puts("\tFlag\tVariable Access Name\n");
561 puts("\t----\t--------------------\n");
562 env_flags_print_varaccess();
563 puts("\n");
564
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600565 /* Print the static flags that may exist */
566 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600567 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
568 "Variable Access");
569 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
570 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600571 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
572 puts("\n");
573
574 /* walk through each variable and print the flags if non-default */
575 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600576 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
577 "Variable Access");
578 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
579 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600580 hwalk_r(&env_htab, print_active_flags);
581 return 0;
582}
583#endif
584
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200585/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500586 * Interactively edit an environment variable
587 */
588#if defined(CONFIG_CMD_EDITENV)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000589static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
590 char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500591{
592 char buffer[CONFIG_SYS_CBSIZE];
593 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500594
Wolfgang Denk3b683112010-07-17 01:06:04 +0200595 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000596 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500597
598 /* Set read buffer to initial value or empty sting */
599 init_val = getenv(argv[1]);
600 if (init_val)
Marek Vasut305e02a2011-09-26 02:26:03 +0200601 sprintf(buffer, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500602 else
603 buffer[0] = '\0';
604
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000605 if (readline_into_buffer("edit: ", buffer, 0) < 0)
606 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500607
608 return setenv(argv[1], buffer);
609}
610#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000611#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500612
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200613/*
wdenka68d3ed2002-10-11 08:38:32 +0000614 * Look up variable from environment,
615 * return address of storage for that variable,
616 * or NULL if not found
617 */
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200618char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000619{
Igor Grinbergde832562011-11-07 01:13:59 +0000620 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200621 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000622
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200623 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000624
Igor Grinbergde832562011-11-07 01:13:59 +0000625 e.key = name;
626 e.data = NULL;
Joe Hershbergera46f7702012-12-11 22:16:19 -0600627 hsearch_r(e, FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200628
Macpaul Linff2a9f32011-04-26 16:16:45 +0000629 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000630 }
631
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200632 /* restricted capabilities before import */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200633 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
634 return (char *)(gd->env_buf);
635
636 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000637}
638
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200639/*
640 * Look up variable from environment for restricted C runtime env.
641 */
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200642int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000643{
644 int i, nxt;
645
Igor Grinbergde832562011-11-07 01:13:59 +0000646 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000647 int val, n;
648
Macpaul Linff2a9f32011-04-26 16:16:45 +0000649 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
650 if (nxt >= CONFIG_ENV_SIZE)
651 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000652 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000653
654 val = envmatch((uchar *)name, i);
655 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000656 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200657
wdenka68d3ed2002-10-11 08:38:32 +0000658 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000659 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergde832562011-11-07 01:13:59 +0000660 *buf = env_get_char(val++);
661 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200662 return n;
663 }
664
665 if (n)
666 *--buf = '\0';
667
Wolfgang Denk43285472011-05-04 10:29:49 +0000668 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
669 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200670
671 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000672 }
Igor Grinbergde832562011-11-07 01:13:59 +0000673
Macpaul Linff2a9f32011-04-26 16:16:45 +0000674 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000675}
676
Simon Glassbf690f82011-10-14 13:25:18 +0000677/**
678 * Decode the integer value of an environment variable and return it.
679 *
680 * @param name Name of environemnt variable
681 * @param base Number base to use (normally 10, or 16 for hex)
682 * @param default_val Default value to return if the variable is not
683 * found
684 * @return the decoded value, or default_val if not found
685 */
686ulong getenv_ulong(const char *name, int base, ulong default_val)
687{
688 /*
689 * We can use getenv() here, even before relocation, since the
690 * environment variable value is an integer and thus short.
691 */
692 const char *str = getenv(name);
693
694 return str ? simple_strtoul(str, NULL, base) : default_val;
695}
696
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000697#ifndef CONFIG_SPL_BUILD
Mike Frysinger78dcaf42009-01-28 19:08:14 -0500698#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000699static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
700 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000701{
Macpaul Linff2a9f32011-04-26 16:16:45 +0000702 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000703
Macpaul Linff2a9f32011-04-26 16:16:45 +0000704 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000705}
wdenk57b2d802003-06-27 21:31:46 +0000706
Mike Frysinger61583932008-12-30 02:59:25 -0500707U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200708 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600709 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200710 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500711);
wdenka68d3ed2002-10-11 08:38:32 +0000712#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000713#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000714
715
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200716/*
wdenka68d3ed2002-10-11 08:38:32 +0000717 * Match a name / name=value pair
718 *
719 * s1 is either a simple 'name', or a 'name=value' pair.
720 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergde832562011-11-07 01:13:59 +0000721 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000722 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000723int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000724{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000725 if (s1 == NULL)
726 return -1;
727
wdenka68d3ed2002-10-11 08:38:32 +0000728 while (*s1 == env_get_char(i2++))
729 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000730 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000731
wdenka68d3ed2002-10-11 08:38:32 +0000732 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000733 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000734
Macpaul Linff2a9f32011-04-26 16:16:45 +0000735 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000736}
wdenk57b2d802003-06-27 21:31:46 +0000737
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000738#ifndef CONFIG_SPL_BUILD
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000739static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergde832562011-11-07 01:13:59 +0000740 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200741{
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000742 int all = 0, flag = 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000743
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000744 debug("Initial value for argc=%d\n", argc);
745 while (--argc > 0 && **++argv == '-') {
746 char *arg = *argv;
747
748 while (*++arg) {
749 switch (*arg) {
750 case 'a': /* default all */
751 all = 1;
752 break;
753 case 'f': /* force */
754 flag |= H_FORCE;
755 break;
756 default:
757 return cmd_usage(cmdtp);
758 }
759 }
760 }
761 debug("Final value for argc=%d\n", argc);
762 if (all && (argc == 0)) {
763 /* Reset the whole environment */
764 set_default_env("## Resetting to default environment\n");
765 return 0;
766 }
767 if (!all && (argc > 0)) {
768 /* Reset individual variables */
769 set_default_vars(argc, argv);
770 return 0;
771 }
772
773 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200774}
775
Igor Grinbergde832562011-11-07 01:13:59 +0000776static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
777 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200778{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600779 int env_flag = H_INTERACTIVE;
780 int ret = 0;
781
782 debug("Initial value for argc=%d\n", argc);
783 while (argc > 1 && **(argv + 1) == '-') {
784 char *arg = *++argv;
785
786 --argc;
787 while (*++arg) {
788 switch (*arg) {
789 case 'f': /* force */
790 env_flag |= H_FORCE;
791 break;
792 default:
793 return CMD_RET_USAGE;
794 }
795 }
796 }
797 debug("Final value for argc=%d\n", argc);
798
799 env_id++;
800
801 while (--argc > 0) {
802 char *name = *++argv;
803
804 if (!hdelete_r(name, &env_htab, env_flag))
805 ret = 1;
806 }
807
808 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200809}
810
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500811#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200812/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100813 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200814 * -t: export as text format; if size is given, data will be
815 * padded with '\0' bytes; if not, one terminating '\0'
816 * will be added (which is included in the "filesize"
817 * setting so you can for exmple copy this to flash and
818 * keep the termination).
819 * -b: export as binary format (name=value pairs separated by
820 * '\0', list end marked by double "\0\0")
821 * -c: export as checksum protected environment format as
822 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100823 * -s size:
824 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200825 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100826 * var... List of variable names that get included into the
827 * export. Without arguments, the whole environment gets
828 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200829 *
830 * With "-c" and size is NOT given, then the export command will
831 * format the data as currently used for the persistent storage,
832 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
833 * prepend a valid CRC32 checksum and, in case of resundant
834 * environment, a "current" redundancy flag. If size is given, this
835 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
836 * checksum and redundancy flag will be inserted.
837 *
838 * With "-b" and "-t", always only the real data (including a
839 * terminating '\0' byte) will be written; here the optional size
840 * argument will be used to make sure not to overflow the user
841 * provided buffer; the command will abort if the size is not
842 * sufficient. Any remainign space will be '\0' padded.
843 *
844 * On successful return, the variable "filesize" will be set.
845 * Note that filesize includes the trailing/terminating '\0' byte(s).
846 *
847 * Usage szenario: create a text snapshot/backup of the current settings:
848 *
849 * => env export -t 100000
850 * => era ${backup_addr} +${filesize}
851 * => cp.b 100000 ${backup_addr} ${filesize}
852 *
853 * Re-import this snapshot, deleting all other settings:
854 *
855 * => env import -d -t ${backup_addr}
856 */
Igor Grinbergde832562011-11-07 01:13:59 +0000857static int do_env_export(cmd_tbl_t *cmdtp, int flag,
858 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200859{
860 char buf[32];
861 char *addr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100862 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200863 ssize_t len;
864 env_t *envp;
865 char sep = '\n';
866 int chk = 0;
867 int fmt = 0;
868
869 cmd = *argv;
870
871 while (--argc > 0 && **++argv == '-') {
872 char *arg = *argv;
873 while (*++arg) {
874 switch (*arg) {
875 case 'b': /* raw binary format */
876 if (fmt++)
877 goto sep_err;
878 sep = '\0';
879 break;
880 case 'c': /* external checksum format */
881 if (fmt++)
882 goto sep_err;
883 sep = '\0';
884 chk = 1;
885 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100886 case 's': /* size given */
887 if (--argc <= 0)
888 return cmd_usage(cmdtp);
889 size = simple_strtoul(*++argv, NULL, 16);
890 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200891 case 't': /* text format */
892 if (fmt++)
893 goto sep_err;
894 sep = '\n';
895 break;
896 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000897 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200898 }
899 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100900NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200901 }
902
Macpaul Linff2a9f32011-04-26 16:16:45 +0000903 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000904 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000905
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200906 addr = (char *)simple_strtoul(argv[0], NULL, 16);
907
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100908 if (size)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200909 memset(addr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100910
911 argc--;
912 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200913
914 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000915 len = hexport_r(&env_htab, sep,
916 H_MATCH_KEY | H_MATCH_IDENT,
917 &addr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200918 if (len < 0) {
Igor Grinbergde832562011-11-07 01:13:59 +0000919 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200920 return 1;
921 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100922 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200923 setenv("filesize", buf);
924
925 return 0;
926 }
927
928 envp = (env_t *)addr;
929
930 if (chk) /* export as checksum protected block */
931 res = (char *)envp->data;
932 else /* export as raw binary data */
933 res = addr;
934
Wolfgang Denk565af272013-03-23 23:50:28 +0000935 len = hexport_r(&env_htab, '\0',
936 H_MATCH_KEY | H_MATCH_IDENT,
937 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200938 if (len < 0) {
Igor Grinbergde832562011-11-07 01:13:59 +0000939 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200940 return 1;
941 }
942
943 if (chk) {
Igor Grinbergde832562011-11-07 01:13:59 +0000944 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200945#ifdef CONFIG_ENV_ADDR_REDUND
946 envp->flags = ACTIVE_FLAG;
947#endif
948 }
Simon Glass103789d2013-02-24 17:33:22 +0000949 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200950
951 return 0;
952
953sep_err:
Igor Grinbergde832562011-11-07 01:13:59 +0000954 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200955 return 1;
956}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500957#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200958
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500959#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200960/*
961 * env import [-d] [-t | -b | -c] addr [size]
962 * -d: delete existing environment before importing;
963 * otherwise overwrite / append to existion definitions
964 * -t: assume text format; either "size" must be given or the
965 * text data must be '\0' terminated
966 * -b: assume binary format ('\0' separated, "\0\0" terminated)
967 * -c: assume checksum protected environment format
968 * addr: memory address to read from
969 * size: length of input data; if missing, proper '\0'
970 * termination is mandatory
971 */
Igor Grinbergde832562011-11-07 01:13:59 +0000972static int do_env_import(cmd_tbl_t *cmdtp, int flag,
973 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200974{
975 char *cmd, *addr;
976 char sep = '\n';
977 int chk = 0;
978 int fmt = 0;
979 int del = 0;
980 size_t size;
981
982 cmd = *argv;
983
984 while (--argc > 0 && **++argv == '-') {
985 char *arg = *argv;
986 while (*++arg) {
987 switch (*arg) {
988 case 'b': /* raw binary format */
989 if (fmt++)
990 goto sep_err;
991 sep = '\0';
992 break;
993 case 'c': /* external checksum format */
994 if (fmt++)
995 goto sep_err;
996 sep = '\0';
997 chk = 1;
998 break;
999 case 't': /* text format */
1000 if (fmt++)
1001 goto sep_err;
1002 sep = '\n';
1003 break;
1004 case 'd':
1005 del = 1;
1006 break;
1007 default:
Simon Glassa06dfc72011-12-10 08:44:01 +00001008 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001009 }
1010 }
1011 }
1012
Macpaul Linff2a9f32011-04-26 16:16:45 +00001013 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001014 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001015
1016 if (!fmt)
1017 printf("## Warning: defaulting to text format\n");
1018
1019 addr = (char *)simple_strtoul(argv[0], NULL, 16);
1020
1021 if (argc == 2) {
1022 size = simple_strtoul(argv[1], NULL, 16);
1023 } else {
1024 char *s = addr;
1025
1026 size = 0;
1027
1028 while (size < MAX_ENV_SIZE) {
1029 if ((*s == sep) && (*(s+1) == '\0'))
1030 break;
1031 ++s;
1032 ++size;
1033 }
1034 if (size == MAX_ENV_SIZE) {
1035 printf("## Warning: Input data exceeds %d bytes"
1036 " - truncated\n", MAX_ENV_SIZE);
1037 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001038 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001039 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001040 }
1041
1042 if (chk) {
1043 uint32_t crc;
1044 env_t *ep = (env_t *)addr;
1045
1046 size -= offsetof(env_t, data);
1047 memcpy(&crc, &ep->crc, sizeof(crc));
1048
1049 if (crc32(0, ep->data, size) != crc) {
1050 puts("## Error: bad CRC, import failed\n");
1051 return 1;
1052 }
1053 addr = (char *)ep->data;
1054 }
1055
Gerlando Falauto9b0e1ff2012-08-24 00:11:38 +00001056 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergera46f7702012-12-11 22:16:19 -06001057 0, NULL) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001058 error("Environment import failed: errno = %d\n", errno);
1059 return 1;
1060 }
1061 gd->flags |= GD_FLG_ENV_READY;
1062
1063 return 0;
1064
1065sep_err:
1066 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1067 cmd);
1068 return 1;
1069}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001070#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001071
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001072/*
1073 * New command line interface: "env" command with subcommands
1074 */
1075static cmd_tbl_t cmd_env_sub[] = {
1076#if defined(CONFIG_CMD_ASKENV)
1077 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1078#endif
1079 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001080 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001081#if defined(CONFIG_CMD_EDITENV)
1082 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1083#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001084#if defined(CONFIG_CMD_ENV_CALLBACK)
1085 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1086#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001087#if defined(CONFIG_CMD_ENV_FLAGS)
1088 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1089#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001090#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001091 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001092#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001093#if defined(CONFIG_CMD_GREPENV)
1094 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1095#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001096#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001097 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001098#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001099 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1100#if defined(CONFIG_CMD_RUN)
1101 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1102#endif
1103#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1104 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1105#endif
1106 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1107};
1108
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001109#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001110void env_reloc(void)
1111{
1112 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1113}
1114#endif
1115
Macpaul Linff2a9f32011-04-26 16:16:45 +00001116static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001117{
1118 cmd_tbl_t *cp;
1119
Thomas Weber4d282652010-11-24 13:07:52 +01001120 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001121 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001122
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001123 /* drop initial "env" arg */
1124 argc--;
1125 argv++;
1126
1127 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1128
1129 if (cp)
1130 return cp->cmd(cmdtp, flag, argc, argv);
1131
Simon Glassa06dfc72011-12-10 08:44:01 +00001132 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001133}
1134
Kim Phillipsdc00a682012-10-29 13:34:31 +00001135#ifdef CONFIG_SYS_LONGHELP
1136static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001137#if defined(CONFIG_CMD_ASKENV)
1138 "ask name [message] [size] - ask for environment variable\nenv "
1139#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001140#if defined(CONFIG_CMD_ENV_CALLBACK)
1141 "callbacks - print callbacks and their associated variables\nenv "
1142#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001143 "default [-f] -a - [forcibly] reset default environment\n"
1144 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001145 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001146#if defined(CONFIG_CMD_EDITENV)
1147 "env edit name - edit environment variable\n"
1148#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001149#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001150 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001151#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001152#if defined(CONFIG_CMD_ENV_FLAGS)
1153 "env flags - print variables that have non-default flags\n"
1154#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001155#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001156 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001157#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001158#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001159 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001160#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001161 "env print [-a | name ...] - print environment\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001162#if defined(CONFIG_CMD_RUN)
1163 "env run var [...] - run commands in an environment variable\n"
1164#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001165#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001166 "env save - save environment\n"
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001167#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001168 "env set [-f] name [arg ...]\n";
1169#endif
1170
1171U_BOOT_CMD(
1172 env, CONFIG_SYS_MAXARGS, 1, do_env,
1173 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001174);
1175
1176/*
1177 * Old command line interface, kept for compatibility
1178 */
wdenk57b2d802003-06-27 21:31:46 +00001179
Peter Tyser0deafa22009-10-25 15:12:56 -05001180#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001181U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001182 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001183 "edit environment variable",
1184 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001185 " - edit environment variable 'name'",
1186 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001187);
1188#endif
1189
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001190U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001191 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001192 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001193 "[-a]\n - print [all] values of all environment variables\n"
wdenk57b2d802003-06-27 21:31:46 +00001194 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001195 " - print value of environment variable 'name'",
1196 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001197);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001198
1199#ifdef CONFIG_CMD_GREPENV
1200U_BOOT_CMD_COMPLETE(
1201 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1202 "search environment variables",
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001203 "[-n | -v | -b] string ...\n"
1204 " - list environment name=value pairs matching 'string'\n"
1205 " \"-n\": search variable names; \"-v\": search values;\n"
1206 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001207 var_complete
1208);
1209#endif
wdenk57b2d802003-06-27 21:31:46 +00001210
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001211U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001212 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001213 "set environment variables",
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001214 "[-f] name value ...\n"
1215 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1216 "setenv [-f] name\n"
1217 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001218 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001219);
1220
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001221#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001222
wdenkf287a242003-07-01 21:06:45 +00001223U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001224 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001225 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001226 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001227 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001228);
Jon Loeligerd704d912007-07-10 11:02:44 -05001229#endif
wdenk57b2d802003-06-27 21:31:46 +00001230
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001231#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001232U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001233 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001234 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001235 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001236 " - run the commands in the environment variable(s) 'var'",
1237 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001238);
Jon Loeligerd704d912007-07-10 11:02:44 -05001239#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001240#endif /* CONFIG_SPL_BUILD */