blob: f645194bf12c702e9b6a2ca5bd307c90f12bae46 [file] [log] [blame]
wdenka68d3ed2002-10-11 08:38:32 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenka68d3ed2002-10-11 08:38:32 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
Kim Phillipsa000b792011-04-05 07:15:14 +00007 *
8 * Copyright 2011 Freescale Semiconductor, Inc.
9 *
wdenka68d3ed2002-10-11 08:38:32 +000010 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
Wolfgang Denkea882ba2010-06-20 23:33:59 +020029/*
wdenka68d3ed2002-10-11 08:38:32 +000030 * Support for persistent environment data
31 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020032 * The "environment" is stored on external storage as a list of '\0'
33 * terminated "name=value" strings. The end of the list is marked by
34 * a double '\0'. The environment is preceeded by a 32 bit CRC over
35 * the data part and, in case of redundant environment, a byte of
36 * flags.
wdenka68d3ed2002-10-11 08:38:32 +000037 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +020038 * This linearized representation will also be used before
39 * relocation, i. e. as long as we don't have a full C runtime
40 * environment. After that, we use a hash table.
wdenka68d3ed2002-10-11 08:38:32 +000041 */
42
43#include <common.h>
44#include <command.h>
45#include <environment.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020046#include <search.h>
47#include <errno.h>
Peter Tyser246c6922009-10-25 15:12:56 -050048#include <malloc.h>
wdenk2a3cb022002-11-05 21:01:48 +000049#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000050#include <linux/stddef.h>
51#include <asm/byteorder.h>
wdenka68d3ed2002-10-11 08:38:32 +000052
Wolfgang Denkd87080b2006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54
Macpaul Linf3c615b2011-04-26 16:16:45 +000055#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
56 !defined(CONFIG_ENV_IS_IN_FLASH) && \
57 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000058 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin57210c72012-03-12 23:57:50 +000059 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000060 !defined(CONFIG_ENV_IS_IN_NAND) && \
61 !defined(CONFIG_ENV_IS_IN_NVRAM) && \
62 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
63 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
Liu Gang0a85a9e2012-03-08 00:33:20 +000064 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Macpaul Linf3c615b2011-04-26 16:16:45 +000065 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kim75eb82e2009-02-25 11:31:24 +090066# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Marek Vasutb37d41a2012-03-04 15:11:32 +000067SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000068#endif
69
Wolfgang Denkea882ba2010-06-20 23:33:59 +020070/*
71 * Maximum expected input data size for import command
72 */
73#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000074
wdenka68d3ed2002-10-11 08:38:32 +000075/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076 * This variable is incremented on each do_env_set(), so it can
Heiko Schocherda954272009-04-28 08:36:11 +020077 * be used via get_env_id() as an indication, if the environment
78 * has changed or not. So it is possible to reread an environment
79 * variable only if the environment was changed ... done so for
80 * example in NetInitLoop()
81 */
Heiko Schocher2f70c492009-02-10 09:38:52 +010082static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000083
Macpaul Linf3c615b2011-04-26 16:16:45 +000084int get_env_id(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +010085{
86 return env_id;
87}
wdenka68d3ed2002-10-11 08:38:32 +000088
Ilya Yanok7ac2fe22012-09-18 00:22:50 +000089#ifndef CONFIG_SPL_BUILD
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040090/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +020091 * Command interface: print one or all environment variables
92 *
93 * Returns 0 in case of error, or length of printed string
Mike Frysinger4c94f6c2009-05-24 02:26:19 -040094 */
Joe Hershbergerbe112352012-12-11 22:16:23 -060095static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000096{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020097 char *res = NULL;
98 size_t len;
wdenka68d3ed2002-10-11 08:38:32 +000099
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200100 if (name) { /* print a single name */
101 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000102
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200103 e.key = name;
104 e.data = NULL;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600105 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200106 if (ep == NULL)
107 return 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000108 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200109 return len;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400110 }
wdenka68d3ed2002-10-11 08:38:32 +0000111
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200112 /* print whole list */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200114
115 if (len > 0) {
116 puts(res);
117 free(res);
118 return len;
119 }
120
121 /* should never happen */
122 return 0;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400123}
124
Kim Phillips088f1b12012-10-29 13:34:31 +0000125static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
126 char * const argv[])
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400127{
128 int i;
129 int rcode = 0;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600130 int env_flag = H_HIDE_DOT;
131
132 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
133 argc--;
134 argv++;
135 env_flag &= ~H_HIDE_DOT;
136 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400137
138 if (argc == 1) {
139 /* print all env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600140 rcode = env_print(NULL, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200141 if (!rcode)
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400142 return 1;
143 printf("\nEnvironment size: %d/%ld bytes\n",
144 rcode, (ulong)ENV_SIZE);
wdenka68d3ed2002-10-11 08:38:32 +0000145 return 0;
146 }
147
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400148 /* print selected env vars */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600149 env_flag &= ~H_HIDE_DOT;
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400150 for (i = 1; i < argc; ++i) {
Joe Hershbergerbe112352012-12-11 22:16:23 -0600151 int rc = env_print(argv[i], env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200152 if (!rc) {
153 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400154 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000155 }
156 }
Mike Frysinger4c94f6c2009-05-24 02:26:19 -0400157
wdenka68d3ed2002-10-11 08:38:32 +0000158 return rcode;
159}
160
Kim Phillipsa000b792011-04-05 07:15:14 +0000161#ifdef CONFIG_CMD_GREPENV
Igor Grinbergd09b1782011-11-07 01:13:59 +0000162static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
163 int argc, char * const argv[])
Kim Phillipsa000b792011-04-05 07:15:14 +0000164{
165 ENTRY *match;
166 unsigned char matched[env_htab.size / 8];
167 int rcode = 1, arg = 1, idx;
168
169 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000170 return CMD_RET_USAGE;
Kim Phillipsa000b792011-04-05 07:15:14 +0000171
172 memset(matched, 0, env_htab.size / 8);
173
174 while (arg <= argc) {
175 idx = 0;
Kumar Gala068f1582011-11-23 09:48:02 +0000176 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
Kim Phillipsa000b792011-04-05 07:15:14 +0000177 if (!(matched[idx / 8] & (1 << (idx & 7)))) {
178 puts(match->key);
179 puts("=");
180 puts(match->data);
181 puts("\n");
182 }
183 matched[idx / 8] |= 1 << (idx & 7);
184 rcode = 0;
185 }
186 arg++;
187 }
188
189 return rcode;
190}
191#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000192#endif /* CONFIG_SPL_BUILD */
Kim Phillipsa000b792011-04-05 07:15:14 +0000193
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200194/*
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000195 * Set a new environment variable,
196 * or replace or delete an existing one.
Joe Hershberger25980902012-12-11 22:16:31 -0600197 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000198static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000199{
200 int i, len;
201 char *name, *value, *s;
202 ENTRY e, *ep;
203
204 name = argv[1];
205 value = argv[2];
206
207 if (strchr(name, '=')) {
208 printf("## Error: illegal character '='"
209 "in variable name \"%s\"\n", name);
210 return 1;
211 }
212
213 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000214
wdenka68d3ed2002-10-11 08:38:32 +0000215 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000216 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600217 int rc = hdelete_r(name, &env_htab, H_INTERACTIVE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200218 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000219 }
220
221 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200222 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000223 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000224 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000225 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000226
227 value = malloc(len);
228 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200229 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000230 return 1;
231 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000232 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200233 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000234
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200235 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000236 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000237 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000238 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200239 if (s != value)
240 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000241
Igor Grinbergd09b1782011-11-07 01:13:59 +0000242 e.key = name;
243 e.data = value;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600244 hsearch_r(e, ENTER, &ep, &env_htab, H_INTERACTIVE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200245 free(value);
246 if (!ep) {
247 printf("## Error inserting \"%s\" variable, errno=%d\n",
248 name, errno);
249 return 1;
250 }
wdenka68d3ed2002-10-11 08:38:32 +0000251
wdenka68d3ed2002-10-11 08:38:32 +0000252 return 0;
253}
254
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200255int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000256{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200257 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
258
Igor Grinbergd09b1782011-11-07 01:13:59 +0000259 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200260 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200261 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200262 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000263}
264
Simon Glassd67f10c2011-10-24 17:59:59 +0000265/**
266 * Set an environment variable to an integer value
267 *
268 * @param varname Environmet variable to set
269 * @param value Value to set it to
270 * @return 0 if ok, 1 on error
271 */
272int setenv_ulong(const char *varname, ulong value)
273{
274 /* TODO: this should be unsigned */
275 char *str = simple_itoa(value);
276
277 return setenv(varname, str);
278}
279
280/**
281 * Set an environment variable to an address in hex
282 *
283 * @param varname Environmet variable to set
284 * @param addr Value to set it to
285 * @return 0 if ok, 1 on error
286 */
287int setenv_addr(const char *varname, const void *addr)
288{
289 char str[17];
290
Simon Glass79afc882011-11-04 06:42:36 +0000291 sprintf(str, "%lx", (uintptr_t)addr);
Simon Glassd67f10c2011-10-24 17:59:59 +0000292 return setenv(varname, str);
293}
294
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000295#ifndef CONFIG_SPL_BUILD
Kim Phillips088f1b12012-10-29 13:34:31 +0000296static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000297{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200298 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000299 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000300
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200301 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000302}
303
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200304/*
wdenka68d3ed2002-10-11 08:38:32 +0000305 * Prompt for environment variable
306 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500307#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000308int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000309{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200310 char message[CONFIG_SYS_CBSIZE];
311 int size = CONFIG_SYS_CBSIZE - 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200312 int i, len, pos;
wdenka68d3ed2002-10-11 08:38:32 +0000313 char *local_args[4];
314
315 local_args[0] = argv[0];
316 local_args[1] = argv[1];
317 local_args[2] = NULL;
318 local_args[3] = NULL;
319
wdenka68d3ed2002-10-11 08:38:32 +0000320 /* Check the syntax */
321 switch (argc) {
322 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000323 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000324
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200325 case 2: /* env_ask envname */
326 sprintf(message, "Please enter '%s':", argv[1]);
wdenka68d3ed2002-10-11 08:38:32 +0000327 break;
328
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200329 case 3: /* env_ask envname size */
330 sprintf(message, "Please enter '%s':", argv[1]);
331 size = simple_strtoul(argv[2], NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000332 break;
333
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200334 default: /* env_ask envname message1 ... messagen size */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000335 for (i = 2, pos = 0; i < argc - 1; i++) {
336 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000337 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000338
Igor Grinbergd09b1782011-11-07 01:13:59 +0000339 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000340 pos += strlen(argv[i]);
341 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000342
wdenka68d3ed2002-10-11 08:38:32 +0000343 message[pos] = '\0';
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200344 size = simple_strtoul(argv[argc - 1], NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000345 break;
346 }
347
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200348 if (size >= CONFIG_SYS_CBSIZE)
349 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000350
351 if (size <= 0)
352 return 1;
353
354 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200355 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000356
357 if (size < len)
358 console_buffer[size] = '\0';
359
360 len = 2;
361 if (console_buffer[0] != '\0') {
362 local_args[2] = console_buffer;
363 len = 3;
364 }
365
366 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200367 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000368}
Jon Loeliger90253172007-07-10 11:02:44 -0500369#endif
wdenka68d3ed2002-10-11 08:38:32 +0000370
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600371#if defined(CONFIG_CMD_ENV_CALLBACK)
372static int print_static_binding(const char *var_name, const char *callback_name)
373{
374 printf("\t%-20s %-20s\n", var_name, callback_name);
375
376 return 0;
377}
378
379static int print_active_callback(ENTRY *entry)
380{
381 struct env_clbk_tbl *clbkp;
382 int i;
383 int num_callbacks;
384
385 if (entry->callback == NULL)
386 return 0;
387
388 /* look up the callback in the linker-list */
389 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
390 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
391 i < num_callbacks;
392 i++, clbkp++) {
393#if defined(CONFIG_NEEDS_MANUAL_RELOC)
394 if (entry->callback == clbkp->callback + gd->reloc_off)
395#else
396 if (entry->callback == clbkp->callback)
397#endif
398 break;
399 }
400
401 if (i == num_callbacks)
402 /* this should probably never happen, but just in case... */
403 printf("\t%-20s %p\n", entry->key, entry->callback);
404 else
405 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
406
407 return 0;
408}
409
410/*
411 * Print the callbacks available and what they are bound to
412 */
413int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
414{
415 struct env_clbk_tbl *clbkp;
416 int i;
417 int num_callbacks;
418
419 /* Print the available callbacks */
420 puts("Available callbacks:\n");
421 puts("\tCallback Name\n");
422 puts("\t-------------\n");
423 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
424 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
425 i < num_callbacks;
426 i++, clbkp++)
427 printf("\t%s\n", clbkp->name);
428 puts("\n");
429
430 /* Print the static bindings that may exist */
431 puts("Static callback bindings:\n");
432 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
433 printf("\t%-20s %-20s\n", "-------------", "-------------");
434 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
435 puts("\n");
436
437 /* walk through each variable and print the callback if it has one */
438 puts("Active callback bindings:\n");
439 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
440 printf("\t%-20s %-20s\n", "-------------", "-------------");
441 hwalk_r(&env_htab, print_active_callback);
442 return 0;
443}
444#endif
445
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200446/*
Peter Tyser246c6922009-10-25 15:12:56 -0500447 * Interactively edit an environment variable
448 */
449#if defined(CONFIG_CMD_EDITENV)
Kim Phillips088f1b12012-10-29 13:34:31 +0000450static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
451 char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500452{
453 char buffer[CONFIG_SYS_CBSIZE];
454 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500455
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200456 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000457 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500458
459 /* Set read buffer to initial value or empty sting */
460 init_val = getenv(argv[1]);
461 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200462 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500463 else
464 buffer[0] = '\0';
465
Heiko Schocher9c348312012-01-16 21:13:05 +0000466 readline_into_buffer("edit: ", buffer, 0);
Peter Tyser246c6922009-10-25 15:12:56 -0500467
468 return setenv(argv[1], buffer);
469}
470#endif /* CONFIG_CMD_EDITENV */
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000471#endif /* CONFIG_SPL_BUILD */
Peter Tyser246c6922009-10-25 15:12:56 -0500472
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200473/*
wdenka68d3ed2002-10-11 08:38:32 +0000474 * Look up variable from environment,
475 * return address of storage for that variable,
476 * or NULL if not found
477 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200478char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000479{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000480 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200481 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000482
Wolfgang Denk91a76752010-07-24 20:22:02 +0200483 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000484
Igor Grinbergd09b1782011-11-07 01:13:59 +0000485 e.key = name;
486 e.data = NULL;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600487 hsearch_r(e, FIND, &ep, &env_htab, 0);
wdenka68d3ed2002-10-11 08:38:32 +0000488
Macpaul Linf3c615b2011-04-26 16:16:45 +0000489 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000490 }
491
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200492 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200493 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
494 return (char *)(gd->env_buf);
495
496 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000497}
498
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200499/*
500 * Look up variable from environment for restricted C runtime env.
501 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200502int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000503{
504 int i, nxt;
505
Igor Grinbergd09b1782011-11-07 01:13:59 +0000506 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000507 int val, n;
508
Macpaul Linf3c615b2011-04-26 16:16:45 +0000509 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
510 if (nxt >= CONFIG_ENV_SIZE)
511 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000512 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000513
514 val = envmatch((uchar *)name, i);
515 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000516 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200517
wdenka68d3ed2002-10-11 08:38:32 +0000518 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000519 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000520 *buf = env_get_char(val++);
521 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200522 return n;
523 }
524
525 if (n)
526 *--buf = '\0';
527
Wolfgang Denka02a8842011-05-04 10:29:49 +0000528 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
529 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200530
531 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000532 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000533
Macpaul Linf3c615b2011-04-26 16:16:45 +0000534 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000535}
536
Simon Glass4a9b4132011-10-14 13:25:18 +0000537/**
538 * Decode the integer value of an environment variable and return it.
539 *
540 * @param name Name of environemnt variable
541 * @param base Number base to use (normally 10, or 16 for hex)
542 * @param default_val Default value to return if the variable is not
543 * found
544 * @return the decoded value, or default_val if not found
545 */
546ulong getenv_ulong(const char *name, int base, ulong default_val)
547{
548 /*
549 * We can use getenv() here, even before relocation, since the
550 * environment variable value is an integer and thus short.
551 */
552 const char *str = getenv(name);
553
554 return str ? simple_strtoul(str, NULL, base) : default_val;
555}
556
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000557#ifndef CONFIG_SPL_BUILD
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500558#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillips088f1b12012-10-29 13:34:31 +0000559static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
560 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000561{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000562 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000563
Macpaul Linf3c615b2011-04-26 16:16:45 +0000564 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000565}
wdenk8bde7f72003-06-27 21:31:46 +0000566
Mike Frysingerba69dc22008-12-30 02:59:25 -0500567U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200568 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600569 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200570 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500571);
wdenka68d3ed2002-10-11 08:38:32 +0000572#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000573#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000574
575
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200576/*
wdenka68d3ed2002-10-11 08:38:32 +0000577 * Match a name / name=value pair
578 *
579 * s1 is either a simple 'name', or a 'name=value' pair.
580 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000581 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000582 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000583int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000584{
Joe Hershberger586197d2012-10-03 09:38:50 +0000585 if (s1 == NULL)
586 return -1;
587
wdenka68d3ed2002-10-11 08:38:32 +0000588 while (*s1 == env_get_char(i2++))
589 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000590 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000591
wdenka68d3ed2002-10-11 08:38:32 +0000592 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000593 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000594
Macpaul Linf3c615b2011-04-26 16:16:45 +0000595 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000596}
wdenk8bde7f72003-06-27 21:31:46 +0000597
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000598#ifndef CONFIG_SPL_BUILD
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000599static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergd09b1782011-11-07 01:13:59 +0000600 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200601{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000602 int all = 0, flag = 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000603
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000604 debug("Initial value for argc=%d\n", argc);
605 while (--argc > 0 && **++argv == '-') {
606 char *arg = *argv;
607
608 while (*++arg) {
609 switch (*arg) {
610 case 'a': /* default all */
611 all = 1;
612 break;
613 case 'f': /* force */
614 flag |= H_FORCE;
615 break;
616 default:
617 return cmd_usage(cmdtp);
618 }
619 }
620 }
621 debug("Final value for argc=%d\n", argc);
622 if (all && (argc == 0)) {
623 /* Reset the whole environment */
624 set_default_env("## Resetting to default environment\n");
625 return 0;
626 }
627 if (!all && (argc > 0)) {
628 /* Reset individual variables */
629 set_default_vars(argc, argv);
630 return 0;
631 }
632
633 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200634}
wdenk8bde7f72003-06-27 21:31:46 +0000635
Igor Grinbergd09b1782011-11-07 01:13:59 +0000636static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
637 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200638{
639 printf("Not implemented yet\n");
640 return 0;
641}
642
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500643#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200644/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100645 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200646 * -t: export as text format; if size is given, data will be
647 * padded with '\0' bytes; if not, one terminating '\0'
648 * will be added (which is included in the "filesize"
649 * setting so you can for exmple copy this to flash and
650 * keep the termination).
651 * -b: export as binary format (name=value pairs separated by
652 * '\0', list end marked by double "\0\0")
653 * -c: export as checksum protected environment format as
654 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100655 * -s size:
656 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200657 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100658 * var... List of variable names that get included into the
659 * export. Without arguments, the whole environment gets
660 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200661 *
662 * With "-c" and size is NOT given, then the export command will
663 * format the data as currently used for the persistent storage,
664 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
665 * prepend a valid CRC32 checksum and, in case of resundant
666 * environment, a "current" redundancy flag. If size is given, this
667 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
668 * checksum and redundancy flag will be inserted.
669 *
670 * With "-b" and "-t", always only the real data (including a
671 * terminating '\0' byte) will be written; here the optional size
672 * argument will be used to make sure not to overflow the user
673 * provided buffer; the command will abort if the size is not
674 * sufficient. Any remainign space will be '\0' padded.
675 *
676 * On successful return, the variable "filesize" will be set.
677 * Note that filesize includes the trailing/terminating '\0' byte(s).
678 *
679 * Usage szenario: create a text snapshot/backup of the current settings:
680 *
681 * => env export -t 100000
682 * => era ${backup_addr} +${filesize}
683 * => cp.b 100000 ${backup_addr} ${filesize}
684 *
685 * Re-import this snapshot, deleting all other settings:
686 *
687 * => env import -d -t ${backup_addr}
688 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000689static int do_env_export(cmd_tbl_t *cmdtp, int flag,
690 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200691{
692 char buf[32];
693 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100694 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200695 ssize_t len;
696 env_t *envp;
697 char sep = '\n';
698 int chk = 0;
699 int fmt = 0;
700
701 cmd = *argv;
702
703 while (--argc > 0 && **++argv == '-') {
704 char *arg = *argv;
705 while (*++arg) {
706 switch (*arg) {
707 case 'b': /* raw binary format */
708 if (fmt++)
709 goto sep_err;
710 sep = '\0';
711 break;
712 case 'c': /* external checksum format */
713 if (fmt++)
714 goto sep_err;
715 sep = '\0';
716 chk = 1;
717 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100718 case 's': /* size given */
719 if (--argc <= 0)
720 return cmd_usage(cmdtp);
721 size = simple_strtoul(*++argv, NULL, 16);
722 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200723 case 't': /* text format */
724 if (fmt++)
725 goto sep_err;
726 sep = '\n';
727 break;
728 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000729 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200730 }
731 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100732NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200733 }
734
Macpaul Linf3c615b2011-04-26 16:16:45 +0000735 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000736 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200737
738 addr = (char *)simple_strtoul(argv[0], NULL, 16);
739
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100740 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200741 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100742
743 argc--;
744 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200745
746 if (sep) { /* export as text file */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600747 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200748 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000749 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200750 return 1;
751 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100752 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200753 setenv("filesize", buf);
754
755 return 0;
756 }
757
758 envp = (env_t *)addr;
759
760 if (chk) /* export as checksum protected block */
761 res = (char *)envp->data;
762 else /* export as raw binary data */
763 res = addr;
764
Joe Hershbergerbe112352012-12-11 22:16:23 -0600765 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200766 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000767 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200768 return 1;
769 }
770
771 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000772 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200773#ifdef CONFIG_ENV_ADDR_REDUND
774 envp->flags = ACTIVE_FLAG;
775#endif
776 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000777 sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200778 setenv("filesize", buf);
779
780 return 0;
781
782sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000783 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200784 return 1;
785}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500786#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200787
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500788#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200789/*
790 * env import [-d] [-t | -b | -c] addr [size]
791 * -d: delete existing environment before importing;
792 * otherwise overwrite / append to existion definitions
793 * -t: assume text format; either "size" must be given or the
794 * text data must be '\0' terminated
795 * -b: assume binary format ('\0' separated, "\0\0" terminated)
796 * -c: assume checksum protected environment format
797 * addr: memory address to read from
798 * size: length of input data; if missing, proper '\0'
799 * termination is mandatory
800 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000801static int do_env_import(cmd_tbl_t *cmdtp, int flag,
802 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200803{
804 char *cmd, *addr;
805 char sep = '\n';
806 int chk = 0;
807 int fmt = 0;
808 int del = 0;
809 size_t size;
810
811 cmd = *argv;
812
813 while (--argc > 0 && **++argv == '-') {
814 char *arg = *argv;
815 while (*++arg) {
816 switch (*arg) {
817 case 'b': /* raw binary format */
818 if (fmt++)
819 goto sep_err;
820 sep = '\0';
821 break;
822 case 'c': /* external checksum format */
823 if (fmt++)
824 goto sep_err;
825 sep = '\0';
826 chk = 1;
827 break;
828 case 't': /* text format */
829 if (fmt++)
830 goto sep_err;
831 sep = '\n';
832 break;
833 case 'd':
834 del = 1;
835 break;
836 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000837 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200838 }
839 }
840 }
841
Macpaul Linf3c615b2011-04-26 16:16:45 +0000842 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000843 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200844
845 if (!fmt)
846 printf("## Warning: defaulting to text format\n");
847
848 addr = (char *)simple_strtoul(argv[0], NULL, 16);
849
850 if (argc == 2) {
851 size = simple_strtoul(argv[1], NULL, 16);
852 } else {
853 char *s = addr;
854
855 size = 0;
856
857 while (size < MAX_ENV_SIZE) {
858 if ((*s == sep) && (*(s+1) == '\0'))
859 break;
860 ++s;
861 ++size;
862 }
863 if (size == MAX_ENV_SIZE) {
864 printf("## Warning: Input data exceeds %d bytes"
865 " - truncated\n", MAX_ENV_SIZE);
866 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000867 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +0000868 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200869 }
870
871 if (chk) {
872 uint32_t crc;
873 env_t *ep = (env_t *)addr;
874
875 size -= offsetof(env_t, data);
876 memcpy(&crc, &ep->crc, sizeof(crc));
877
878 if (crc32(0, ep->data, size) != crc) {
879 puts("## Error: bad CRC, import failed\n");
880 return 1;
881 }
882 addr = (char *)ep->data;
883 }
884
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000885 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600886 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200887 error("Environment import failed: errno = %d\n", errno);
888 return 1;
889 }
890 gd->flags |= GD_FLG_ENV_READY;
891
892 return 0;
893
894sep_err:
895 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
896 cmd);
897 return 1;
898}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500899#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200900
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200901/*
902 * New command line interface: "env" command with subcommands
903 */
904static cmd_tbl_t cmd_env_sub[] = {
905#if defined(CONFIG_CMD_ASKENV)
906 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
907#endif
908 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
909 U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
910#if defined(CONFIG_CMD_EDITENV)
911 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
912#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600913#if defined(CONFIG_CMD_ENV_CALLBACK)
914 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
915#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500916#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200917 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500918#endif
Kim Phillipsa000b792011-04-05 07:15:14 +0000919#if defined(CONFIG_CMD_GREPENV)
920 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
921#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500922#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200923 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500924#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200925 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
926#if defined(CONFIG_CMD_RUN)
927 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
928#endif
929#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
930 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
931#endif
932 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
933};
934
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200935#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +0200936void env_reloc(void)
937{
938 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
939}
940#endif
941
Macpaul Linf3c615b2011-04-26 16:16:45 +0000942static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200943{
944 cmd_tbl_t *cp;
945
Thomas Weber5904da02010-11-24 13:07:52 +0100946 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000947 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +0100948
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200949 /* drop initial "env" arg */
950 argc--;
951 argv++;
952
953 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
954
955 if (cp)
956 return cp->cmd(cmdtp, flag, argc, argv);
957
Simon Glass4c12eeb2011-12-10 08:44:01 +0000958 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200959}
960
Kim Phillips088f1b12012-10-29 13:34:31 +0000961#ifdef CONFIG_SYS_LONGHELP
962static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200963#if defined(CONFIG_CMD_ASKENV)
964 "ask name [message] [size] - ask for environment variable\nenv "
965#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600966#if defined(CONFIG_CMD_ENV_CALLBACK)
967 "callbacks - print callbacks and their associated variables\nenv "
968#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000969 "default [-f] -a - [forcibly] reset default environment\n"
970 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200971#if defined(CONFIG_CMD_EDITENV)
972 "env edit name - edit environment variable\n"
973#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +0000974#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100975 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +0000976#endif
Kim Phillipsa000b792011-04-05 07:15:14 +0000977#if defined(CONFIG_CMD_GREPENV)
978 "env grep string [...] - search environment\n"
979#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +0000980#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +0000981 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +0000982#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -0600983 "env print [-a | name ...] - print environment\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200984#if defined(CONFIG_CMD_RUN)
985 "env run var [...] - run commands in an environment variable\n"
986#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +0000987#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200988 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +0000989#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000990 "env set [-f] name [arg ...]\n";
991#endif
992
993U_BOOT_CMD(
994 env, CONFIG_SYS_MAXARGS, 1, do_env,
995 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200996);
997
998/*
999 * Old command line interface, kept for compatibility
1000 */
wdenk8bde7f72003-06-27 21:31:46 +00001001
Peter Tyser246c6922009-10-25 15:12:56 -05001002#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001003U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001004 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001005 "edit environment variable",
1006 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001007 " - edit environment variable 'name'",
1008 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001009);
1010#endif
1011
Mike Frysinger722b0612010-10-20 03:52:39 -04001012U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001013 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001014 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001015 "[-a]\n - print [all] values of all environment variables\n"
wdenk8bde7f72003-06-27 21:31:46 +00001016 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001017 " - print value of environment variable 'name'",
1018 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001019);
1020
Kim Phillipsa000b792011-04-05 07:15:14 +00001021#ifdef CONFIG_CMD_GREPENV
1022U_BOOT_CMD_COMPLETE(
1023 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1024 "search environment variables",
1025 "string ...\n"
1026 " - list environment name=value pairs matching 'string'",
1027 var_complete
1028);
1029#endif
1030
Mike Frysinger722b0612010-10-20 03:52:39 -04001031U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001032 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001033 "set environment variables",
wdenk8bde7f72003-06-27 21:31:46 +00001034 "name value ...\n"
1035 " - set environment variable 'name' to 'value ...'\n"
1036 "setenv name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001037 " - delete environment variable 'name'",
1038 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001039);
1040
Jon Loeligerc76fe472007-07-08 18:02:23 -05001041#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001042
wdenk0d498392003-07-01 21:06:45 +00001043U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001044 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001045 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001046 "name [message] [size]\n"
1047 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1048 "askenv name\n"
1049 " - get environment variable 'name' from stdin\n"
1050 "askenv name size\n"
1051 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1052 "askenv name [message] size\n"
1053 " - display 'message' string and get environment variable 'name'"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001054 "from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001055);
Jon Loeliger90253172007-07-10 11:02:44 -05001056#endif
wdenk8bde7f72003-06-27 21:31:46 +00001057
Jon Loeligerc76fe472007-07-08 18:02:23 -05001058#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001059U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001060 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001061 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001062 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001063 " - run the commands in the environment variable(s) 'var'",
1064 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001065);
Jon Loeliger90253172007-07-10 11:02:44 -05001066#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001067#endif /* CONFIG_SPL_BUILD */