blob: c53601cf7457e89475885ab4f1f57870487129be [file] [log] [blame]
wdenka68d3ed2002-10-11 08:38:32 +00001/*
Wolfgang Denkea009d42013-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 Phillipsa000b792011-04-05 07:15:14 +00007 *
8 * Copyright 2011 Freescale Semiconductor, Inc.
9 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenka68d3ed2002-10-11 08:38:32 +000011 */
12
Wolfgang Denkea882ba2010-06-20 23:33:59 +020013/*
wdenka68d3ed2002-10-11 08:38:32 +000014 * Support for persistent environment data
15 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020016 * The "environment" is stored on external storage as a list of '\0'
17 * terminated "name=value" strings. The end of the list is marked by
18 * a double '\0'. The environment is preceeded by a 32 bit CRC over
19 * the data part and, in case of redundant environment, a byte of
20 * flags.
wdenka68d3ed2002-10-11 08:38:32 +000021 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020022 * This linearized representation will also be used before
23 * relocation, i. e. as long as we don't have a full C runtime
24 * environment. After that, we use a hash table.
wdenka68d3ed2002-10-11 08:38:32 +000025 */
26
27#include <common.h>
28#include <command.h>
29#include <environment.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020030#include <search.h>
31#include <errno.h>
Peter Tyser246c6922009-10-25 15:12:56 -050032#include <malloc.h>
wdenk2a3cb022002-11-05 21:01:48 +000033#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000034#include <linux/stddef.h>
35#include <asm/byteorder.h>
wdenka68d3ed2002-10-11 08:38:32 +000036
Wolfgang Denkd87080b2006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
Macpaul Linf3c615b2011-04-26 16:16:45 +000039#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
40 !defined(CONFIG_ENV_IS_IN_FLASH) && \
41 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000042 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin57210c72012-03-12 23:57:50 +000043 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000044 !defined(CONFIG_ENV_IS_IN_NAND) && \
45 !defined(CONFIG_ENV_IS_IN_NVRAM) && \
46 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
47 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
Liu Gang0a85a9e2012-03-08 00:33:20 +000048 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Joe Hershberger2b744332013-04-08 10:32:51 +000049 !defined(CONFIG_ENV_IS_IN_UBI) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000050 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kim75eb82e2009-02-25 11:31:24 +090051# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Joe Hershberger2b744332013-04-08 10:32:51 +000052SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000053#endif
54
Wolfgang Denkea882ba2010-06-20 23:33:59 +020055/*
56 * Maximum expected input data size for import command
57 */
58#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000059
wdenka68d3ed2002-10-11 08:38:32 +000060/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020061 * This variable is incremented on each do_env_set(), so it can
Heiko Schocherda954272009-04-28 08:36:11 +020062 * be used via get_env_id() as an indication, if the environment
63 * has changed or not. So it is possible to reread an environment
64 * variable only if the environment was changed ... done so for
65 * example in NetInitLoop()
66 */
Heiko Schocher2f70c492009-02-10 09:38:52 +010067static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000068
Macpaul Linf3c615b2011-04-26 16:16:45 +000069int get_env_id(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +010070{
71 return env_id;
72}
wdenka68d3ed2002-10-11 08:38:32 +000073
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000074#ifndef CONFIG_SPL_BUILD
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040075/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076 * Command interface: print one or all environment variables
77 *
78 * Returns 0 in case of error, or length of printed string
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040079 */
Joe Hershbergerbe112352012-12-11 22:16:23 -060080static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000081{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020082 char *res = NULL;
Maxime Larocque22a4a6c2012-09-28 05:00:13 +000083 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000084
Wolfgang Denkea882ba2010-06-20 23:33:59 +020085 if (name) { /* print a single name */
86 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000087
Wolfgang Denkea882ba2010-06-20 23:33:59 +020088 e.key = name;
89 e.data = NULL;
Joe Hershbergerbe112352012-12-11 22:16:23 -060090 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020091 if (ep == NULL)
92 return 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +000093 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020094 return len;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040095 }
wdenka68d3ed2002-10-11 08:38:32 +000096
Wolfgang Denkea882ba2010-06-20 23:33:59 +020097 /* print whole list */
Joe Hershbergerbe112352012-12-11 22:16:23 -060098 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020099
100 if (len > 0) {
101 puts(res);
102 free(res);
103 return len;
104 }
105
106 /* should never happen */
Maxime Larocque22a4a6c2012-09-28 05:00:13 +0000107 printf("## Error: cannot export environment\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200108 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400109}
110
Kim Phillips088f1b12012-10-29 13:34:31 +0000111static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
112 char * const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400113{
114 int i;
115 int rcode = 0;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600116 int env_flag = H_HIDE_DOT;
117
118 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
119 argc--;
120 argv++;
121 env_flag &= ~H_HIDE_DOT;
122 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400123
124 if (argc == 1) {
125 /* print all env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600126 rcode = env_print(NULL, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200127 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400128 return 1;
129 printf("\nEnvironment size: %d/%ld bytes\n",
130 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000131 return 0;
132 }
133
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400134 /* print selected env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600135 env_flag &= ~H_HIDE_DOT;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400136 for (i = 1; i < argc; ++i) {
Joe Hershbergerbe112352012-12-11 22:16:23 -0600137 int rc = env_print(argv[i], env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200138 if (!rc) {
139 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400140 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000141 }
142 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400143
wdenka68d3ed2002-10-11 08:38:32 +0000144 return rcode;
145}
146
Kim Phillipsa000b792011-04-05 07:15:14 +0000147#ifdef CONFIG_CMD_GREPENV
Igor Grinbergd09b1782011-11-07 01:13:59 +0000148static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
149 int argc, char * const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000150{
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000151 char *res = NULL;
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000152 int len, grep_how, grep_what;
Kim Phillipsa000b792011-04-05 07:15:14 +0000153
154 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000155 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000156
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000157 grep_how = H_MATCH_SUBSTR; /* default: substring search */
158 grep_what = H_MATCH_BOTH; /* default: grep names and values */
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000159
Pierre Aubert9a832332013-10-08 14:20:27 +0200160 while (--argc > 0 && **++argv == '-') {
161 char *arg = *argv;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000162 while (*++arg) {
163 switch (*arg) {
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000164#ifdef CONFIG_REGEX
165 case 'e': /* use regex matching */
166 grep_how = H_MATCH_REGEX;
167 break;
168#endif
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000169 case 'n': /* grep for name */
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000170 grep_what = H_MATCH_KEY;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000171 break;
172 case 'v': /* grep for value */
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000173 grep_what = H_MATCH_DATA;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000174 break;
175 case 'b': /* grep for both */
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000176 grep_what = H_MATCH_BOTH;
Wolfgang Denkd87244d2013-03-23 23:50:30 +0000177 break;
178 case '-':
179 goto DONE;
180 default:
181 return CMD_RET_USAGE;
182 }
183 }
184 }
185
186DONE:
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000187 len = hexport_r(&env_htab, '\n',
Wolfgang Denkbe29df62013-03-23 23:50:32 +0000188 flag | grep_what | grep_how,
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000189 &res, 0, argc, argv);
Kim Phillipsa000b792011-04-05 07:15:14 +0000190
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000191 if (len > 0) {
192 puts(res);
193 free(res);
Kim Phillipsa000b792011-04-05 07:15:14 +0000194 }
195
Wolfgang Denk5a31ea02013-03-23 23:50:29 +0000196 if (len < 2)
197 return 1;
198
199 return 0;
Kim Phillipsa000b792011-04-05 07:15:14 +0000200}
201#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000202#endif /* CONFIG_SPL_BUILD */
Kim Phillipsa000b792011-04-05 07:15:14 +0000203
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200204/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000205 * Set a new environment variable,
206 * or replace or delete an existing one.
Joe Hershberger25980902012-12-11 22:16:31 -0600207 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000208static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000209{
210 int i, len;
211 char *name, *value, *s;
212 ENTRY e, *ep;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600213 int env_flag = H_INTERACTIVE;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000214
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600215 debug("Initial value for argc=%d\n", argc);
216 while (argc > 1 && **(argv + 1) == '-') {
217 char *arg = *++argv;
218
219 --argc;
220 while (*++arg) {
221 switch (*arg) {
222 case 'f': /* force */
223 env_flag |= H_FORCE;
224 break;
225 default:
226 return CMD_RET_USAGE;
227 }
228 }
229 }
230 debug("Final value for argc=%d\n", argc);
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000231 name = argv[1];
232 value = argv[2];
233
234 if (strchr(name, '=')) {
235 printf("## Error: illegal character '='"
236 "in variable name \"%s\"\n", name);
237 return 1;
238 }
239
240 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000241
wdenka68d3ed2002-10-11 08:38:32 +0000242 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000243 if (argc < 3 || argv[2] == NULL) {
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600244 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200245 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000246 }
247
248 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200249 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000250 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000251 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000252 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000253
254 value = malloc(len);
255 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000257 return 1;
258 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000259 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200260 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000261
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200262 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000263 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000264 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000265 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200266 if (s != value)
267 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000268
Igor Grinbergd09b1782011-11-07 01:13:59 +0000269 e.key = name;
270 e.data = value;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600271 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200272 free(value);
273 if (!ep) {
274 printf("## Error inserting \"%s\" variable, errno=%d\n",
275 name, errno);
276 return 1;
277 }
wdenka68d3ed2002-10-11 08:38:32 +0000278
wdenka68d3ed2002-10-11 08:38:32 +0000279 return 0;
280}
281
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200282int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000283{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200284 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
285
Joe Hershbergera7eb1d62013-04-08 10:32:50 +0000286 /* before import into hashtable */
287 if (!(gd->flags & GD_FLG_ENV_READY))
288 return 1;
289
Igor Grinbergd09b1782011-11-07 01:13:59 +0000290 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200291 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200292 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200293 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000294}
295
Simon Glassd67f10c2011-10-24 17:59:59 +0000296/**
297 * Set an environment variable to an integer value
298 *
Simon Glass96022862013-05-07 06:11:45 +0000299 * @param varname Environment variable to set
Simon Glassd67f10c2011-10-24 17:59:59 +0000300 * @param value Value to set it to
301 * @return 0 if ok, 1 on error
302 */
303int setenv_ulong(const char *varname, ulong value)
304{
305 /* TODO: this should be unsigned */
306 char *str = simple_itoa(value);
307
308 return setenv(varname, str);
309}
310
311/**
Simon Glassbfc59962013-02-24 17:33:21 +0000312 * Set an environment variable to an value in hex
Simon Glassd67f10c2011-10-24 17:59:59 +0000313 *
Simon Glass96022862013-05-07 06:11:45 +0000314 * @param varname Environment variable to set
Simon Glassbfc59962013-02-24 17:33:21 +0000315 * @param value Value to set it to
Simon Glassd67f10c2011-10-24 17:59:59 +0000316 * @return 0 if ok, 1 on error
317 */
Simon Glassbfc59962013-02-24 17:33:21 +0000318int setenv_hex(const char *varname, ulong value)
Simon Glassd67f10c2011-10-24 17:59:59 +0000319{
320 char str[17];
321
Simon Glassbfc59962013-02-24 17:33:21 +0000322 sprintf(str, "%lx", value);
Simon Glassd67f10c2011-10-24 17:59:59 +0000323 return setenv(varname, str);
324}
325
Simon Glass76b8f792013-04-20 08:42:43 +0000326ulong getenv_hex(const char *varname, ulong default_val)
327{
328 const char *s;
329 ulong value;
330 char *endp;
331
332 s = getenv(varname);
333 if (s)
334 value = simple_strtoul(s, &endp, 16);
335 if (!s || endp == s)
336 return default_val;
337
338 return value;
339}
340
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000341#ifndef CONFIG_SPL_BUILD
Kim Phillips088f1b12012-10-29 13:34:31 +0000342static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000343{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200344 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000345 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000346
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200347 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000348}
349
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200350/*
wdenka68d3ed2002-10-11 08:38:32 +0000351 * Prompt for environment variable
352 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500353#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000354int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000355{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200356 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000357 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000358 char *local_args[4];
Wolfgang Denk7d855912013-02-20 04:53:16 +0000359 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000360
361 local_args[0] = argv[0];
362 local_args[1] = argv[1];
363 local_args[2] = NULL;
364 local_args[3] = NULL;
365
Wolfgang Denk7d855912013-02-20 04:53:16 +0000366 /*
367 * Check the syntax:
368 *
369 * env_ask envname [message1 ...] [size]
370 */
371 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000372 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000373
Wolfgang Denk7d855912013-02-20 04:53:16 +0000374 /*
375 * We test the last argument if it can be converted
376 * into a decimal number. If yes, we assume it's
377 * the size. Otherwise we echo it as part of the
378 * message.
379 */
380 i = simple_strtoul(argv[argc - 1], &endptr, 10);
381 if (*endptr != '\0') { /* no size */
382 size = CONFIG_SYS_CBSIZE - 1;
383 } else { /* size given */
384 size = i;
385 --argc;
386 }
wdenka68d3ed2002-10-11 08:38:32 +0000387
Wolfgang Denk7d855912013-02-20 04:53:16 +0000388 if (argc <= 2) {
389 sprintf(message, "Please enter '%s': ", argv[1]);
390 } else {
391 /* env_ask envname message1 ... messagen [size] */
392 for (i = 2, pos = 0; i < argc; i++) {
Macpaul Linf3c615b2011-04-26 16:16:45 +0000393 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000394 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000395
Igor Grinbergd09b1782011-11-07 01:13:59 +0000396 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000397 pos += strlen(argv[i]);
398 }
Wolfgang Denk7d855912013-02-20 04:53:16 +0000399 message[pos++] = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000400 message[pos] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000401 }
402
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200403 if (size >= CONFIG_SYS_CBSIZE)
404 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000405
406 if (size <= 0)
407 return 1;
408
409 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200410 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000411
412 if (size < len)
413 console_buffer[size] = '\0';
414
415 len = 2;
416 if (console_buffer[0] != '\0') {
417 local_args[2] = console_buffer;
418 len = 3;
419 }
420
421 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200422 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000423}
Jon Loeliger90253172007-07-10 11:02:44 -0500424#endif
wdenka68d3ed2002-10-11 08:38:32 +0000425
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600426#if defined(CONFIG_CMD_ENV_CALLBACK)
427static int print_static_binding(const char *var_name, const char *callback_name)
428{
429 printf("\t%-20s %-20s\n", var_name, callback_name);
430
431 return 0;
432}
433
434static int print_active_callback(ENTRY *entry)
435{
436 struct env_clbk_tbl *clbkp;
437 int i;
438 int num_callbacks;
439
440 if (entry->callback == NULL)
441 return 0;
442
443 /* look up the callback in the linker-list */
444 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
445 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
446 i < num_callbacks;
447 i++, clbkp++) {
448#if defined(CONFIG_NEEDS_MANUAL_RELOC)
449 if (entry->callback == clbkp->callback + gd->reloc_off)
450#else
451 if (entry->callback == clbkp->callback)
452#endif
453 break;
454 }
455
456 if (i == num_callbacks)
457 /* this should probably never happen, but just in case... */
458 printf("\t%-20s %p\n", entry->key, entry->callback);
459 else
460 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
461
462 return 0;
463}
464
465/*
466 * Print the callbacks available and what they are bound to
467 */
468int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
469{
470 struct env_clbk_tbl *clbkp;
471 int i;
472 int num_callbacks;
473
474 /* Print the available callbacks */
475 puts("Available callbacks:\n");
476 puts("\tCallback Name\n");
477 puts("\t-------------\n");
478 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
479 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
480 i < num_callbacks;
481 i++, clbkp++)
482 printf("\t%s\n", clbkp->name);
483 puts("\n");
484
485 /* Print the static bindings that may exist */
486 puts("Static callback bindings:\n");
487 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
488 printf("\t%-20s %-20s\n", "-------------", "-------------");
489 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
490 puts("\n");
491
492 /* walk through each variable and print the callback if it has one */
493 puts("Active callback bindings:\n");
494 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
495 printf("\t%-20s %-20s\n", "-------------", "-------------");
496 hwalk_r(&env_htab, print_active_callback);
497 return 0;
498}
499#endif
500
Joe Hershbergerfffad712012-12-11 22:16:33 -0600501#if defined(CONFIG_CMD_ENV_FLAGS)
502static int print_static_flags(const char *var_name, const char *flags)
503{
504 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600505 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600506
Joe Hershberger267541f2012-12-11 22:16:34 -0600507 printf("\t%-20s %-20s %-20s\n", var_name,
508 env_flags_get_vartype_name(type),
509 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600510
511 return 0;
512}
513
514static int print_active_flags(ENTRY *entry)
515{
516 enum env_flags_vartype type;
Joe Hershberger267541f2012-12-11 22:16:34 -0600517 enum env_flags_varaccess access;
Joe Hershbergerfffad712012-12-11 22:16:33 -0600518
519 if (entry->flags == 0)
520 return 0;
521
522 type = (enum env_flags_vartype)
523 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger267541f2012-12-11 22:16:34 -0600524 access = env_flags_parse_varaccess_from_binflags(entry->flags);
525 printf("\t%-20s %-20s %-20s\n", entry->key,
526 env_flags_get_vartype_name(type),
527 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600528
529 return 0;
530}
531
532/*
533 * Print the flags available and what variables have flags
534 */
535int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
536{
537 /* Print the available variable types */
538 printf("Available variable type flags (position %d):\n",
539 ENV_FLAGS_VARTYPE_LOC);
540 puts("\tFlag\tVariable Type Name\n");
541 puts("\t----\t------------------\n");
542 env_flags_print_vartypes();
543 puts("\n");
544
Joe Hershberger267541f2012-12-11 22:16:34 -0600545 /* Print the available variable access types */
546 printf("Available variable access flags (position %d):\n",
547 ENV_FLAGS_VARACCESS_LOC);
548 puts("\tFlag\tVariable Access Name\n");
549 puts("\t----\t--------------------\n");
550 env_flags_print_varaccess();
551 puts("\n");
552
Joe Hershbergerfffad712012-12-11 22:16:33 -0600553 /* Print the static flags that may exist */
554 puts("Static flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600555 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
556 "Variable Access");
557 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
558 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600559 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
560 puts("\n");
561
562 /* walk through each variable and print the flags if non-default */
563 puts("Active flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600564 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
565 "Variable Access");
566 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
567 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600568 hwalk_r(&env_htab, print_active_flags);
569 return 0;
570}
571#endif
572
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200573/*
Peter Tyser246c6922009-10-25 15:12:56 -0500574 * Interactively edit an environment variable
575 */
576#if defined(CONFIG_CMD_EDITENV)
Kim Phillips088f1b12012-10-29 13:34:31 +0000577static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
578 char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500579{
580 char buffer[CONFIG_SYS_CBSIZE];
581 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500582
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200583 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000584 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500585
586 /* Set read buffer to initial value or empty sting */
587 init_val = getenv(argv[1]);
588 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200589 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500590 else
591 buffer[0] = '\0';
592
Joe Hershberger18a3cce2013-02-08 10:12:34 +0000593 if (readline_into_buffer("edit: ", buffer, 0) < 0)
594 return 1;
Peter Tyser246c6922009-10-25 15:12:56 -0500595
596 return setenv(argv[1], buffer);
597}
598#endif /* CONFIG_CMD_EDITENV */
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000599#endif /* CONFIG_SPL_BUILD */
Peter Tyser246c6922009-10-25 15:12:56 -0500600
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200601/*
wdenka68d3ed2002-10-11 08:38:32 +0000602 * Look up variable from environment,
603 * return address of storage for that variable,
604 * or NULL if not found
605 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200606char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000607{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000608 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200609 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000610
Wolfgang Denk91a76752010-07-24 20:22:02 +0200611 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000612
Igor Grinbergd09b1782011-11-07 01:13:59 +0000613 e.key = name;
614 e.data = NULL;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600615 hsearch_r(e, FIND, &ep, &env_htab, 0);
wdenka68d3ed2002-10-11 08:38:32 +0000616
Macpaul Linf3c615b2011-04-26 16:16:45 +0000617 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000618 }
619
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200620 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200621 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
622 return (char *)(gd->env_buf);
623
624 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000625}
626
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200627/*
628 * Look up variable from environment for restricted C runtime env.
629 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200630int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000631{
632 int i, nxt;
633
Igor Grinbergd09b1782011-11-07 01:13:59 +0000634 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000635 int val, n;
636
Macpaul Linf3c615b2011-04-26 16:16:45 +0000637 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
638 if (nxt >= CONFIG_ENV_SIZE)
639 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000640 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000641
642 val = envmatch((uchar *)name, i);
643 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000644 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200645
wdenka68d3ed2002-10-11 08:38:32 +0000646 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000647 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000648 *buf = env_get_char(val++);
649 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200650 return n;
651 }
652
653 if (n)
654 *--buf = '\0';
655
Wolfgang Denka02a8842011-05-04 10:29:49 +0000656 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
657 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200658
659 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000660 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000661
Macpaul Linf3c615b2011-04-26 16:16:45 +0000662 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000663}
664
Simon Glass4a9b4132011-10-14 13:25:18 +0000665/**
666 * Decode the integer value of an environment variable and return it.
667 *
668 * @param name Name of environemnt variable
669 * @param base Number base to use (normally 10, or 16 for hex)
670 * @param default_val Default value to return if the variable is not
671 * found
672 * @return the decoded value, or default_val if not found
673 */
674ulong getenv_ulong(const char *name, int base, ulong default_val)
675{
676 /*
677 * We can use getenv() here, even before relocation, since the
678 * environment variable value is an integer and thus short.
679 */
680 const char *str = getenv(name);
681
682 return str ? simple_strtoul(str, NULL, base) : default_val;
683}
684
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000685#ifndef CONFIG_SPL_BUILD
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500686#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillips088f1b12012-10-29 13:34:31 +0000687static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
688 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000689{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000690 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000691
Macpaul Linf3c615b2011-04-26 16:16:45 +0000692 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000693}
wdenk8bde7f72003-06-27 21:31:46 +0000694
Mike Frysingerba69dc22008-12-30 02:59:25 -0500695U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200696 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600697 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200698 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500699);
wdenka68d3ed2002-10-11 08:38:32 +0000700#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000701#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000702
703
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200704/*
wdenka68d3ed2002-10-11 08:38:32 +0000705 * Match a name / name=value pair
706 *
707 * s1 is either a simple 'name', or a 'name=value' pair.
708 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000709 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000710 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000711int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000712{
Joe Hershberger586197d2012-10-03 09:38:50 +0000713 if (s1 == NULL)
714 return -1;
715
wdenka68d3ed2002-10-11 08:38:32 +0000716 while (*s1 == env_get_char(i2++))
717 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000718 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000719
wdenka68d3ed2002-10-11 08:38:32 +0000720 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000721 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000722
Macpaul Linf3c615b2011-04-26 16:16:45 +0000723 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000724}
wdenk8bde7f72003-06-27 21:31:46 +0000725
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000726#ifndef CONFIG_SPL_BUILD
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000727static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergd09b1782011-11-07 01:13:59 +0000728 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200729{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000730 int all = 0, flag = 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000731
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000732 debug("Initial value for argc=%d\n", argc);
733 while (--argc > 0 && **++argv == '-') {
734 char *arg = *argv;
735
736 while (*++arg) {
737 switch (*arg) {
738 case 'a': /* default all */
739 all = 1;
740 break;
741 case 'f': /* force */
742 flag |= H_FORCE;
743 break;
744 default:
745 return cmd_usage(cmdtp);
746 }
747 }
748 }
749 debug("Final value for argc=%d\n", argc);
750 if (all && (argc == 0)) {
751 /* Reset the whole environment */
752 set_default_env("## Resetting to default environment\n");
753 return 0;
754 }
755 if (!all && (argc > 0)) {
756 /* Reset individual variables */
757 set_default_vars(argc, argv);
758 return 0;
759 }
760
761 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200762}
wdenk8bde7f72003-06-27 21:31:46 +0000763
Igor Grinbergd09b1782011-11-07 01:13:59 +0000764static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
765 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200766{
Joe Hershberger9d8d6612012-12-11 22:16:36 -0600767 int env_flag = H_INTERACTIVE;
768 int ret = 0;
769
770 debug("Initial value for argc=%d\n", argc);
771 while (argc > 1 && **(argv + 1) == '-') {
772 char *arg = *++argv;
773
774 --argc;
775 while (*++arg) {
776 switch (*arg) {
777 case 'f': /* force */
778 env_flag |= H_FORCE;
779 break;
780 default:
781 return CMD_RET_USAGE;
782 }
783 }
784 }
785 debug("Final value for argc=%d\n", argc);
786
787 env_id++;
788
789 while (--argc > 0) {
790 char *name = *++argv;
791
792 if (!hdelete_r(name, &env_htab, env_flag))
793 ret = 1;
794 }
795
796 return ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200797}
798
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500799#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200800/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100801 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200802 * -t: export as text format; if size is given, data will be
803 * padded with '\0' bytes; if not, one terminating '\0'
804 * will be added (which is included in the "filesize"
805 * setting so you can for exmple copy this to flash and
806 * keep the termination).
807 * -b: export as binary format (name=value pairs separated by
808 * '\0', list end marked by double "\0\0")
809 * -c: export as checksum protected environment format as
810 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100811 * -s size:
812 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200813 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100814 * var... List of variable names that get included into the
815 * export. Without arguments, the whole environment gets
816 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200817 *
818 * With "-c" and size is NOT given, then the export command will
819 * format the data as currently used for the persistent storage,
820 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
821 * prepend a valid CRC32 checksum and, in case of resundant
822 * environment, a "current" redundancy flag. If size is given, this
823 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
824 * checksum and redundancy flag will be inserted.
825 *
826 * With "-b" and "-t", always only the real data (including a
827 * terminating '\0' byte) will be written; here the optional size
828 * argument will be used to make sure not to overflow the user
829 * provided buffer; the command will abort if the size is not
830 * sufficient. Any remainign space will be '\0' padded.
831 *
832 * On successful return, the variable "filesize" will be set.
833 * Note that filesize includes the trailing/terminating '\0' byte(s).
834 *
835 * Usage szenario: create a text snapshot/backup of the current settings:
836 *
837 * => env export -t 100000
838 * => era ${backup_addr} +${filesize}
839 * => cp.b 100000 ${backup_addr} ${filesize}
840 *
841 * Re-import this snapshot, deleting all other settings:
842 *
843 * => env import -d -t ${backup_addr}
844 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000845static int do_env_export(cmd_tbl_t *cmdtp, int flag,
846 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200847{
848 char buf[32];
849 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100850 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200851 ssize_t len;
852 env_t *envp;
853 char sep = '\n';
854 int chk = 0;
855 int fmt = 0;
856
857 cmd = *argv;
858
859 while (--argc > 0 && **++argv == '-') {
860 char *arg = *argv;
861 while (*++arg) {
862 switch (*arg) {
863 case 'b': /* raw binary format */
864 if (fmt++)
865 goto sep_err;
866 sep = '\0';
867 break;
868 case 'c': /* external checksum format */
869 if (fmt++)
870 goto sep_err;
871 sep = '\0';
872 chk = 1;
873 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100874 case 's': /* size given */
875 if (--argc <= 0)
876 return cmd_usage(cmdtp);
877 size = simple_strtoul(*++argv, NULL, 16);
878 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200879 case 't': /* text format */
880 if (fmt++)
881 goto sep_err;
882 sep = '\n';
883 break;
884 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000885 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200886 }
887 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100888NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200889 }
890
Macpaul Linf3c615b2011-04-26 16:16:45 +0000891 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000892 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200893
894 addr = (char *)simple_strtoul(argv[0], NULL, 16);
895
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100896 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200897 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100898
899 argc--;
900 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200901
902 if (sep) { /* export as text file */
Wolfgang Denkea009d42013-03-23 23:50:28 +0000903 len = hexport_r(&env_htab, sep,
904 H_MATCH_KEY | H_MATCH_IDENT,
905 &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200906 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000907 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200908 return 1;
909 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100910 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200911 setenv("filesize", buf);
912
913 return 0;
914 }
915
916 envp = (env_t *)addr;
917
918 if (chk) /* export as checksum protected block */
919 res = (char *)envp->data;
920 else /* export as raw binary data */
921 res = addr;
922
Wolfgang Denkea009d42013-03-23 23:50:28 +0000923 len = hexport_r(&env_htab, '\0',
924 H_MATCH_KEY | H_MATCH_IDENT,
925 &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200926 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000927 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200928 return 1;
929 }
930
931 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000932 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200933#ifdef CONFIG_ENV_ADDR_REDUND
934 envp->flags = ACTIVE_FLAG;
935#endif
936 }
Simon Glass41ef3722013-02-24 17:33:22 +0000937 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200938
939 return 0;
940
941sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000942 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200943 return 1;
944}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500945#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200946
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500947#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200948/*
949 * env import [-d] [-t | -b | -c] addr [size]
950 * -d: delete existing environment before importing;
951 * otherwise overwrite / append to existion definitions
952 * -t: assume text format; either "size" must be given or the
953 * text data must be '\0' terminated
954 * -b: assume binary format ('\0' separated, "\0\0" terminated)
955 * -c: assume checksum protected environment format
956 * addr: memory address to read from
957 * size: length of input data; if missing, proper '\0'
958 * termination is mandatory
959 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000960static int do_env_import(cmd_tbl_t *cmdtp, int flag,
961 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200962{
963 char *cmd, *addr;
964 char sep = '\n';
965 int chk = 0;
966 int fmt = 0;
967 int del = 0;
968 size_t size;
969
970 cmd = *argv;
971
972 while (--argc > 0 && **++argv == '-') {
973 char *arg = *argv;
974 while (*++arg) {
975 switch (*arg) {
976 case 'b': /* raw binary format */
977 if (fmt++)
978 goto sep_err;
979 sep = '\0';
980 break;
981 case 'c': /* external checksum format */
982 if (fmt++)
983 goto sep_err;
984 sep = '\0';
985 chk = 1;
986 break;
987 case 't': /* text format */
988 if (fmt++)
989 goto sep_err;
990 sep = '\n';
991 break;
992 case 'd':
993 del = 1;
994 break;
995 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000996 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200997 }
998 }
999 }
1000
Macpaul Linf3c615b2011-04-26 16:16:45 +00001001 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001002 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001003
1004 if (!fmt)
1005 printf("## Warning: defaulting to text format\n");
1006
1007 addr = (char *)simple_strtoul(argv[0], NULL, 16);
1008
1009 if (argc == 2) {
1010 size = simple_strtoul(argv[1], NULL, 16);
Tom Rini3775dcd2014-03-04 15:52:35 -05001011 } else if (argc == 1 && chk) {
1012 puts("## Error: external checksum format must pass size\n");
1013 return CMD_RET_FAILURE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001014 } else {
1015 char *s = addr;
1016
1017 size = 0;
1018
1019 while (size < MAX_ENV_SIZE) {
1020 if ((*s == sep) && (*(s+1) == '\0'))
1021 break;
1022 ++s;
1023 ++size;
1024 }
1025 if (size == MAX_ENV_SIZE) {
1026 printf("## Warning: Input data exceeds %d bytes"
1027 " - truncated\n", MAX_ENV_SIZE);
1028 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +00001029 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +00001030 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001031 }
1032
1033 if (chk) {
1034 uint32_t crc;
1035 env_t *ep = (env_t *)addr;
1036
1037 size -= offsetof(env_t, data);
1038 memcpy(&crc, &ep->crc, sizeof(crc));
1039
1040 if (crc32(0, ep->data, size) != crc) {
1041 puts("## Error: bad CRC, import failed\n");
1042 return 1;
1043 }
1044 addr = (char *)ep->data;
1045 }
1046
Gerlando Falauto348b1f12012-08-24 00:11:38 +00001047 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergerc4e00572012-12-11 22:16:19 -06001048 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001049 error("Environment import failed: errno = %d\n", errno);
1050 return 1;
1051 }
1052 gd->flags |= GD_FLG_ENV_READY;
1053
1054 return 0;
1055
1056sep_err:
1057 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1058 cmd);
1059 return 1;
1060}
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001061#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001062
Andrew Ruder88733e22013-10-22 19:07:34 -05001063#if defined(CONFIG_CMD_ENV_EXISTS)
1064static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1065 char * const argv[])
1066{
1067 ENTRY e, *ep;
1068
1069 if (argc < 2)
1070 return CMD_RET_USAGE;
1071
1072 e.key = argv[1];
1073 e.data = NULL;
1074 hsearch_r(e, FIND, &ep, &env_htab, 0);
1075
1076 return (ep == NULL) ? 1 : 0;
1077}
1078#endif
1079
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001080/*
1081 * New command line interface: "env" command with subcommands
1082 */
1083static cmd_tbl_t cmd_env_sub[] = {
1084#if defined(CONFIG_CMD_ASKENV)
1085 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1086#endif
1087 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001088 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001089#if defined(CONFIG_CMD_EDITENV)
1090 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1091#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001092#if defined(CONFIG_CMD_ENV_CALLBACK)
1093 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1094#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001095#if defined(CONFIG_CMD_ENV_FLAGS)
1096 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1097#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001098#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001099 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001100#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001101#if defined(CONFIG_CMD_GREPENV)
1102 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1103#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001104#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001105 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001106#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001107 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1108#if defined(CONFIG_CMD_RUN)
1109 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1110#endif
1111#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1112 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1113#endif
1114 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder88733e22013-10-22 19:07:34 -05001115#if defined(CONFIG_CMD_ENV_EXISTS)
1116 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1117#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001118};
1119
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001120#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +02001121void env_reloc(void)
1122{
1123 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1124}
1125#endif
1126
Macpaul Linf3c615b2011-04-26 16:16:45 +00001127static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001128{
1129 cmd_tbl_t *cp;
1130
Thomas Weber5904da02010-11-24 13:07:52 +01001131 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001132 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +01001133
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001134 /* drop initial "env" arg */
1135 argc--;
1136 argv++;
1137
1138 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1139
1140 if (cp)
1141 return cp->cmd(cmdtp, flag, argc, argv);
1142
Simon Glass4c12eeb2011-12-10 08:44:01 +00001143 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001144}
1145
Kim Phillips088f1b12012-10-29 13:34:31 +00001146#ifdef CONFIG_SYS_LONGHELP
1147static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001148#if defined(CONFIG_CMD_ASKENV)
1149 "ask name [message] [size] - ask for environment variable\nenv "
1150#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001151#if defined(CONFIG_CMD_ENV_CALLBACK)
1152 "callbacks - print callbacks and their associated variables\nenv "
1153#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +00001154 "default [-f] -a - [forcibly] reset default environment\n"
1155 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger9d8d6612012-12-11 22:16:36 -06001156 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001157#if defined(CONFIG_CMD_EDITENV)
1158 "env edit name - edit environment variable\n"
1159#endif
Andrew Ruder88733e22013-10-22 19:07:34 -05001160#if defined(CONFIG_CMD_ENV_EXISTS)
1161 "env exists name - tests for existence of variable\n"
1162#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001163#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001164 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001165#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001166#if defined(CONFIG_CMD_ENV_FLAGS)
1167 "env flags - print variables that have non-default flags\n"
1168#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001169#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001170#ifdef CONFIG_REGEX
1171 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1172#else
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001173 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsa000b792011-04-05 07:15:14 +00001174#endif
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001175#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001176#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +00001177 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001178#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -06001179 "env print [-a | name ...] - print environment\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001180#if defined(CONFIG_CMD_RUN)
1181 "env run var [...] - run commands in an environment variable\n"
1182#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001183#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001184 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001185#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001186 "env set [-f] name [arg ...]\n";
1187#endif
1188
1189U_BOOT_CMD(
1190 env, CONFIG_SYS_MAXARGS, 1, do_env,
1191 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001192);
1193
1194/*
1195 * Old command line interface, kept for compatibility
1196 */
wdenk8bde7f72003-06-27 21:31:46 +00001197
Peter Tyser246c6922009-10-25 15:12:56 -05001198#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001199U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001200 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001201 "edit environment variable",
1202 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001203 " - edit environment variable 'name'",
1204 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001205);
1206#endif
1207
Mike Frysinger722b0612010-10-20 03:52:39 -04001208U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001209 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001210 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001211 "[-a]\n - print [all] values of all environment variables\n"
wdenk8bde7f72003-06-27 21:31:46 +00001212 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001213 " - print value of environment variable 'name'",
1214 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001215);
1216
Kim Phillipsa000b792011-04-05 07:15:14 +00001217#ifdef CONFIG_CMD_GREPENV
1218U_BOOT_CMD_COMPLETE(
1219 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1220 "search environment variables",
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001221#ifdef CONFIG_REGEX
1222 "[-e] [-n | -v | -b] string ...\n"
1223#else
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001224 "[-n | -v | -b] string ...\n"
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001225#endif
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001226 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkbe29df62013-03-23 23:50:32 +00001227#ifdef CONFIG_REGEX
1228 " \"-e\": enable regular expressions;\n"
1229#endif
Wolfgang Denkd87244d2013-03-23 23:50:30 +00001230 " \"-n\": search variable names; \"-v\": search values;\n"
1231 " \"-b\": search both names and values (default)",
Kim Phillipsa000b792011-04-05 07:15:14 +00001232 var_complete
1233);
1234#endif
1235
Mike Frysinger722b0612010-10-20 03:52:39 -04001236U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001237 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001238 "set environment variables",
Joe Hershberger24ab5a12012-12-11 22:16:35 -06001239 "[-f] name value ...\n"
1240 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1241 "setenv [-f] name\n"
1242 " - [forcibly] delete environment variable 'name'",
Mike Frysinger722b0612010-10-20 03:52:39 -04001243 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001244);
1245
Jon Loeligerc76fe472007-07-08 18:02:23 -05001246#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001247
wdenk0d498392003-07-01 21:06:45 +00001248U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001249 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001250 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001251 "name [message] [size]\n"
Wolfgang Denk7d855912013-02-20 04:53:16 +00001252 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001253);
Jon Loeliger90253172007-07-10 11:02:44 -05001254#endif
wdenk8bde7f72003-06-27 21:31:46 +00001255
Jon Loeligerc76fe472007-07-08 18:02:23 -05001256#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001257U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001258 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001259 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001260 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001261 " - run the commands in the environment variable(s) 'var'",
1262 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001263);
Jon Loeliger90253172007-07-10 11:02:44 -05001264#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001265#endif /* CONFIG_SPL_BUILD */