blob: 5bcc324675eb7535c9e103cd473134122b83e376 [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 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenka68d3ed2002-10-11 08:38:32 +000011 */
12
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020013/*
wdenka68d3ed2002-10-11 08:38:32 +000014 * Support for persistent environment data
15 *
Wolfgang Denk460a9ff2010-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 Denk460a9ff2010-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 Denk460a9ff2010-06-20 23:33:59 +020030#include <search.h>
31#include <errno.h>
Peter Tyser0deafa22009-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 Denk6405a152006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
Macpaul Linff2a9f32011-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 Linff2a9f32011-04-26 16:16:45 +000042 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin9765c082012-03-12 23:57:50 +000043 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linff2a9f32011-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 Gang85bcd732012-03-08 00:33:20 +000048 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Joe Hershberger0c5faa82013-04-08 10:32:51 +000049 !defined(CONFIG_ENV_IS_IN_UBI) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000050 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kimf0b1bdd2009-02-25 11:31:24 +090051# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Joe Hershberger0c5faa82013-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 Denk460a9ff2010-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 Denk460a9ff2010-06-20 23:33:59 +020061 * This variable is incremented on each do_env_set(), so it can
Heiko Schocher3b195ff2009-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 Schocher0c303fa2009-02-10 09:38:52 +010067static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000068
Macpaul Linff2a9f32011-04-26 16:16:45 +000069int get_env_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010070{
71 return env_id;
72}
wdenka68d3ed2002-10-11 08:38:32 +000073
Ilya Yanokf7a2c552012-09-18 00:22:50 +000074#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040075/*
Wolfgang Denk460a9ff2010-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 Frysinger87944922009-05-24 02:26:19 -040079 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060080static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000081{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020082 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000083 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +000084
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020085 if (name) { /* print a single name */
86 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +000087
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088 e.key = name;
89 e.data = NULL;
Joe Hershberger79a905e2012-12-11 22:16:23 -060090 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020091 if (ep == NULL)
92 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +000093 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020094 return len;
95 }
wdenka68d3ed2002-10-11 08:38:32 +000096
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020097 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -060098 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +000099
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200100 if (len > 0) {
101 puts(res);
102 free(res);
103 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000104 }
105
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200106 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000107 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200108 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400109}
wdenka68d3ed2002-10-11 08:38:32 +0000110
Kim Phillipsdc00a682012-10-29 13:34:31 +0000111static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
112 char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400113{
114 int i;
115 int rcode = 0;
Joe Hershberger79a905e2012-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 }
wdenka68d3ed2002-10-11 08:38:32 +0000123
Mike Frysinger87944922009-05-24 02:26:19 -0400124 if (argc == 1) {
125 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600126 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200127 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400128 return 1;
129 printf("\nEnvironment size: %d/%ld bytes\n",
130 rcode, (ulong)ENV_SIZE);
131 return 0;
132 }
wdenka68d3ed2002-10-11 08:38:32 +0000133
Mike Frysinger87944922009-05-24 02:26:19 -0400134 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600135 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400136 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600137 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200138 if (!rc) {
139 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400140 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000141 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000142 }
143
144 return rcode;
145}
146
147#ifdef CONFIG_CMD_GREPENV
Igor Grinbergde832562011-11-07 01:13:59 +0000148static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
149 int argc, char * const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000150{
Wolfgang Denke27fc122013-03-23 23:50:29 +0000151 char *res = NULL;
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000152 int len, grep_how, grep_what;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000153
154 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000155 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000156
Wolfgang Denkac9c3012013-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 Denkb917ba42013-03-23 23:50:30 +0000159
Pierre Aubertd8d51f82013-10-08 14:20:27 +0200160 while (--argc > 0 && **++argv == '-') {
161 char *arg = *argv;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000162 while (*++arg) {
163 switch (*arg) {
Wolfgang Denkac9c3012013-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 Denkb917ba42013-03-23 23:50:30 +0000169 case 'n': /* grep for name */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000170 grep_what = H_MATCH_KEY;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000171 break;
172 case 'v': /* grep for value */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000173 grep_what = H_MATCH_DATA;
Wolfgang Denkb917ba42013-03-23 23:50:30 +0000174 break;
175 case 'b': /* grep for both */
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000176 grep_what = H_MATCH_BOTH;
Wolfgang Denkb917ba42013-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 Denke27fc122013-03-23 23:50:29 +0000187 len = hexport_r(&env_htab, '\n',
Wolfgang Denkac9c3012013-03-23 23:50:32 +0000188 flag | grep_what | grep_how,
Wolfgang Denke27fc122013-03-23 23:50:29 +0000189 &res, 0, argc, argv);
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000190
Wolfgang Denke27fc122013-03-23 23:50:29 +0000191 if (len > 0) {
192 puts(res);
193 free(res);
wdenka68d3ed2002-10-11 08:38:32 +0000194 }
Mike Frysinger87944922009-05-24 02:26:19 -0400195
Wolfgang Denke27fc122013-03-23 23:50:29 +0000196 if (len < 2)
197 return 1;
198
199 return 0;
wdenka68d3ed2002-10-11 08:38:32 +0000200}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000201#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000202#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000203
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200204/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000205 * Set a new environment variable,
206 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600207 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000208static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautodc88d592012-08-24 00:11:37 +0000209{
210 int i, len;
211 char *name, *value, *s;
212 ENTRY e, *ep;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600213 int env_flag = H_INTERACTIVE;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000214
Joe Hershbergerf64bb772012-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 Falautodc88d592012-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++;
wdenka68d3ed2002-10-11 08:38:32 +0000241
wdenka68d3ed2002-10-11 08:38:32 +0000242 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000243 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600244 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200245 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000246 }
247
248 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200249 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000250 */
Macpaul Linff2a9f32011-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 Linff2a9f32011-04-26 16:16:45 +0000253
254 value = malloc(len);
255 if (value == NULL) {
Wolfgang Denk460a9ff2010-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 Linff2a9f32011-04-26 16:16:45 +0000259 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200260 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000261
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200262 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000263 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000264 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000265 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200266 if (s != value)
267 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000268
Igor Grinbergde832562011-11-07 01:13:59 +0000269 e.key = name;
270 e.data = value;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600271 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-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 Denkd67abff2011-07-29 14:42:18 +0200282int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000283{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200284 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
285
Joe Hershberger3902c602013-04-08 10:32:50 +0000286 /* before import into hashtable */
287 if (!(gd->flags & GD_FLG_ENV_READY))
288 return 1;
289
Igor Grinbergde832562011-11-07 01:13:59 +0000290 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200291 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200292 else
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200293 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000294}
295
Simon Glassb8e926e2011-10-24 17:59:59 +0000296/**
297 * Set an environment variable to an integer value
298 *
Simon Glassdf372d62013-05-07 06:11:45 +0000299 * @param varname Environment variable to set
Simon Glassb8e926e2011-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 Glass044dc9e2013-02-24 17:33:21 +0000312 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000313 *
Simon Glassdf372d62013-05-07 06:11:45 +0000314 * @param varname Environment variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000315 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000316 * @return 0 if ok, 1 on error
317 */
Simon Glass044dc9e2013-02-24 17:33:21 +0000318int setenv_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000319{
320 char str[17];
321
Simon Glass044dc9e2013-02-24 17:33:21 +0000322 sprintf(str, "%lx", value);
Simon Glassb8e926e2011-10-24 17:59:59 +0000323 return setenv(varname, str);
324}
325
Simon Glassabbd5122013-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 Yanokf7a2c552012-09-18 00:22:50 +0000341#ifndef CONFIG_SPL_BUILD
Kim Phillipsdc00a682012-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 Denk3b683112010-07-17 01:06:04 +0200344 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000345 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000346
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200347 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000348}
349
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200350/*
wdenka68d3ed2002-10-11 08:38:32 +0000351 * Prompt for environment variable
352 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500353#if defined(CONFIG_CMD_ASKENV)
Macpaul Linff2a9f32011-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-VILLARD03836942008-10-16 15:01:15 +0200356 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000357 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000358 char *local_args[4];
Wolfgang Denk586895d2013-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 Denk586895d2013-02-20 04:53:16 +0000366 /*
367 * Check the syntax:
368 *
369 * env_ask envname [message1 ...] [size]
370 */
371 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000372 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000373
Wolfgang Denk586895d2013-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 Denk586895d2013-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 Linff2a9f32011-04-26 16:16:45 +0000393 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000394 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000395
Igor Grinbergde832562011-11-07 01:13:59 +0000396 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000397 pos += strlen(argv[i]);
398 }
Wolfgang Denk586895d2013-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-VILLARD03836942008-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 Denk460a9ff2010-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 Denk460a9ff2010-06-20 23:33:59 +0200422 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000423}
Jon Loeligerd704d912007-07-10 11:02:44 -0500424#endif
wdenka68d3ed2002-10-11 08:38:32 +0000425
Joe Hershberger1b0d5512012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-12-11 22:16:34 -0600505 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600506
Joe Hershberger6fe26c92012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-12-11 22:16:34 -0600517 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-12-11 22:16:33 -0600553 /* Print the static flags that may exist */
554 puts("Static flags:\n");
Joe Hershberger6fe26c92012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-12-11 22:16:33 -0600568 hwalk_r(&env_htab, print_active_flags);
569 return 0;
570}
571#endif
572
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200573/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500574 * Interactively edit an environment variable
575 */
576#if defined(CONFIG_CMD_EDITENV)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000577static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
578 char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500579{
580 char buffer[CONFIG_SYS_CBSIZE];
581 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500582
Wolfgang Denk3b683112010-07-17 01:06:04 +0200583 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000584 return CMD_RET_USAGE;
Peter Tyser0deafa22009-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 Vasut305e02a2011-09-26 02:26:03 +0200589 sprintf(buffer, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500590 else
591 buffer[0] = '\0';
592
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000593 if (readline_into_buffer("edit: ", buffer, 0) < 0)
594 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500595
596 return setenv(argv[1], buffer);
597}
598#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000599#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500600
Wolfgang Denk460a9ff2010-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 Denkd67abff2011-07-29 14:42:18 +0200606char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000607{
Igor Grinbergde832562011-11-07 01:13:59 +0000608 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200609 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000610
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200611 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000612
Igor Grinbergde832562011-11-07 01:13:59 +0000613 e.key = name;
614 e.data = NULL;
Joe Hershbergera46f7702012-12-11 22:16:19 -0600615 hsearch_r(e, FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200616
Macpaul Linff2a9f32011-04-26 16:16:45 +0000617 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000618 }
619
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200620 /* restricted capabilities before import */
Wolfgang Denk460a9ff2010-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 Denk460a9ff2010-06-20 23:33:59 +0200627/*
628 * Look up variable from environment for restricted C runtime env.
629 */
Wolfgang Denkd67abff2011-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 Grinbergde832562011-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 Linff2a9f32011-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 Linff2a9f32011-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 Denk7a060a72010-07-24 22:16:20 +0200645
wdenka68d3ed2002-10-11 08:38:32 +0000646 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000647 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergde832562011-11-07 01:13:59 +0000648 *buf = env_get_char(val++);
649 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200650 return n;
651 }
652
653 if (n)
654 *--buf = '\0';
655
Wolfgang Denk43285472011-05-04 10:29:49 +0000656 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
657 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200658
659 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000660 }
Igor Grinbergde832562011-11-07 01:13:59 +0000661
Macpaul Linff2a9f32011-04-26 16:16:45 +0000662 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000663}
664
Simon Glassbf690f82011-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 Yanokf7a2c552012-09-18 00:22:50 +0000685#ifndef CONFIG_SPL_BUILD
Mike Frysinger78dcaf42009-01-28 19:08:14 -0500686#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillipsdc00a682012-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 Linff2a9f32011-04-26 16:16:45 +0000690 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000691
Macpaul Linff2a9f32011-04-26 16:16:45 +0000692 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000693}
wdenk57b2d802003-06-27 21:31:46 +0000694
Mike Frysinger61583932008-12-30 02:59:25 -0500695U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200696 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600697 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200698 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500699);
wdenka68d3ed2002-10-11 08:38:32 +0000700#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000701#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000702
703
Wolfgang Denk460a9ff2010-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 Grinbergde832562011-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 Linff2a9f32011-04-26 16:16:45 +0000711int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000712{
Joe Hershbergerdd000dd2012-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 Linff2a9f32011-04-26 16:16:45 +0000718 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000719
wdenka68d3ed2002-10-11 08:38:32 +0000720 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000721 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000722
Macpaul Linff2a9f32011-04-26 16:16:45 +0000723 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000724}
wdenk57b2d802003-06-27 21:31:46 +0000725
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000726#ifndef CONFIG_SPL_BUILD
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000727static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergde832562011-11-07 01:13:59 +0000728 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200729{
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000730 int all = 0, flag = 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000731
Gerlando Falautofe9f49d2012-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 Denk460a9ff2010-06-20 23:33:59 +0200762}
763
Igor Grinbergde832562011-11-07 01:13:59 +0000764static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
765 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200766{
Joe Hershberger8335ce72012-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 Denk460a9ff2010-06-20 23:33:59 +0200797}
798
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500799#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200800/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100801 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-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 Denk1e3cdf32011-11-06 22:49:44 +0100811 * -s size:
812 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200813 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-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 Denk460a9ff2010-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 Grinbergde832562011-11-07 01:13:59 +0000845static int do_env_export(cmd_tbl_t *cmdtp, int flag,
846 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200847{
848 char buf[32];
849 char *addr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100850 size_t size = 0;
Wolfgang Denk460a9ff2010-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 Denk1e3cdf32011-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 Denk460a9ff2010-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 Glassa06dfc72011-12-10 08:44:01 +0000885 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200886 }
887 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100888NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200889 }
890
Macpaul Linff2a9f32011-04-26 16:16:45 +0000891 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000892 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000893
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200894 addr = (char *)simple_strtoul(argv[0], NULL, 16);
895
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100896 if (size)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200897 memset(addr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100898
899 argc--;
900 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200901
902 if (sep) { /* export as text file */
Wolfgang Denk565af272013-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 Denk460a9ff2010-06-20 23:33:59 +0200906 if (len < 0) {
Igor Grinbergde832562011-11-07 01:13:59 +0000907 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200908 return 1;
909 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100910 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denk460a9ff2010-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 Denk565af272013-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 Denk460a9ff2010-06-20 23:33:59 +0200926 if (len < 0) {
Igor Grinbergde832562011-11-07 01:13:59 +0000927 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200928 return 1;
929 }
930
931 if (chk) {
Igor Grinbergde832562011-11-07 01:13:59 +0000932 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200933#ifdef CONFIG_ENV_ADDR_REDUND
934 envp->flags = ACTIVE_FLAG;
935#endif
936 }
Simon Glass103789d2013-02-24 17:33:22 +0000937 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200938
939 return 0;
940
941sep_err:
Igor Grinbergde832562011-11-07 01:13:59 +0000942 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200943 return 1;
944}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500945#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200946
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500947#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-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 Grinbergde832562011-11-07 01:13:59 +0000960static int do_env_import(cmd_tbl_t *cmdtp, int flag,
961 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-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 Glassa06dfc72011-12-10 08:44:01 +0000996 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200997 }
998 }
999 }
1000
Macpaul Linff2a9f32011-04-26 16:16:45 +00001001 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +00001002 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-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);
1011 } else {
1012 char *s = addr;
1013
1014 size = 0;
1015
1016 while (size < MAX_ENV_SIZE) {
1017 if ((*s == sep) && (*(s+1) == '\0'))
1018 break;
1019 ++s;
1020 ++size;
1021 }
1022 if (size == MAX_ENV_SIZE) {
1023 printf("## Warning: Input data exceeds %d bytes"
1024 " - truncated\n", MAX_ENV_SIZE);
1025 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001026 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001027 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001028 }
1029
1030 if (chk) {
1031 uint32_t crc;
1032 env_t *ep = (env_t *)addr;
1033
1034 size -= offsetof(env_t, data);
1035 memcpy(&crc, &ep->crc, sizeof(crc));
1036
1037 if (crc32(0, ep->data, size) != crc) {
1038 puts("## Error: bad CRC, import failed\n");
1039 return 1;
1040 }
1041 addr = (char *)ep->data;
1042 }
1043
Gerlando Falauto9b0e1ff2012-08-24 00:11:38 +00001044 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergera46f7702012-12-11 22:16:19 -06001045 0, NULL) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001046 error("Environment import failed: errno = %d\n", errno);
1047 return 1;
1048 }
1049 gd->flags |= GD_FLG_ENV_READY;
1050
1051 return 0;
1052
1053sep_err:
1054 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1055 cmd);
1056 return 1;
1057}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001058#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001059
Andrew Ruder94463402013-10-22 19:07:34 -05001060#if defined(CONFIG_CMD_ENV_EXISTS)
1061static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
1062 char * const argv[])
1063{
1064 ENTRY e, *ep;
1065
1066 if (argc < 2)
1067 return CMD_RET_USAGE;
1068
1069 e.key = argv[1];
1070 e.data = NULL;
1071 hsearch_r(e, FIND, &ep, &env_htab, 0);
1072
1073 return (ep == NULL) ? 1 : 0;
1074}
1075#endif
1076
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001077/*
1078 * New command line interface: "env" command with subcommands
1079 */
1080static cmd_tbl_t cmd_env_sub[] = {
1081#if defined(CONFIG_CMD_ASKENV)
1082 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1083#endif
1084 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001085 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001086#if defined(CONFIG_CMD_EDITENV)
1087 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1088#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001089#if defined(CONFIG_CMD_ENV_CALLBACK)
1090 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1091#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001092#if defined(CONFIG_CMD_ENV_FLAGS)
1093 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1094#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001095#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001096 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001097#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001098#if defined(CONFIG_CMD_GREPENV)
1099 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1100#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001101#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001102 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001103#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001104 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1105#if defined(CONFIG_CMD_RUN)
1106 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1107#endif
1108#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1109 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1110#endif
1111 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
Andrew Ruder94463402013-10-22 19:07:34 -05001112#if defined(CONFIG_CMD_ENV_EXISTS)
1113 U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
1114#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001115};
1116
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001117#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001118void env_reloc(void)
1119{
1120 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1121}
1122#endif
1123
Macpaul Linff2a9f32011-04-26 16:16:45 +00001124static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001125{
1126 cmd_tbl_t *cp;
1127
Thomas Weber4d282652010-11-24 13:07:52 +01001128 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001129 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001130
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001131 /* drop initial "env" arg */
1132 argc--;
1133 argv++;
1134
1135 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1136
1137 if (cp)
1138 return cp->cmd(cmdtp, flag, argc, argv);
1139
Simon Glassa06dfc72011-12-10 08:44:01 +00001140 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001141}
1142
Kim Phillipsdc00a682012-10-29 13:34:31 +00001143#ifdef CONFIG_SYS_LONGHELP
1144static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001145#if defined(CONFIG_CMD_ASKENV)
1146 "ask name [message] [size] - ask for environment variable\nenv "
1147#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001148#if defined(CONFIG_CMD_ENV_CALLBACK)
1149 "callbacks - print callbacks and their associated variables\nenv "
1150#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001151 "default [-f] -a - [forcibly] reset default environment\n"
1152 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001153 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001154#if defined(CONFIG_CMD_EDITENV)
1155 "env edit name - edit environment variable\n"
1156#endif
Andrew Ruder94463402013-10-22 19:07:34 -05001157#if defined(CONFIG_CMD_ENV_EXISTS)
1158 "env exists name - tests for existence of variable\n"
1159#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001160#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001161 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001162#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001163#if defined(CONFIG_CMD_ENV_FLAGS)
1164 "env flags - print variables that have non-default flags\n"
1165#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001166#if defined(CONFIG_CMD_GREPENV)
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001167#ifdef CONFIG_REGEX
1168 "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
1169#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001170 "env grep [-n | -v | -b] string [...] - search environment\n"
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001171#endif
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001172#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001173#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001174 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001175#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001176 "env print [-a | name ...] - print environment\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001177#if defined(CONFIG_CMD_RUN)
1178 "env run var [...] - run commands in an environment variable\n"
1179#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001180#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001181 "env save - save environment\n"
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001182#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001183 "env set [-f] name [arg ...]\n";
1184#endif
1185
1186U_BOOT_CMD(
1187 env, CONFIG_SYS_MAXARGS, 1, do_env,
1188 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001189);
1190
1191/*
1192 * Old command line interface, kept for compatibility
1193 */
wdenk57b2d802003-06-27 21:31:46 +00001194
Peter Tyser0deafa22009-10-25 15:12:56 -05001195#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001196U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001197 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001198 "edit environment variable",
1199 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001200 " - edit environment variable 'name'",
1201 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001202);
1203#endif
1204
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001205U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001206 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001207 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001208 "[-a]\n - print [all] values of all environment variables\n"
wdenk57b2d802003-06-27 21:31:46 +00001209 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001210 " - print value of environment variable 'name'",
1211 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001212);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001213
1214#ifdef CONFIG_CMD_GREPENV
1215U_BOOT_CMD_COMPLETE(
1216 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1217 "search environment variables",
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001218#ifdef CONFIG_REGEX
1219 "[-e] [-n | -v | -b] string ...\n"
1220#else
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001221 "[-n | -v | -b] string ...\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001222#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001223 " - list environment name=value pairs matching 'string'\n"
Wolfgang Denkac9c3012013-03-23 23:50:32 +00001224#ifdef CONFIG_REGEX
1225 " \"-e\": enable regular expressions;\n"
1226#endif
Wolfgang Denkb917ba42013-03-23 23:50:30 +00001227 " \"-n\": search variable names; \"-v\": search values;\n"
1228 " \"-b\": search both names and values (default)",
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001229 var_complete
1230);
1231#endif
wdenk57b2d802003-06-27 21:31:46 +00001232
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001233U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001234 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001235 "set environment variables",
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001236 "[-f] name value ...\n"
1237 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1238 "setenv [-f] name\n"
1239 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001240 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001241);
1242
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001243#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001244
wdenkf287a242003-07-01 21:06:45 +00001245U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001246 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001247 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001248 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001249 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001250);
Jon Loeligerd704d912007-07-10 11:02:44 -05001251#endif
wdenk57b2d802003-06-27 21:31:46 +00001252
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001253#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001254U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001255 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001256 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001257 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001258 " - run the commands in the environment variable(s) 'var'",
1259 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001260);
Jon Loeligerd704d912007-07-10 11:02:44 -05001261#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001262#endif /* CONFIG_SPL_BUILD */