blob: 988d6b3c705d85bbc0906b3091f649e09c8686b3 [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;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600203 int env_flag = H_INTERACTIVE;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000204
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600205 debug("Initial value for argc=%d\n", argc);
206 while (argc > 1 && **(argv + 1) == '-') {
207 char *arg = *++argv;
208
209 --argc;
210 while (*++arg) {
211 switch (*arg) {
212 case 'f': /* force */
213 env_flag |= H_FORCE;
214 break;
215 default:
216 return CMD_RET_USAGE;
217 }
218 }
219 }
220 debug("Final value for argc=%d\n", argc);
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000221 name = argv[1];
222 value = argv[2];
223
224 if (strchr(name, '=')) {
225 printf("## Error: illegal character '='"
226 "in variable name \"%s\"\n", name);
227 return 1;
228 }
229
230 env_id++;
Gerlando Falautoc3f65252012-08-24 00:11:37 +0000231
wdenka68d3ed2002-10-11 08:38:32 +0000232 /* Delete only ? */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000233 if (argc < 3 || argv[2] == NULL) {
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600234 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200235 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000236 }
237
238 /*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200239 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000240 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000241 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000242 len += strlen(argv[i]) + 1;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000243
244 value = malloc(len);
245 if (value == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200246 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000247 return 1;
248 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000249 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200250 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000251
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000253 ;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000254 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000255 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256 if (s != value)
257 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000258
Igor Grinbergd09b1782011-11-07 01:13:59 +0000259 e.key = name;
260 e.data = value;
Joe Hershberger24ab5a12012-12-11 22:16:35 -0600261 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200262 free(value);
263 if (!ep) {
264 printf("## Error inserting \"%s\" variable, errno=%d\n",
265 name, errno);
266 return 1;
267 }
wdenka68d3ed2002-10-11 08:38:32 +0000268
wdenka68d3ed2002-10-11 08:38:32 +0000269 return 0;
270}
271
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200272int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000273{
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200274 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
275
Igor Grinbergd09b1782011-11-07 01:13:59 +0000276 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200277 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann9ffd4512007-04-23 14:00:11 +0200278 else
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200279 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000280}
281
Simon Glassd67f10c2011-10-24 17:59:59 +0000282/**
283 * Set an environment variable to an integer value
284 *
285 * @param varname Environmet variable to set
286 * @param value Value to set it to
287 * @return 0 if ok, 1 on error
288 */
289int setenv_ulong(const char *varname, ulong value)
290{
291 /* TODO: this should be unsigned */
292 char *str = simple_itoa(value);
293
294 return setenv(varname, str);
295}
296
297/**
298 * Set an environment variable to an address in hex
299 *
300 * @param varname Environmet variable to set
301 * @param addr Value to set it to
302 * @return 0 if ok, 1 on error
303 */
304int setenv_addr(const char *varname, const void *addr)
305{
306 char str[17];
307
Simon Glass79afc882011-11-04 06:42:36 +0000308 sprintf(str, "%lx", (uintptr_t)addr);
Simon Glassd67f10c2011-10-24 17:59:59 +0000309 return setenv(varname, str);
310}
311
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000312#ifndef CONFIG_SPL_BUILD
Kim Phillips088f1b12012-10-29 13:34:31 +0000313static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000314{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200315 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000316 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000317
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200318 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000319}
320
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200321/*
wdenka68d3ed2002-10-11 08:38:32 +0000322 * Prompt for environment variable
323 */
Jon Loeligerc76fe472007-07-08 18:02:23 -0500324#if defined(CONFIG_CMD_ASKENV)
Macpaul Linf3c615b2011-04-26 16:16:45 +0000325int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000326{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200327 char message[CONFIG_SYS_CBSIZE];
328 int size = CONFIG_SYS_CBSIZE - 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200329 int i, len, pos;
wdenka68d3ed2002-10-11 08:38:32 +0000330 char *local_args[4];
331
332 local_args[0] = argv[0];
333 local_args[1] = argv[1];
334 local_args[2] = NULL;
335 local_args[3] = NULL;
336
wdenka68d3ed2002-10-11 08:38:32 +0000337 /* Check the syntax */
338 switch (argc) {
339 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000340 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000341
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200342 case 2: /* env_ask envname */
343 sprintf(message, "Please enter '%s':", argv[1]);
wdenka68d3ed2002-10-11 08:38:32 +0000344 break;
345
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200346 case 3: /* env_ask envname size */
347 sprintf(message, "Please enter '%s':", argv[1]);
348 size = simple_strtoul(argv[2], NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000349 break;
350
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200351 default: /* env_ask envname message1 ... messagen size */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000352 for (i = 2, pos = 0; i < argc - 1; i++) {
353 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000354 message[pos++] = ' ';
Macpaul Linf3c615b2011-04-26 16:16:45 +0000355
Igor Grinbergd09b1782011-11-07 01:13:59 +0000356 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000357 pos += strlen(argv[i]);
358 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000359
wdenka68d3ed2002-10-11 08:38:32 +0000360 message[pos] = '\0';
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200361 size = simple_strtoul(argv[argc - 1], NULL, 10);
wdenka68d3ed2002-10-11 08:38:32 +0000362 break;
363 }
364
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200365 if (size >= CONFIG_SYS_CBSIZE)
366 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000367
368 if (size <= 0)
369 return 1;
370
371 /* prompt for input */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200372 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000373
374 if (size < len)
375 console_buffer[size] = '\0';
376
377 len = 2;
378 if (console_buffer[0] != '\0') {
379 local_args[2] = console_buffer;
380 len = 3;
381 }
382
383 /* Continue calling setenv code */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200384 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000385}
Jon Loeliger90253172007-07-10 11:02:44 -0500386#endif
wdenka68d3ed2002-10-11 08:38:32 +0000387
Joe Hershberger5e2b3e02012-12-11 22:16:25 -0600388#if defined(CONFIG_CMD_ENV_CALLBACK)
389static int print_static_binding(const char *var_name, const char *callback_name)
390{
391 printf("\t%-20s %-20s\n", var_name, callback_name);
392
393 return 0;
394}
395
396static int print_active_callback(ENTRY *entry)
397{
398 struct env_clbk_tbl *clbkp;
399 int i;
400 int num_callbacks;
401
402 if (entry->callback == NULL)
403 return 0;
404
405 /* look up the callback in the linker-list */
406 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
407 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
408 i < num_callbacks;
409 i++, clbkp++) {
410#if defined(CONFIG_NEEDS_MANUAL_RELOC)
411 if (entry->callback == clbkp->callback + gd->reloc_off)
412#else
413 if (entry->callback == clbkp->callback)
414#endif
415 break;
416 }
417
418 if (i == num_callbacks)
419 /* this should probably never happen, but just in case... */
420 printf("\t%-20s %p\n", entry->key, entry->callback);
421 else
422 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
423
424 return 0;
425}
426
427/*
428 * Print the callbacks available and what they are bound to
429 */
430int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
431{
432 struct env_clbk_tbl *clbkp;
433 int i;
434 int num_callbacks;
435
436 /* Print the available callbacks */
437 puts("Available callbacks:\n");
438 puts("\tCallback Name\n");
439 puts("\t-------------\n");
440 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
441 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
442 i < num_callbacks;
443 i++, clbkp++)
444 printf("\t%s\n", clbkp->name);
445 puts("\n");
446
447 /* Print the static bindings that may exist */
448 puts("Static callback bindings:\n");
449 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
450 printf("\t%-20s %-20s\n", "-------------", "-------------");
451 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
452 puts("\n");
453
454 /* walk through each variable and print the callback if it has one */
455 puts("Active callback bindings:\n");
456 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
457 printf("\t%-20s %-20s\n", "-------------", "-------------");
458 hwalk_r(&env_htab, print_active_callback);
459 return 0;
460}
461#endif
462
Joe Hershbergerfffad712012-12-11 22:16:33 -0600463#if defined(CONFIG_CMD_ENV_FLAGS)
464static int print_static_flags(const char *var_name, const char *flags)
465{
466 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600467 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergerfffad712012-12-11 22:16:33 -0600468
Joe Hershberger267541f2012-12-11 22:16:34 -0600469 printf("\t%-20s %-20s %-20s\n", var_name,
470 env_flags_get_vartype_name(type),
471 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600472
473 return 0;
474}
475
476static int print_active_flags(ENTRY *entry)
477{
478 enum env_flags_vartype type;
Joe Hershberger267541f2012-12-11 22:16:34 -0600479 enum env_flags_varaccess access;
Joe Hershbergerfffad712012-12-11 22:16:33 -0600480
481 if (entry->flags == 0)
482 return 0;
483
484 type = (enum env_flags_vartype)
485 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger267541f2012-12-11 22:16:34 -0600486 access = env_flags_parse_varaccess_from_binflags(entry->flags);
487 printf("\t%-20s %-20s %-20s\n", entry->key,
488 env_flags_get_vartype_name(type),
489 env_flags_get_varaccess_name(access));
Joe Hershbergerfffad712012-12-11 22:16:33 -0600490
491 return 0;
492}
493
494/*
495 * Print the flags available and what variables have flags
496 */
497int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
498{
499 /* Print the available variable types */
500 printf("Available variable type flags (position %d):\n",
501 ENV_FLAGS_VARTYPE_LOC);
502 puts("\tFlag\tVariable Type Name\n");
503 puts("\t----\t------------------\n");
504 env_flags_print_vartypes();
505 puts("\n");
506
Joe Hershberger267541f2012-12-11 22:16:34 -0600507 /* Print the available variable access types */
508 printf("Available variable access flags (position %d):\n",
509 ENV_FLAGS_VARACCESS_LOC);
510 puts("\tFlag\tVariable Access Name\n");
511 puts("\t----\t--------------------\n");
512 env_flags_print_varaccess();
513 puts("\n");
514
Joe Hershbergerfffad712012-12-11 22:16:33 -0600515 /* Print the static flags that may exist */
516 puts("Static flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600517 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
518 "Variable Access");
519 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
520 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600521 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
522 puts("\n");
523
524 /* walk through each variable and print the flags if non-default */
525 puts("Active flags:\n");
Joe Hershberger267541f2012-12-11 22:16:34 -0600526 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
527 "Variable Access");
528 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
529 "---------------");
Joe Hershbergerfffad712012-12-11 22:16:33 -0600530 hwalk_r(&env_htab, print_active_flags);
531 return 0;
532}
533#endif
534
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200535/*
Peter Tyser246c6922009-10-25 15:12:56 -0500536 * Interactively edit an environment variable
537 */
538#if defined(CONFIG_CMD_EDITENV)
Kim Phillips088f1b12012-10-29 13:34:31 +0000539static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
540 char * const argv[])
Peter Tyser246c6922009-10-25 15:12:56 -0500541{
542 char buffer[CONFIG_SYS_CBSIZE];
543 char *init_val;
Peter Tyser246c6922009-10-25 15:12:56 -0500544
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200545 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000546 return CMD_RET_USAGE;
Peter Tyser246c6922009-10-25 15:12:56 -0500547
548 /* Set read buffer to initial value or empty sting */
549 init_val = getenv(argv[1]);
550 if (init_val)
Marek Vasut7fcd9bb2011-09-26 02:26:03 +0200551 sprintf(buffer, "%s", init_val);
Peter Tyser246c6922009-10-25 15:12:56 -0500552 else
553 buffer[0] = '\0';
554
Heiko Schocher9c348312012-01-16 21:13:05 +0000555 readline_into_buffer("edit: ", buffer, 0);
Peter Tyser246c6922009-10-25 15:12:56 -0500556
557 return setenv(argv[1], buffer);
558}
559#endif /* CONFIG_CMD_EDITENV */
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000560#endif /* CONFIG_SPL_BUILD */
Peter Tyser246c6922009-10-25 15:12:56 -0500561
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200562/*
wdenka68d3ed2002-10-11 08:38:32 +0000563 * Look up variable from environment,
564 * return address of storage for that variable,
565 * or NULL if not found
566 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200567char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000568{
Igor Grinbergd09b1782011-11-07 01:13:59 +0000569 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200570 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000571
Wolfgang Denk91a76752010-07-24 20:22:02 +0200572 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000573
Igor Grinbergd09b1782011-11-07 01:13:59 +0000574 e.key = name;
575 e.data = NULL;
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600576 hsearch_r(e, FIND, &ep, &env_htab, 0);
wdenka68d3ed2002-10-11 08:38:32 +0000577
Macpaul Linf3c615b2011-04-26 16:16:45 +0000578 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000579 }
580
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200581 /* restricted capabilities before import */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200582 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
583 return (char *)(gd->env_buf);
584
585 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000586}
587
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200588/*
589 * Look up variable from environment for restricted C runtime env.
590 */
Wolfgang Denk84b5e802011-07-29 14:42:18 +0200591int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000592{
593 int i, nxt;
594
Igor Grinbergd09b1782011-11-07 01:13:59 +0000595 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000596 int val, n;
597
Macpaul Linf3c615b2011-04-26 16:16:45 +0000598 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
599 if (nxt >= CONFIG_ENV_SIZE)
600 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000601 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000602
603 val = envmatch((uchar *)name, i);
604 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000605 continue;
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200606
wdenka68d3ed2002-10-11 08:38:32 +0000607 /* found; copy out */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000608 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000609 *buf = env_get_char(val++);
610 if (*buf == '\0')
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200611 return n;
612 }
613
614 if (n)
615 *--buf = '\0';
616
Wolfgang Denka02a8842011-05-04 10:29:49 +0000617 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
618 len, name);
Wolfgang Denk9ed4a952010-07-24 22:16:20 +0200619
620 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000621 }
Igor Grinbergd09b1782011-11-07 01:13:59 +0000622
Macpaul Linf3c615b2011-04-26 16:16:45 +0000623 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000624}
625
Simon Glass4a9b4132011-10-14 13:25:18 +0000626/**
627 * Decode the integer value of an environment variable and return it.
628 *
629 * @param name Name of environemnt variable
630 * @param base Number base to use (normally 10, or 16 for hex)
631 * @param default_val Default value to return if the variable is not
632 * found
633 * @return the decoded value, or default_val if not found
634 */
635ulong getenv_ulong(const char *name, int base, ulong default_val)
636{
637 /*
638 * We can use getenv() here, even before relocation, since the
639 * environment variable value is an integer and thus short.
640 */
641 const char *str = getenv(name);
642
643 return str ? simple_strtoul(str, NULL, base) : default_val;
644}
645
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000646#ifndef CONFIG_SPL_BUILD
Mike Frysingerbdab39d2009-01-28 19:08:14 -0500647#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillips088f1b12012-10-29 13:34:31 +0000648static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
649 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000650{
Macpaul Linf3c615b2011-04-26 16:16:45 +0000651 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000652
Macpaul Linf3c615b2011-04-26 16:16:45 +0000653 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000654}
wdenk8bde7f72003-06-27 21:31:46 +0000655
Mike Frysingerba69dc22008-12-30 02:59:25 -0500656U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200657 saveenv, 1, 0, do_env_save,
Peter Tyser2fb26042009-01-27 18:03:12 -0600658 "save environment variables to persistent storage",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200659 ""
Mike Frysingerba69dc22008-12-30 02:59:25 -0500660);
wdenka68d3ed2002-10-11 08:38:32 +0000661#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000662#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000663
664
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200665/*
wdenka68d3ed2002-10-11 08:38:32 +0000666 * Match a name / name=value pair
667 *
668 * s1 is either a simple 'name', or a 'name=value' pair.
669 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergd09b1782011-11-07 01:13:59 +0000670 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000671 */
Macpaul Linf3c615b2011-04-26 16:16:45 +0000672int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000673{
Joe Hershberger586197d2012-10-03 09:38:50 +0000674 if (s1 == NULL)
675 return -1;
676
wdenka68d3ed2002-10-11 08:38:32 +0000677 while (*s1 == env_get_char(i2++))
678 if (*s1++ == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000679 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000680
wdenka68d3ed2002-10-11 08:38:32 +0000681 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linf3c615b2011-04-26 16:16:45 +0000682 return i2;
Igor Grinbergd09b1782011-11-07 01:13:59 +0000683
Macpaul Linf3c615b2011-04-26 16:16:45 +0000684 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000685}
wdenk8bde7f72003-06-27 21:31:46 +0000686
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000687#ifndef CONFIG_SPL_BUILD
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000688static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergd09b1782011-11-07 01:13:59 +0000689 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200690{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000691 int all = 0, flag = 0;
Macpaul Linf3c615b2011-04-26 16:16:45 +0000692
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000693 debug("Initial value for argc=%d\n", argc);
694 while (--argc > 0 && **++argv == '-') {
695 char *arg = *argv;
696
697 while (*++arg) {
698 switch (*arg) {
699 case 'a': /* default all */
700 all = 1;
701 break;
702 case 'f': /* force */
703 flag |= H_FORCE;
704 break;
705 default:
706 return cmd_usage(cmdtp);
707 }
708 }
709 }
710 debug("Final value for argc=%d\n", argc);
711 if (all && (argc == 0)) {
712 /* Reset the whole environment */
713 set_default_env("## Resetting to default environment\n");
714 return 0;
715 }
716 if (!all && (argc > 0)) {
717 /* Reset individual variables */
718 set_default_vars(argc, argv);
719 return 0;
720 }
721
722 return cmd_usage(cmdtp);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200723}
wdenk8bde7f72003-06-27 21:31:46 +0000724
Igor Grinbergd09b1782011-11-07 01:13:59 +0000725static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
726 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200727{
728 printf("Not implemented yet\n");
729 return 0;
730}
731
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500732#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200733/*
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100734 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200735 * -t: export as text format; if size is given, data will be
736 * padded with '\0' bytes; if not, one terminating '\0'
737 * will be added (which is included in the "filesize"
738 * setting so you can for exmple copy this to flash and
739 * keep the termination).
740 * -b: export as binary format (name=value pairs separated by
741 * '\0', list end marked by double "\0\0")
742 * -c: export as checksum protected environment format as
743 * used for example by "saveenv" command
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100744 * -s size:
745 * size of output buffer
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200746 * addr: memory address where environment gets stored
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100747 * var... List of variable names that get included into the
748 * export. Without arguments, the whole environment gets
749 * exported.
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200750 *
751 * With "-c" and size is NOT given, then the export command will
752 * format the data as currently used for the persistent storage,
753 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
754 * prepend a valid CRC32 checksum and, in case of resundant
755 * environment, a "current" redundancy flag. If size is given, this
756 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
757 * checksum and redundancy flag will be inserted.
758 *
759 * With "-b" and "-t", always only the real data (including a
760 * terminating '\0' byte) will be written; here the optional size
761 * argument will be used to make sure not to overflow the user
762 * provided buffer; the command will abort if the size is not
763 * sufficient. Any remainign space will be '\0' padded.
764 *
765 * On successful return, the variable "filesize" will be set.
766 * Note that filesize includes the trailing/terminating '\0' byte(s).
767 *
768 * Usage szenario: create a text snapshot/backup of the current settings:
769 *
770 * => env export -t 100000
771 * => era ${backup_addr} +${filesize}
772 * => cp.b 100000 ${backup_addr} ${filesize}
773 *
774 * Re-import this snapshot, deleting all other settings:
775 *
776 * => env import -d -t ${backup_addr}
777 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000778static int do_env_export(cmd_tbl_t *cmdtp, int flag,
779 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200780{
781 char buf[32];
782 char *addr, *cmd, *res;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100783 size_t size = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200784 ssize_t len;
785 env_t *envp;
786 char sep = '\n';
787 int chk = 0;
788 int fmt = 0;
789
790 cmd = *argv;
791
792 while (--argc > 0 && **++argv == '-') {
793 char *arg = *argv;
794 while (*++arg) {
795 switch (*arg) {
796 case 'b': /* raw binary format */
797 if (fmt++)
798 goto sep_err;
799 sep = '\0';
800 break;
801 case 'c': /* external checksum format */
802 if (fmt++)
803 goto sep_err;
804 sep = '\0';
805 chk = 1;
806 break;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100807 case 's': /* size given */
808 if (--argc <= 0)
809 return cmd_usage(cmdtp);
810 size = simple_strtoul(*++argv, NULL, 16);
811 goto NXTARG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200812 case 't': /* text format */
813 if (fmt++)
814 goto sep_err;
815 sep = '\n';
816 break;
817 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000818 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200819 }
820 }
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100821NXTARG: ;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200822 }
823
Macpaul Linf3c615b2011-04-26 16:16:45 +0000824 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000825 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200826
827 addr = (char *)simple_strtoul(argv[0], NULL, 16);
828
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100829 if (size)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200830 memset(addr, '\0', size);
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100831
832 argc--;
833 argv++;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200834
835 if (sep) { /* export as text file */
Joe Hershbergerbe112352012-12-11 22:16:23 -0600836 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200837 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000838 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200839 return 1;
840 }
Andreas Bießmann8c3aff52011-02-09 15:10:29 +0100841 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200842 setenv("filesize", buf);
843
844 return 0;
845 }
846
847 envp = (env_t *)addr;
848
849 if (chk) /* export as checksum protected block */
850 res = (char *)envp->data;
851 else /* export as raw binary data */
852 res = addr;
853
Joe Hershbergerbe112352012-12-11 22:16:23 -0600854 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200855 if (len < 0) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000856 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200857 return 1;
858 }
859
860 if (chk) {
Igor Grinbergd09b1782011-11-07 01:13:59 +0000861 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200862#ifdef CONFIG_ENV_ADDR_REDUND
863 envp->flags = ACTIVE_FLAG;
864#endif
865 }
Macpaul Linf3c615b2011-04-26 16:16:45 +0000866 sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200867 setenv("filesize", buf);
868
869 return 0;
870
871sep_err:
Igor Grinbergd09b1782011-11-07 01:13:59 +0000872 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200873 return 1;
874}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500875#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200876
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500877#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200878/*
879 * env import [-d] [-t | -b | -c] addr [size]
880 * -d: delete existing environment before importing;
881 * otherwise overwrite / append to existion definitions
882 * -t: assume text format; either "size" must be given or the
883 * text data must be '\0' terminated
884 * -b: assume binary format ('\0' separated, "\0\0" terminated)
885 * -c: assume checksum protected environment format
886 * addr: memory address to read from
887 * size: length of input data; if missing, proper '\0'
888 * termination is mandatory
889 */
Igor Grinbergd09b1782011-11-07 01:13:59 +0000890static int do_env_import(cmd_tbl_t *cmdtp, int flag,
891 int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200892{
893 char *cmd, *addr;
894 char sep = '\n';
895 int chk = 0;
896 int fmt = 0;
897 int del = 0;
898 size_t size;
899
900 cmd = *argv;
901
902 while (--argc > 0 && **++argv == '-') {
903 char *arg = *argv;
904 while (*++arg) {
905 switch (*arg) {
906 case 'b': /* raw binary format */
907 if (fmt++)
908 goto sep_err;
909 sep = '\0';
910 break;
911 case 'c': /* external checksum format */
912 if (fmt++)
913 goto sep_err;
914 sep = '\0';
915 chk = 1;
916 break;
917 case 't': /* text format */
918 if (fmt++)
919 goto sep_err;
920 sep = '\n';
921 break;
922 case 'd':
923 del = 1;
924 break;
925 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000926 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200927 }
928 }
929 }
930
Macpaul Linf3c615b2011-04-26 16:16:45 +0000931 if (argc < 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000932 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200933
934 if (!fmt)
935 printf("## Warning: defaulting to text format\n");
936
937 addr = (char *)simple_strtoul(argv[0], NULL, 16);
938
939 if (argc == 2) {
940 size = simple_strtoul(argv[1], NULL, 16);
941 } else {
942 char *s = addr;
943
944 size = 0;
945
946 while (size < MAX_ENV_SIZE) {
947 if ((*s == sep) && (*(s+1) == '\0'))
948 break;
949 ++s;
950 ++size;
951 }
952 if (size == MAX_ENV_SIZE) {
953 printf("## Warning: Input data exceeds %d bytes"
954 " - truncated\n", MAX_ENV_SIZE);
955 }
Horst Kronstorferd3f80c72011-12-16 23:33:10 +0000956 size += 2;
Simon Glass79afc882011-11-04 06:42:36 +0000957 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200958 }
959
960 if (chk) {
961 uint32_t crc;
962 env_t *ep = (env_t *)addr;
963
964 size -= offsetof(env_t, data);
965 memcpy(&crc, &ep->crc, sizeof(crc));
966
967 if (crc32(0, ep->data, size) != crc) {
968 puts("## Error: bad CRC, import failed\n");
969 return 1;
970 }
971 addr = (char *)ep->data;
972 }
973
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000974 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600975 0, NULL) == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200976 error("Environment import failed: errno = %d\n", errno);
977 return 1;
978 }
979 gd->flags |= GD_FLG_ENV_READY;
980
981 return 0;
982
983sep_err:
984 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
985 cmd);
986 return 1;
987}
Mike Frysinger0c79cda2010-12-26 23:09:45 -0500988#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200989
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200990/*
991 * New command line interface: "env" command with subcommands
992 */
993static cmd_tbl_t cmd_env_sub[] = {
994#if defined(CONFIG_CMD_ASKENV)
995 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
996#endif
997 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
998 U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
999#if defined(CONFIG_CMD_EDITENV)
1000 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1001#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001002#if defined(CONFIG_CMD_ENV_CALLBACK)
1003 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1004#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001005#if defined(CONFIG_CMD_ENV_FLAGS)
1006 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1007#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001008#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001009 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001010#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001011#if defined(CONFIG_CMD_GREPENV)
1012 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1013#endif
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001014#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001015 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysinger0c79cda2010-12-26 23:09:45 -05001016#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001017 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1018#if defined(CONFIG_CMD_RUN)
1019 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1020#endif
1021#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1022 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1023#endif
1024 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1025};
1026
Wolfgang Denk2e5167c2010-10-28 20:00:11 +02001027#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +02001028void env_reloc(void)
1029{
1030 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1031}
1032#endif
1033
Macpaul Linf3c615b2011-04-26 16:16:45 +00001034static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001035{
1036 cmd_tbl_t *cp;
1037
Thomas Weber5904da02010-11-24 13:07:52 +01001038 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001039 return CMD_RET_USAGE;
Thomas Weber5904da02010-11-24 13:07:52 +01001040
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001041 /* drop initial "env" arg */
1042 argc--;
1043 argv++;
1044
1045 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1046
1047 if (cp)
1048 return cp->cmd(cmdtp, flag, argc, argv);
1049
Simon Glass4c12eeb2011-12-10 08:44:01 +00001050 return CMD_RET_USAGE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001051}
1052
Kim Phillips088f1b12012-10-29 13:34:31 +00001053#ifdef CONFIG_SYS_LONGHELP
1054static char env_help_text[] =
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001055#if defined(CONFIG_CMD_ASKENV)
1056 "ask name [message] [size] - ask for environment variable\nenv "
1057#endif
Joe Hershberger5e2b3e02012-12-11 22:16:25 -06001058#if defined(CONFIG_CMD_ENV_CALLBACK)
1059 "callbacks - print callbacks and their associated variables\nenv "
1060#endif
Gerlando Falautob64b7c32012-08-24 00:11:41 +00001061 "default [-f] -a - [forcibly] reset default environment\n"
1062 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001063#if defined(CONFIG_CMD_EDITENV)
1064 "env edit name - edit environment variable\n"
1065#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001066#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk37f2fe72011-11-06 22:49:44 +01001067 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001068#endif
Joe Hershbergerfffad712012-12-11 22:16:33 -06001069#if defined(CONFIG_CMD_ENV_FLAGS)
1070 "env flags - print variables that have non-default flags\n"
1071#endif
Kim Phillipsa000b792011-04-05 07:15:14 +00001072#if defined(CONFIG_CMD_GREPENV)
1073 "env grep string [...] - search environment\n"
1074#endif
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001075#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsa000b792011-04-05 07:15:14 +00001076 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeau4796bc42012-08-10 07:45:44 +00001077#endif
Joe Hershbergerbe112352012-12-11 22:16:23 -06001078 "env print [-a | name ...] - print environment\n"
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001079#if defined(CONFIG_CMD_RUN)
1080 "env run var [...] - run commands in an environment variable\n"
1081#endif
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001082#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001083 "env save - save environment\n"
Horst Kronstorferd798a9b2011-12-10 02:25:19 +00001084#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001085 "env set [-f] name [arg ...]\n";
1086#endif
1087
1088U_BOOT_CMD(
1089 env, CONFIG_SYS_MAXARGS, 1, do_env,
1090 "environment handling commands", env_help_text
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001091);
1092
1093/*
1094 * Old command line interface, kept for compatibility
1095 */
wdenk8bde7f72003-06-27 21:31:46 +00001096
Peter Tyser246c6922009-10-25 15:12:56 -05001097#if defined(CONFIG_CMD_EDITENV)
Mike Frysinger722b0612010-10-20 03:52:39 -04001098U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001099 editenv, 2, 0, do_env_edit,
Peter Tyser246c6922009-10-25 15:12:56 -05001100 "edit environment variable",
1101 "name\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001102 " - edit environment variable 'name'",
1103 var_complete
Peter Tyser246c6922009-10-25 15:12:56 -05001104);
1105#endif
1106
Mike Frysinger722b0612010-10-20 03:52:39 -04001107U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001108 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyser2fb26042009-01-27 18:03:12 -06001109 "print environment variables",
Joe Hershbergerbe112352012-12-11 22:16:23 -06001110 "[-a]\n - print [all] values of all environment variables\n"
wdenk8bde7f72003-06-27 21:31:46 +00001111 "printenv name ...\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001112 " - print value of environment variable 'name'",
1113 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001114);
1115
Kim Phillipsa000b792011-04-05 07:15:14 +00001116#ifdef CONFIG_CMD_GREPENV
1117U_BOOT_CMD_COMPLETE(
1118 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1119 "search environment variables",
1120 "string ...\n"
1121 " - list environment name=value pairs matching 'string'",
1122 var_complete
1123);
1124#endif
1125
Mike Frysinger722b0612010-10-20 03:52:39 -04001126U_BOOT_CMD_COMPLETE(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001127 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyser2fb26042009-01-27 18:03:12 -06001128 "set environment variables",
Joe Hershberger24ab5a12012-12-11 22:16:35 -06001129 "[-f] name value ...\n"
1130 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1131 "setenv [-f] name\n"
1132 " - [forcibly] delete environment variable 'name'",
Mike Frysinger722b0612010-10-20 03:52:39 -04001133 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001134);
1135
Jon Loeligerc76fe472007-07-08 18:02:23 -05001136#if defined(CONFIG_CMD_ASKENV)
wdenk8bde7f72003-06-27 21:31:46 +00001137
wdenk0d498392003-07-01 21:06:45 +00001138U_BOOT_CMD(
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001139 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyser2fb26042009-01-27 18:03:12 -06001140 "get environment variables from stdin",
wdenk8bde7f72003-06-27 21:31:46 +00001141 "name [message] [size]\n"
1142 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1143 "askenv name\n"
1144 " - get environment variable 'name' from stdin\n"
1145 "askenv name size\n"
1146 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1147 "askenv name [message] size\n"
1148 " - display 'message' string and get environment variable 'name'"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001149 "from stdin (max 'size' chars)"
wdenk8bde7f72003-06-27 21:31:46 +00001150);
Jon Loeliger90253172007-07-10 11:02:44 -05001151#endif
wdenk8bde7f72003-06-27 21:31:46 +00001152
Jon Loeligerc76fe472007-07-08 18:02:23 -05001153#if defined(CONFIG_CMD_RUN)
Mike Frysinger722b0612010-10-20 03:52:39 -04001154U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001155 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyser2fb26042009-01-27 18:03:12 -06001156 "run commands in an environment variable",
wdenk8bde7f72003-06-27 21:31:46 +00001157 "var [...]\n"
Mike Frysinger722b0612010-10-20 03:52:39 -04001158 " - run the commands in the environment variable(s) 'var'",
1159 var_complete
wdenk8bde7f72003-06-27 21:31:46 +00001160);
Jon Loeliger90253172007-07-10 11:02:44 -05001161#endif
Ilya Yanok7ac2fe22012-09-18 00:22:50 +00001162#endif /* CONFIG_SPL_BUILD */