blob: 49b9d74a8cc37f2727c7a45d20880d3805724287 [file] [log] [blame]
wdenka68d3ed2002-10-11 08:38:32 +00001/*
Wolfgang Denk565af272013-03-23 23:50:28 +00002 * (C) Copyright 2000-2013
wdenka68d3ed2002-10-11 08:38:32 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
Kim Phillipsf0c9d532011-04-05 07:15:14 +00007 *
8 * Copyright 2011 Freescale Semiconductor, Inc.
9 *
wdenka68d3ed2002-10-11 08:38:32 +000010 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020029/*
wdenka68d3ed2002-10-11 08:38:32 +000030 * Support for persistent environment data
31 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020032 * The "environment" is stored on external storage as a list of '\0'
33 * terminated "name=value" strings. The end of the list is marked by
34 * a double '\0'. The environment is preceeded by a 32 bit CRC over
35 * the data part and, in case of redundant environment, a byte of
36 * flags.
wdenka68d3ed2002-10-11 08:38:32 +000037 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020038 * This linearized representation will also be used before
39 * relocation, i. e. as long as we don't have a full C runtime
40 * environment. After that, we use a hash table.
wdenka68d3ed2002-10-11 08:38:32 +000041 */
42
43#include <common.h>
44#include <command.h>
45#include <environment.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020046#include <search.h>
47#include <errno.h>
Peter Tyser0deafa22009-10-25 15:12:56 -050048#include <malloc.h>
wdenk2a3cb022002-11-05 21:01:48 +000049#include <watchdog.h>
wdenka68d3ed2002-10-11 08:38:32 +000050#include <linux/stddef.h>
51#include <asm/byteorder.h>
wdenka68d3ed2002-10-11 08:38:32 +000052
Wolfgang Denk6405a152006-03-31 18:32:53 +020053DECLARE_GLOBAL_DATA_PTR;
54
Macpaul Linff2a9f32011-04-26 16:16:45 +000055#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
56 !defined(CONFIG_ENV_IS_IN_FLASH) && \
57 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000058 !defined(CONFIG_ENV_IS_IN_MMC) && \
Maximilian Schwerin9765c082012-03-12 23:57:50 +000059 !defined(CONFIG_ENV_IS_IN_FAT) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000060 !defined(CONFIG_ENV_IS_IN_NAND) && \
61 !defined(CONFIG_ENV_IS_IN_NVRAM) && \
62 !defined(CONFIG_ENV_IS_IN_ONENAND) && \
63 !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
Liu Gang85bcd732012-03-08 00:33:20 +000064 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
Joe Hershberger0c5faa82013-04-08 10:32:51 +000065 !defined(CONFIG_ENV_IS_IN_UBI) && \
Macpaul Linff2a9f32011-04-26 16:16:45 +000066 !defined(CONFIG_ENV_IS_NOWHERE)
unsik Kimf0b1bdd2009-02-25 11:31:24 +090067# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
Joe Hershberger0c5faa82013-04-08 10:32:51 +000068SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
wdenka68d3ed2002-10-11 08:38:32 +000069#endif
70
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071/*
72 * Maximum expected input data size for import command
73 */
74#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
wdenka68d3ed2002-10-11 08:38:32 +000075
wdenka68d3ed2002-10-11 08:38:32 +000076/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020077 * This variable is incremented on each do_env_set(), so it can
Heiko Schocher3b195ff2009-04-28 08:36:11 +020078 * be used via get_env_id() as an indication, if the environment
79 * has changed or not. So it is possible to reread an environment
80 * variable only if the environment was changed ... done so for
81 * example in NetInitLoop()
82 */
Heiko Schocher0c303fa2009-02-10 09:38:52 +010083static int env_id = 1;
wdenka68d3ed2002-10-11 08:38:32 +000084
Macpaul Linff2a9f32011-04-26 16:16:45 +000085int get_env_id(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +010086{
87 return env_id;
88}
wdenka68d3ed2002-10-11 08:38:32 +000089
Ilya Yanokf7a2c552012-09-18 00:22:50 +000090#ifndef CONFIG_SPL_BUILD
Mike Frysinger87944922009-05-24 02:26:19 -040091/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020092 * Command interface: print one or all environment variables
93 *
94 * Returns 0 in case of error, or length of printed string
Mike Frysinger87944922009-05-24 02:26:19 -040095 */
Joe Hershberger79a905e2012-12-11 22:16:23 -060096static int env_print(char *name, int flag)
wdenka68d3ed2002-10-11 08:38:32 +000097{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098 char *res = NULL;
Maxime Larocqueec856812012-09-28 05:00:13 +000099 ssize_t len;
wdenka68d3ed2002-10-11 08:38:32 +0000100
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101 if (name) { /* print a single name */
102 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000103
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200104 e.key = name;
105 e.data = NULL;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600106 hsearch_r(e, FIND, &ep, &env_htab, flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200107 if (ep == NULL)
108 return 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000109 len = printf("%s=%s\n", ep->key, ep->data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 return len;
111 }
wdenka68d3ed2002-10-11 08:38:32 +0000112
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200113 /* print whole list */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600114 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
wdenka68d3ed2002-10-11 08:38:32 +0000115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 if (len > 0) {
117 puts(res);
118 free(res);
119 return len;
wdenka68d3ed2002-10-11 08:38:32 +0000120 }
121
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200122 /* should never happen */
Maxime Larocqueec856812012-09-28 05:00:13 +0000123 printf("## Error: cannot export environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200124 return 0;
Mike Frysinger87944922009-05-24 02:26:19 -0400125}
wdenka68d3ed2002-10-11 08:38:32 +0000126
Kim Phillipsdc00a682012-10-29 13:34:31 +0000127static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
128 char * const argv[])
Mike Frysinger87944922009-05-24 02:26:19 -0400129{
130 int i;
131 int rcode = 0;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600132 int env_flag = H_HIDE_DOT;
133
134 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
135 argc--;
136 argv++;
137 env_flag &= ~H_HIDE_DOT;
138 }
wdenka68d3ed2002-10-11 08:38:32 +0000139
Mike Frysinger87944922009-05-24 02:26:19 -0400140 if (argc == 1) {
141 /* print all env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600142 rcode = env_print(NULL, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200143 if (!rcode)
Mike Frysinger87944922009-05-24 02:26:19 -0400144 return 1;
145 printf("\nEnvironment size: %d/%ld bytes\n",
146 rcode, (ulong)ENV_SIZE);
147 return 0;
148 }
wdenka68d3ed2002-10-11 08:38:32 +0000149
Mike Frysinger87944922009-05-24 02:26:19 -0400150 /* print selected env vars */
Joe Hershberger79a905e2012-12-11 22:16:23 -0600151 env_flag &= ~H_HIDE_DOT;
Mike Frysinger87944922009-05-24 02:26:19 -0400152 for (i = 1; i < argc; ++i) {
Joe Hershberger79a905e2012-12-11 22:16:23 -0600153 int rc = env_print(argv[i], env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200154 if (!rc) {
155 printf("## Error: \"%s\" not defined\n", argv[i]);
Mike Frysinger87944922009-05-24 02:26:19 -0400156 ++rcode;
wdenka68d3ed2002-10-11 08:38:32 +0000157 }
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000158 }
159
160 return rcode;
161}
162
163#ifdef CONFIG_CMD_GREPENV
Igor Grinbergde832562011-11-07 01:13:59 +0000164static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
165 int argc, char * const argv[])
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000166{
167 ENTRY *match;
168 unsigned char matched[env_htab.size / 8];
169 int rcode = 1, arg = 1, idx;
170
171 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000172 return CMD_RET_USAGE;
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000173
174 memset(matched, 0, env_htab.size / 8);
175
176 while (arg <= argc) {
177 idx = 0;
Kumar Galae7c338b2011-11-23 09:48:02 +0000178 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000179 if (!(matched[idx / 8] & (1 << (idx & 7)))) {
180 puts(match->key);
181 puts("=");
182 puts(match->data);
183 puts("\n");
184 }
185 matched[idx / 8] |= 1 << (idx & 7);
186 rcode = 0;
187 }
188 arg++;
wdenka68d3ed2002-10-11 08:38:32 +0000189 }
Mike Frysinger87944922009-05-24 02:26:19 -0400190
wdenka68d3ed2002-10-11 08:38:32 +0000191 return rcode;
192}
Kim Phillipsf0c9d532011-04-05 07:15:14 +0000193#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000194#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000195
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200196/*
Gerlando Falautodc88d592012-08-24 00:11:37 +0000197 * Set a new environment variable,
198 * or replace or delete an existing one.
Joe Hershberger71497d02012-12-11 22:16:31 -0600199 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000200static int _do_env_set(int flag, int argc, char * const argv[])
Gerlando Falautodc88d592012-08-24 00:11:37 +0000201{
202 int i, len;
203 char *name, *value, *s;
204 ENTRY e, *ep;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600205 int env_flag = H_INTERACTIVE;
Gerlando Falautodc88d592012-08-24 00:11:37 +0000206
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600207 debug("Initial value for argc=%d\n", argc);
208 while (argc > 1 && **(argv + 1) == '-') {
209 char *arg = *++argv;
210
211 --argc;
212 while (*++arg) {
213 switch (*arg) {
214 case 'f': /* force */
215 env_flag |= H_FORCE;
216 break;
217 default:
218 return CMD_RET_USAGE;
219 }
220 }
221 }
222 debug("Final value for argc=%d\n", argc);
Gerlando Falautodc88d592012-08-24 00:11:37 +0000223 name = argv[1];
224 value = argv[2];
225
226 if (strchr(name, '=')) {
227 printf("## Error: illegal character '='"
228 "in variable name \"%s\"\n", name);
229 return 1;
230 }
231
232 env_id++;
wdenka68d3ed2002-10-11 08:38:32 +0000233
wdenka68d3ed2002-10-11 08:38:32 +0000234 /* Delete only ? */
Igor Grinbergde832562011-11-07 01:13:59 +0000235 if (argc < 3 || argv[2] == NULL) {
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600236 int rc = hdelete_r(name, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200237 return !rc;
wdenka68d3ed2002-10-11 08:38:32 +0000238 }
239
240 /*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200241 * Insert / replace new value
wdenka68d3ed2002-10-11 08:38:32 +0000242 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000243 for (i = 2, len = 0; i < argc; ++i)
wdenka68d3ed2002-10-11 08:38:32 +0000244 len += strlen(argv[i]) + 1;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000245
246 value = malloc(len);
247 if (value == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200248 printf("## Can't malloc %d bytes\n", len);
wdenka68d3ed2002-10-11 08:38:32 +0000249 return 1;
250 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000251 for (i = 2, s = value; i < argc; ++i) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200252 char *v = argv[i];
wdenka68d3ed2002-10-11 08:38:32 +0000253
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200254 while ((*s++ = *v++) != '\0')
wdenka68d3ed2002-10-11 08:38:32 +0000255 ;
Igor Grinbergde832562011-11-07 01:13:59 +0000256 *(s - 1) = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000257 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200258 if (s != value)
259 *--s = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000260
Igor Grinbergde832562011-11-07 01:13:59 +0000261 e.key = name;
262 e.data = value;
Joe Hershbergerf64bb772012-12-11 22:16:35 -0600263 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200264 free(value);
265 if (!ep) {
266 printf("## Error inserting \"%s\" variable, errno=%d\n",
267 name, errno);
268 return 1;
269 }
wdenka68d3ed2002-10-11 08:38:32 +0000270
wdenka68d3ed2002-10-11 08:38:32 +0000271 return 0;
272}
273
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200274int setenv(const char *varname, const char *varvalue)
wdenka68d3ed2002-10-11 08:38:32 +0000275{
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200276 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
277
Joe Hershberger3902c602013-04-08 10:32:50 +0000278 /* before import into hashtable */
279 if (!(gd->flags & GD_FLG_ENV_READY))
280 return 1;
281
Igor Grinbergde832562011-11-07 01:13:59 +0000282 if (varvalue == NULL || varvalue[0] == '\0')
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200283 return _do_env_set(0, 2, (char * const *)argv);
Jeffrey Mann62b896f2007-04-23 14:00:11 +0200284 else
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200285 return _do_env_set(0, 3, (char * const *)argv);
wdenka68d3ed2002-10-11 08:38:32 +0000286}
287
Simon Glassb8e926e2011-10-24 17:59:59 +0000288/**
289 * Set an environment variable to an integer value
290 *
291 * @param varname Environmet variable to set
292 * @param value Value to set it to
293 * @return 0 if ok, 1 on error
294 */
295int setenv_ulong(const char *varname, ulong value)
296{
297 /* TODO: this should be unsigned */
298 char *str = simple_itoa(value);
299
300 return setenv(varname, str);
301}
302
303/**
Simon Glass044dc9e2013-02-24 17:33:21 +0000304 * Set an environment variable to an value in hex
Simon Glassb8e926e2011-10-24 17:59:59 +0000305 *
306 * @param varname Environmet variable to set
Simon Glass044dc9e2013-02-24 17:33:21 +0000307 * @param value Value to set it to
Simon Glassb8e926e2011-10-24 17:59:59 +0000308 * @return 0 if ok, 1 on error
309 */
Simon Glass044dc9e2013-02-24 17:33:21 +0000310int setenv_hex(const char *varname, ulong value)
Simon Glassb8e926e2011-10-24 17:59:59 +0000311{
312 char str[17];
313
Simon Glass044dc9e2013-02-24 17:33:21 +0000314 sprintf(str, "%lx", value);
Simon Glassb8e926e2011-10-24 17:59:59 +0000315 return setenv(varname, str);
316}
317
Simon Glassabbd5122013-04-20 08:42:43 +0000318ulong getenv_hex(const char *varname, ulong default_val)
319{
320 const char *s;
321 ulong value;
322 char *endp;
323
324 s = getenv(varname);
325 if (s)
326 value = simple_strtoul(s, &endp, 16);
327 if (!s || endp == s)
328 return default_val;
329
330 return value;
331}
332
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000333#ifndef CONFIG_SPL_BUILD
Kim Phillipsdc00a682012-10-29 13:34:31 +0000334static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000335{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200336 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000337 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000338
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200339 return _do_env_set(flag, argc, argv);
wdenka68d3ed2002-10-11 08:38:32 +0000340}
341
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200342/*
wdenka68d3ed2002-10-11 08:38:32 +0000343 * Prompt for environment variable
344 */
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500345#if defined(CONFIG_CMD_ASKENV)
Macpaul Linff2a9f32011-04-26 16:16:45 +0000346int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000347{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200348 char message[CONFIG_SYS_CBSIZE];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000349 int i, len, pos, size;
wdenka68d3ed2002-10-11 08:38:32 +0000350 char *local_args[4];
Wolfgang Denk586895d2013-02-20 04:53:16 +0000351 char *endptr;
wdenka68d3ed2002-10-11 08:38:32 +0000352
353 local_args[0] = argv[0];
354 local_args[1] = argv[1];
355 local_args[2] = NULL;
356 local_args[3] = NULL;
357
Wolfgang Denk586895d2013-02-20 04:53:16 +0000358 /*
359 * Check the syntax:
360 *
361 * env_ask envname [message1 ...] [size]
362 */
363 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000364 return CMD_RET_USAGE;
wdenka68d3ed2002-10-11 08:38:32 +0000365
Wolfgang Denk586895d2013-02-20 04:53:16 +0000366 /*
367 * We test the last argument if it can be converted
368 * into a decimal number. If yes, we assume it's
369 * the size. Otherwise we echo it as part of the
370 * message.
371 */
372 i = simple_strtoul(argv[argc - 1], &endptr, 10);
373 if (*endptr != '\0') { /* no size */
374 size = CONFIG_SYS_CBSIZE - 1;
375 } else { /* size given */
376 size = i;
377 --argc;
378 }
wdenka68d3ed2002-10-11 08:38:32 +0000379
Wolfgang Denk586895d2013-02-20 04:53:16 +0000380 if (argc <= 2) {
381 sprintf(message, "Please enter '%s': ", argv[1]);
382 } else {
383 /* env_ask envname message1 ... messagen [size] */
384 for (i = 2, pos = 0; i < argc; i++) {
Macpaul Linff2a9f32011-04-26 16:16:45 +0000385 if (pos)
wdenka68d3ed2002-10-11 08:38:32 +0000386 message[pos++] = ' ';
Macpaul Linff2a9f32011-04-26 16:16:45 +0000387
Igor Grinbergde832562011-11-07 01:13:59 +0000388 strcpy(message + pos, argv[i]);
wdenka68d3ed2002-10-11 08:38:32 +0000389 pos += strlen(argv[i]);
390 }
Wolfgang Denk586895d2013-02-20 04:53:16 +0000391 message[pos++] = ' ';
wdenka68d3ed2002-10-11 08:38:32 +0000392 message[pos] = '\0';
wdenka68d3ed2002-10-11 08:38:32 +0000393 }
394
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200395 if (size >= CONFIG_SYS_CBSIZE)
396 size = CONFIG_SYS_CBSIZE - 1;
wdenka68d3ed2002-10-11 08:38:32 +0000397
398 if (size <= 0)
399 return 1;
400
401 /* prompt for input */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200402 len = readline(message);
wdenka68d3ed2002-10-11 08:38:32 +0000403
404 if (size < len)
405 console_buffer[size] = '\0';
406
407 len = 2;
408 if (console_buffer[0] != '\0') {
409 local_args[2] = console_buffer;
410 len = 3;
411 }
412
413 /* Continue calling setenv code */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200414 return _do_env_set(flag, len, local_args);
wdenka68d3ed2002-10-11 08:38:32 +0000415}
Jon Loeligerd704d912007-07-10 11:02:44 -0500416#endif
wdenka68d3ed2002-10-11 08:38:32 +0000417
Joe Hershberger1b0d5512012-12-11 22:16:25 -0600418#if defined(CONFIG_CMD_ENV_CALLBACK)
419static int print_static_binding(const char *var_name, const char *callback_name)
420{
421 printf("\t%-20s %-20s\n", var_name, callback_name);
422
423 return 0;
424}
425
426static int print_active_callback(ENTRY *entry)
427{
428 struct env_clbk_tbl *clbkp;
429 int i;
430 int num_callbacks;
431
432 if (entry->callback == NULL)
433 return 0;
434
435 /* look up the callback in the linker-list */
436 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
437 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
438 i < num_callbacks;
439 i++, clbkp++) {
440#if defined(CONFIG_NEEDS_MANUAL_RELOC)
441 if (entry->callback == clbkp->callback + gd->reloc_off)
442#else
443 if (entry->callback == clbkp->callback)
444#endif
445 break;
446 }
447
448 if (i == num_callbacks)
449 /* this should probably never happen, but just in case... */
450 printf("\t%-20s %p\n", entry->key, entry->callback);
451 else
452 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
453
454 return 0;
455}
456
457/*
458 * Print the callbacks available and what they are bound to
459 */
460int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
461{
462 struct env_clbk_tbl *clbkp;
463 int i;
464 int num_callbacks;
465
466 /* Print the available callbacks */
467 puts("Available callbacks:\n");
468 puts("\tCallback Name\n");
469 puts("\t-------------\n");
470 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
471 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
472 i < num_callbacks;
473 i++, clbkp++)
474 printf("\t%s\n", clbkp->name);
475 puts("\n");
476
477 /* Print the static bindings that may exist */
478 puts("Static callback bindings:\n");
479 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
480 printf("\t%-20s %-20s\n", "-------------", "-------------");
481 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
482 puts("\n");
483
484 /* walk through each variable and print the callback if it has one */
485 puts("Active callback bindings:\n");
486 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
487 printf("\t%-20s %-20s\n", "-------------", "-------------");
488 hwalk_r(&env_htab, print_active_callback);
489 return 0;
490}
491#endif
492
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600493#if defined(CONFIG_CMD_ENV_FLAGS)
494static int print_static_flags(const char *var_name, const char *flags)
495{
496 enum env_flags_vartype type = env_flags_parse_vartype(flags);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600497 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600498
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600499 printf("\t%-20s %-20s %-20s\n", var_name,
500 env_flags_get_vartype_name(type),
501 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600502
503 return 0;
504}
505
506static int print_active_flags(ENTRY *entry)
507{
508 enum env_flags_vartype type;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600509 enum env_flags_varaccess access;
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600510
511 if (entry->flags == 0)
512 return 0;
513
514 type = (enum env_flags_vartype)
515 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600516 access = env_flags_parse_varaccess_from_binflags(entry->flags);
517 printf("\t%-20s %-20s %-20s\n", entry->key,
518 env_flags_get_vartype_name(type),
519 env_flags_get_varaccess_name(access));
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600520
521 return 0;
522}
523
524/*
525 * Print the flags available and what variables have flags
526 */
527int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
528{
529 /* Print the available variable types */
530 printf("Available variable type flags (position %d):\n",
531 ENV_FLAGS_VARTYPE_LOC);
532 puts("\tFlag\tVariable Type Name\n");
533 puts("\t----\t------------------\n");
534 env_flags_print_vartypes();
535 puts("\n");
536
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600537 /* Print the available variable access types */
538 printf("Available variable access flags (position %d):\n",
539 ENV_FLAGS_VARACCESS_LOC);
540 puts("\tFlag\tVariable Access Name\n");
541 puts("\t----\t--------------------\n");
542 env_flags_print_varaccess();
543 puts("\n");
544
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600545 /* Print the static flags that may exist */
546 puts("Static flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600547 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
548 "Variable Access");
549 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
550 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600551 env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
552 puts("\n");
553
554 /* walk through each variable and print the flags if non-default */
555 puts("Active flags:\n");
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600556 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
557 "Variable Access");
558 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
559 "---------------");
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600560 hwalk_r(&env_htab, print_active_flags);
561 return 0;
562}
563#endif
564
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200565/*
Peter Tyser0deafa22009-10-25 15:12:56 -0500566 * Interactively edit an environment variable
567 */
568#if defined(CONFIG_CMD_EDITENV)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000569static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
570 char * const argv[])
Peter Tyser0deafa22009-10-25 15:12:56 -0500571{
572 char buffer[CONFIG_SYS_CBSIZE];
573 char *init_val;
Peter Tyser0deafa22009-10-25 15:12:56 -0500574
Wolfgang Denk3b683112010-07-17 01:06:04 +0200575 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000576 return CMD_RET_USAGE;
Peter Tyser0deafa22009-10-25 15:12:56 -0500577
578 /* Set read buffer to initial value or empty sting */
579 init_val = getenv(argv[1]);
580 if (init_val)
Marek Vasut305e02a2011-09-26 02:26:03 +0200581 sprintf(buffer, "%s", init_val);
Peter Tyser0deafa22009-10-25 15:12:56 -0500582 else
583 buffer[0] = '\0';
584
Joe Hershberger91d1b5f2013-02-08 10:12:34 +0000585 if (readline_into_buffer("edit: ", buffer, 0) < 0)
586 return 1;
Peter Tyser0deafa22009-10-25 15:12:56 -0500587
588 return setenv(argv[1], buffer);
589}
590#endif /* CONFIG_CMD_EDITENV */
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000591#endif /* CONFIG_SPL_BUILD */
Peter Tyser0deafa22009-10-25 15:12:56 -0500592
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200593/*
wdenka68d3ed2002-10-11 08:38:32 +0000594 * Look up variable from environment,
595 * return address of storage for that variable,
596 * or NULL if not found
597 */
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200598char *getenv(const char *name)
wdenka68d3ed2002-10-11 08:38:32 +0000599{
Igor Grinbergde832562011-11-07 01:13:59 +0000600 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200601 ENTRY e, *ep;
wdenka68d3ed2002-10-11 08:38:32 +0000602
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200603 WATCHDOG_RESET();
wdenk2a3cb022002-11-05 21:01:48 +0000604
Igor Grinbergde832562011-11-07 01:13:59 +0000605 e.key = name;
606 e.data = NULL;
Joe Hershbergera46f7702012-12-11 22:16:19 -0600607 hsearch_r(e, FIND, &ep, &env_htab, 0);
Wolfgang Denkf710efd2010-07-24 20:22:02 +0200608
Macpaul Linff2a9f32011-04-26 16:16:45 +0000609 return ep ? ep->data : NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000610 }
611
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200612 /* restricted capabilities before import */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200613 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
614 return (char *)(gd->env_buf);
615
616 return NULL;
wdenka68d3ed2002-10-11 08:38:32 +0000617}
618
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200619/*
620 * Look up variable from environment for restricted C runtime env.
621 */
Wolfgang Denkd67abff2011-07-29 14:42:18 +0200622int getenv_f(const char *name, char *buf, unsigned len)
wdenka68d3ed2002-10-11 08:38:32 +0000623{
624 int i, nxt;
625
Igor Grinbergde832562011-11-07 01:13:59 +0000626 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
wdenka68d3ed2002-10-11 08:38:32 +0000627 int val, n;
628
Macpaul Linff2a9f32011-04-26 16:16:45 +0000629 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
630 if (nxt >= CONFIG_ENV_SIZE)
631 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000632 }
Macpaul Linff2a9f32011-04-26 16:16:45 +0000633
634 val = envmatch((uchar *)name, i);
635 if (val < 0)
wdenka68d3ed2002-10-11 08:38:32 +0000636 continue;
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200637
wdenka68d3ed2002-10-11 08:38:32 +0000638 /* found; copy out */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000639 for (n = 0; n < len; ++n, ++buf) {
Igor Grinbergde832562011-11-07 01:13:59 +0000640 *buf = env_get_char(val++);
641 if (*buf == '\0')
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200642 return n;
643 }
644
645 if (n)
646 *--buf = '\0';
647
Wolfgang Denk43285472011-05-04 10:29:49 +0000648 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
649 len, name);
Wolfgang Denk7a060a72010-07-24 22:16:20 +0200650
651 return n;
wdenka68d3ed2002-10-11 08:38:32 +0000652 }
Igor Grinbergde832562011-11-07 01:13:59 +0000653
Macpaul Linff2a9f32011-04-26 16:16:45 +0000654 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000655}
656
Simon Glassbf690f82011-10-14 13:25:18 +0000657/**
658 * Decode the integer value of an environment variable and return it.
659 *
660 * @param name Name of environemnt variable
661 * @param base Number base to use (normally 10, or 16 for hex)
662 * @param default_val Default value to return if the variable is not
663 * found
664 * @return the decoded value, or default_val if not found
665 */
666ulong getenv_ulong(const char *name, int base, ulong default_val)
667{
668 /*
669 * We can use getenv() here, even before relocation, since the
670 * environment variable value is an integer and thus short.
671 */
672 const char *str = getenv(name);
673
674 return str ? simple_strtoul(str, NULL, base) : default_val;
675}
676
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000677#ifndef CONFIG_SPL_BUILD
Mike Frysinger78dcaf42009-01-28 19:08:14 -0500678#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000679static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
680 char * const argv[])
wdenka68d3ed2002-10-11 08:38:32 +0000681{
Macpaul Linff2a9f32011-04-26 16:16:45 +0000682 printf("Saving Environment to %s...\n", env_name_spec);
wdenka68d3ed2002-10-11 08:38:32 +0000683
Macpaul Linff2a9f32011-04-26 16:16:45 +0000684 return saveenv() ? 1 : 0;
wdenka68d3ed2002-10-11 08:38:32 +0000685}
wdenk57b2d802003-06-27 21:31:46 +0000686
Mike Frysinger61583932008-12-30 02:59:25 -0500687U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200688 saveenv, 1, 0, do_env_save,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600689 "save environment variables to persistent storage",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200690 ""
Mike Frysinger61583932008-12-30 02:59:25 -0500691);
wdenka68d3ed2002-10-11 08:38:32 +0000692#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000693#endif /* CONFIG_SPL_BUILD */
wdenka68d3ed2002-10-11 08:38:32 +0000694
695
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200696/*
wdenka68d3ed2002-10-11 08:38:32 +0000697 * Match a name / name=value pair
698 *
699 * s1 is either a simple 'name', or a 'name=value' pair.
700 * i2 is the environment index for a 'name2=value2' pair.
Igor Grinbergde832562011-11-07 01:13:59 +0000701 * If the names match, return the index for the value2, else -1.
wdenka68d3ed2002-10-11 08:38:32 +0000702 */
Macpaul Linff2a9f32011-04-26 16:16:45 +0000703int envmatch(uchar *s1, int i2)
wdenka68d3ed2002-10-11 08:38:32 +0000704{
Joe Hershbergerdd000dd2012-10-03 09:38:50 +0000705 if (s1 == NULL)
706 return -1;
707
wdenka68d3ed2002-10-11 08:38:32 +0000708 while (*s1 == env_get_char(i2++))
709 if (*s1++ == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000710 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000711
wdenka68d3ed2002-10-11 08:38:32 +0000712 if (*s1 == '\0' && env_get_char(i2-1) == '=')
Macpaul Linff2a9f32011-04-26 16:16:45 +0000713 return i2;
Igor Grinbergde832562011-11-07 01:13:59 +0000714
Macpaul Linff2a9f32011-04-26 16:16:45 +0000715 return -1;
wdenka68d3ed2002-10-11 08:38:32 +0000716}
wdenk57b2d802003-06-27 21:31:46 +0000717
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000718#ifndef CONFIG_SPL_BUILD
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000719static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
Igor Grinbergde832562011-11-07 01:13:59 +0000720 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200721{
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000722 int all = 0, flag = 0;
Macpaul Linff2a9f32011-04-26 16:16:45 +0000723
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000724 debug("Initial value for argc=%d\n", argc);
725 while (--argc > 0 && **++argv == '-') {
726 char *arg = *argv;
727
728 while (*++arg) {
729 switch (*arg) {
730 case 'a': /* default all */
731 all = 1;
732 break;
733 case 'f': /* force */
734 flag |= H_FORCE;
735 break;
736 default:
737 return cmd_usage(cmdtp);
738 }
739 }
740 }
741 debug("Final value for argc=%d\n", argc);
742 if (all && (argc == 0)) {
743 /* Reset the whole environment */
744 set_default_env("## Resetting to default environment\n");
745 return 0;
746 }
747 if (!all && (argc > 0)) {
748 /* Reset individual variables */
749 set_default_vars(argc, argv);
750 return 0;
751 }
752
753 return cmd_usage(cmdtp);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200754}
755
Igor Grinbergde832562011-11-07 01:13:59 +0000756static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
757 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200758{
Joe Hershberger8335ce72012-12-11 22:16:36 -0600759 int env_flag = H_INTERACTIVE;
760 int ret = 0;
761
762 debug("Initial value for argc=%d\n", argc);
763 while (argc > 1 && **(argv + 1) == '-') {
764 char *arg = *++argv;
765
766 --argc;
767 while (*++arg) {
768 switch (*arg) {
769 case 'f': /* force */
770 env_flag |= H_FORCE;
771 break;
772 default:
773 return CMD_RET_USAGE;
774 }
775 }
776 }
777 debug("Final value for argc=%d\n", argc);
778
779 env_id++;
780
781 while (--argc > 0) {
782 char *name = *++argv;
783
784 if (!hdelete_r(name, &env_htab, env_flag))
785 ret = 1;
786 }
787
788 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200789}
790
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500791#ifdef CONFIG_CMD_EXPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200792/*
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100793 * env export [-t | -b | -c] [-s size] addr [var ...]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200794 * -t: export as text format; if size is given, data will be
795 * padded with '\0' bytes; if not, one terminating '\0'
796 * will be added (which is included in the "filesize"
797 * setting so you can for exmple copy this to flash and
798 * keep the termination).
799 * -b: export as binary format (name=value pairs separated by
800 * '\0', list end marked by double "\0\0")
801 * -c: export as checksum protected environment format as
802 * used for example by "saveenv" command
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100803 * -s size:
804 * size of output buffer
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200805 * addr: memory address where environment gets stored
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100806 * var... List of variable names that get included into the
807 * export. Without arguments, the whole environment gets
808 * exported.
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200809 *
810 * With "-c" and size is NOT given, then the export command will
811 * format the data as currently used for the persistent storage,
812 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
813 * prepend a valid CRC32 checksum and, in case of resundant
814 * environment, a "current" redundancy flag. If size is given, this
815 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
816 * checksum and redundancy flag will be inserted.
817 *
818 * With "-b" and "-t", always only the real data (including a
819 * terminating '\0' byte) will be written; here the optional size
820 * argument will be used to make sure not to overflow the user
821 * provided buffer; the command will abort if the size is not
822 * sufficient. Any remainign space will be '\0' padded.
823 *
824 * On successful return, the variable "filesize" will be set.
825 * Note that filesize includes the trailing/terminating '\0' byte(s).
826 *
827 * Usage szenario: create a text snapshot/backup of the current settings:
828 *
829 * => env export -t 100000
830 * => era ${backup_addr} +${filesize}
831 * => cp.b 100000 ${backup_addr} ${filesize}
832 *
833 * Re-import this snapshot, deleting all other settings:
834 *
835 * => env import -d -t ${backup_addr}
836 */
Igor Grinbergde832562011-11-07 01:13:59 +0000837static int do_env_export(cmd_tbl_t *cmdtp, int flag,
838 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200839{
840 char buf[32];
841 char *addr, *cmd, *res;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100842 size_t size = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200843 ssize_t len;
844 env_t *envp;
845 char sep = '\n';
846 int chk = 0;
847 int fmt = 0;
848
849 cmd = *argv;
850
851 while (--argc > 0 && **++argv == '-') {
852 char *arg = *argv;
853 while (*++arg) {
854 switch (*arg) {
855 case 'b': /* raw binary format */
856 if (fmt++)
857 goto sep_err;
858 sep = '\0';
859 break;
860 case 'c': /* external checksum format */
861 if (fmt++)
862 goto sep_err;
863 sep = '\0';
864 chk = 1;
865 break;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100866 case 's': /* size given */
867 if (--argc <= 0)
868 return cmd_usage(cmdtp);
869 size = simple_strtoul(*++argv, NULL, 16);
870 goto NXTARG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200871 case 't': /* text format */
872 if (fmt++)
873 goto sep_err;
874 sep = '\n';
875 break;
876 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000877 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200878 }
879 }
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100880NXTARG: ;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200881 }
882
Macpaul Linff2a9f32011-04-26 16:16:45 +0000883 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000884 return CMD_RET_USAGE;
wdenk57b2d802003-06-27 21:31:46 +0000885
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200886 addr = (char *)simple_strtoul(argv[0], NULL, 16);
887
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100888 if (size)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200889 memset(addr, '\0', size);
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100890
891 argc--;
892 argv++;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200893
894 if (sep) { /* export as text file */
Wolfgang Denk565af272013-03-23 23:50:28 +0000895 len = hexport_r(&env_htab, sep,
896 H_MATCH_KEY | H_MATCH_IDENT,
897 &addr, size, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200898 if (len < 0) {
Igor Grinbergde832562011-11-07 01:13:59 +0000899 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200900 return 1;
901 }
Andreas Bießmann92b9b2e2011-02-09 15:10:29 +0100902 sprintf(buf, "%zX", (size_t)len);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200903 setenv("filesize", buf);
904
905 return 0;
906 }
907
908 envp = (env_t *)addr;
909
910 if (chk) /* export as checksum protected block */
911 res = (char *)envp->data;
912 else /* export as raw binary data */
913 res = addr;
914
Wolfgang Denk565af272013-03-23 23:50:28 +0000915 len = hexport_r(&env_htab, '\0',
916 H_MATCH_KEY | H_MATCH_IDENT,
917 &res, ENV_SIZE, argc, argv);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200918 if (len < 0) {
Igor Grinbergde832562011-11-07 01:13:59 +0000919 error("Cannot export environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200920 return 1;
921 }
922
923 if (chk) {
Igor Grinbergde832562011-11-07 01:13:59 +0000924 envp->crc = crc32(0, envp->data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200925#ifdef CONFIG_ENV_ADDR_REDUND
926 envp->flags = ACTIVE_FLAG;
927#endif
928 }
Simon Glass103789d2013-02-24 17:33:22 +0000929 setenv_hex("filesize", len + offsetof(env_t, data));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200930
931 return 0;
932
933sep_err:
Igor Grinbergde832562011-11-07 01:13:59 +0000934 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200935 return 1;
936}
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500937#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200938
Mike Frysingerf3ddf202010-12-26 23:09:45 -0500939#ifdef CONFIG_CMD_IMPORTENV
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200940/*
941 * env import [-d] [-t | -b | -c] addr [size]
942 * -d: delete existing environment before importing;
943 * otherwise overwrite / append to existion definitions
944 * -t: assume text format; either "size" must be given or the
945 * text data must be '\0' terminated
946 * -b: assume binary format ('\0' separated, "\0\0" terminated)
947 * -c: assume checksum protected environment format
948 * addr: memory address to read from
949 * size: length of input data; if missing, proper '\0'
950 * termination is mandatory
951 */
Igor Grinbergde832562011-11-07 01:13:59 +0000952static int do_env_import(cmd_tbl_t *cmdtp, int flag,
953 int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200954{
955 char *cmd, *addr;
956 char sep = '\n';
957 int chk = 0;
958 int fmt = 0;
959 int del = 0;
960 size_t size;
961
962 cmd = *argv;
963
964 while (--argc > 0 && **++argv == '-') {
965 char *arg = *argv;
966 while (*++arg) {
967 switch (*arg) {
968 case 'b': /* raw binary format */
969 if (fmt++)
970 goto sep_err;
971 sep = '\0';
972 break;
973 case 'c': /* external checksum format */
974 if (fmt++)
975 goto sep_err;
976 sep = '\0';
977 chk = 1;
978 break;
979 case 't': /* text format */
980 if (fmt++)
981 goto sep_err;
982 sep = '\n';
983 break;
984 case 'd':
985 del = 1;
986 break;
987 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000988 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200989 }
990 }
991 }
992
Macpaul Linff2a9f32011-04-26 16:16:45 +0000993 if (argc < 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000994 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200995
996 if (!fmt)
997 printf("## Warning: defaulting to text format\n");
998
999 addr = (char *)simple_strtoul(argv[0], NULL, 16);
1000
1001 if (argc == 2) {
1002 size = simple_strtoul(argv[1], NULL, 16);
1003 } else {
1004 char *s = addr;
1005
1006 size = 0;
1007
1008 while (size < MAX_ENV_SIZE) {
1009 if ((*s == sep) && (*(s+1) == '\0'))
1010 break;
1011 ++s;
1012 ++size;
1013 }
1014 if (size == MAX_ENV_SIZE) {
1015 printf("## Warning: Input data exceeds %d bytes"
1016 " - truncated\n", MAX_ENV_SIZE);
1017 }
Horst Kronstorfer295da642011-12-16 23:33:10 +00001018 size += 2;
Simon Glassc6f4e122011-11-04 06:42:36 +00001019 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001020 }
1021
1022 if (chk) {
1023 uint32_t crc;
1024 env_t *ep = (env_t *)addr;
1025
1026 size -= offsetof(env_t, data);
1027 memcpy(&crc, &ep->crc, sizeof(crc));
1028
1029 if (crc32(0, ep->data, size) != crc) {
1030 puts("## Error: bad CRC, import failed\n");
1031 return 1;
1032 }
1033 addr = (char *)ep->data;
1034 }
1035
Gerlando Falauto9b0e1ff2012-08-24 00:11:38 +00001036 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
Joe Hershbergera46f7702012-12-11 22:16:19 -06001037 0, NULL) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001038 error("Environment import failed: errno = %d\n", errno);
1039 return 1;
1040 }
1041 gd->flags |= GD_FLG_ENV_READY;
1042
1043 return 0;
1044
1045sep_err:
1046 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1047 cmd);
1048 return 1;
1049}
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001050#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001051
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001052/*
1053 * New command line interface: "env" command with subcommands
1054 */
1055static cmd_tbl_t cmd_env_sub[] = {
1056#if defined(CONFIG_CMD_ASKENV)
1057 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1058#endif
1059 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
Joe Hershberger8335ce72012-12-11 22:16:36 -06001060 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001061#if defined(CONFIG_CMD_EDITENV)
1062 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1063#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001064#if defined(CONFIG_CMD_ENV_CALLBACK)
1065 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1066#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001067#if defined(CONFIG_CMD_ENV_FLAGS)
1068 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1069#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001070#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001071 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001072#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001073#if defined(CONFIG_CMD_GREPENV)
1074 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1075#endif
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001076#if defined(CONFIG_CMD_IMPORTENV)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001077 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
Mike Frysingerf3ddf202010-12-26 23:09:45 -05001078#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001079 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1080#if defined(CONFIG_CMD_RUN)
1081 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1082#endif
1083#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1084 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1085#endif
1086 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1087};
1088
Wolfgang Denkd0813e52010-10-28 20:00:11 +02001089#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +02001090void env_reloc(void)
1091{
1092 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1093}
1094#endif
1095
Macpaul Linff2a9f32011-04-26 16:16:45 +00001096static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001097{
1098 cmd_tbl_t *cp;
1099
Thomas Weber4d282652010-11-24 13:07:52 +01001100 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001101 return CMD_RET_USAGE;
Thomas Weber4d282652010-11-24 13:07:52 +01001102
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001103 /* drop initial "env" arg */
1104 argc--;
1105 argv++;
1106
1107 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1108
1109 if (cp)
1110 return cp->cmd(cmdtp, flag, argc, argv);
1111
Simon Glassa06dfc72011-12-10 08:44:01 +00001112 return CMD_RET_USAGE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001113}
1114
Kim Phillipsdc00a682012-10-29 13:34:31 +00001115#ifdef CONFIG_SYS_LONGHELP
1116static char env_help_text[] =
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001117#if defined(CONFIG_CMD_ASKENV)
1118 "ask name [message] [size] - ask for environment variable\nenv "
1119#endif
Joe Hershberger1b0d5512012-12-11 22:16:25 -06001120#if defined(CONFIG_CMD_ENV_CALLBACK)
1121 "callbacks - print callbacks and their associated variables\nenv "
1122#endif
Gerlando Falautofe9f49d2012-08-24 00:11:41 +00001123 "default [-f] -a - [forcibly] reset default environment\n"
1124 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
Joe Hershberger8335ce72012-12-11 22:16:36 -06001125 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001126#if defined(CONFIG_CMD_EDITENV)
1127 "env edit name - edit environment variable\n"
1128#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001129#if defined(CONFIG_CMD_EXPORTENV)
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +01001130 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001131#endif
Joe Hershbergera2d62b72012-12-11 22:16:33 -06001132#if defined(CONFIG_CMD_ENV_FLAGS)
1133 "env flags - print variables that have non-default flags\n"
1134#endif
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001135#if defined(CONFIG_CMD_GREPENV)
1136 "env grep string [...] - search environment\n"
1137#endif
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001138#if defined(CONFIG_CMD_IMPORTENV)
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001139 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
Benoît Thébaudeauf7128972012-08-10 07:45:44 +00001140#endif
Joe Hershberger79a905e2012-12-11 22:16:23 -06001141 "env print [-a | name ...] - print environment\n"
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001142#if defined(CONFIG_CMD_RUN)
1143 "env run var [...] - run commands in an environment variable\n"
1144#endif
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001145#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001146 "env save - save environment\n"
Horst Kronstorferc63e9002011-12-10 02:25:19 +00001147#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001148 "env set [-f] name [arg ...]\n";
1149#endif
1150
1151U_BOOT_CMD(
1152 env, CONFIG_SYS_MAXARGS, 1, do_env,
1153 "environment handling commands", env_help_text
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001154);
1155
1156/*
1157 * Old command line interface, kept for compatibility
1158 */
wdenk57b2d802003-06-27 21:31:46 +00001159
Peter Tyser0deafa22009-10-25 15:12:56 -05001160#if defined(CONFIG_CMD_EDITENV)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001161U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001162 editenv, 2, 0, do_env_edit,
Peter Tyser0deafa22009-10-25 15:12:56 -05001163 "edit environment variable",
1164 "name\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001165 " - edit environment variable 'name'",
1166 var_complete
Peter Tyser0deafa22009-10-25 15:12:56 -05001167);
1168#endif
1169
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001170U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001171 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001172 "print environment variables",
Joe Hershberger79a905e2012-12-11 22:16:23 -06001173 "[-a]\n - print [all] values of all environment variables\n"
wdenk57b2d802003-06-27 21:31:46 +00001174 "printenv name ...\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001175 " - print value of environment variable 'name'",
1176 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001177);
Kim Phillipsf0c9d532011-04-05 07:15:14 +00001178
1179#ifdef CONFIG_CMD_GREPENV
1180U_BOOT_CMD_COMPLETE(
1181 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1182 "search environment variables",
1183 "string ...\n"
1184 " - list environment name=value pairs matching 'string'",
1185 var_complete
1186);
1187#endif
wdenk57b2d802003-06-27 21:31:46 +00001188
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001189U_BOOT_CMD_COMPLETE(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001190 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001191 "set environment variables",
Joe Hershbergerf64bb772012-12-11 22:16:35 -06001192 "[-f] name value ...\n"
1193 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1194 "setenv [-f] name\n"
1195 " - [forcibly] delete environment variable 'name'",
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001196 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001197);
1198
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001199#if defined(CONFIG_CMD_ASKENV)
wdenk57b2d802003-06-27 21:31:46 +00001200
wdenkf287a242003-07-01 21:06:45 +00001201U_BOOT_CMD(
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001202 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001203 "get environment variables from stdin",
wdenk57b2d802003-06-27 21:31:46 +00001204 "name [message] [size]\n"
Wolfgang Denk586895d2013-02-20 04:53:16 +00001205 " - get environment variable 'name' from stdin (max 'size' chars)"
wdenk57b2d802003-06-27 21:31:46 +00001206);
Jon Loeligerd704d912007-07-10 11:02:44 -05001207#endif
wdenk57b2d802003-06-27 21:31:46 +00001208
Jon Loeligerd76b5c12007-07-08 18:02:23 -05001209#if defined(CONFIG_CMD_RUN)
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001210U_BOOT_CMD_COMPLETE(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001211 run, CONFIG_SYS_MAXARGS, 1, do_run,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001212 "run commands in an environment variable",
wdenk57b2d802003-06-27 21:31:46 +00001213 "var [...]\n"
Mike Frysingerb5bc05e2010-10-20 03:52:39 -04001214 " - run the commands in the environment variable(s) 'var'",
1215 var_complete
wdenk57b2d802003-06-27 21:31:46 +00001216);
Jon Loeligerd704d912007-07-10 11:02:44 -05001217#endif
Ilya Yanokf7a2c552012-09-18 00:22:50 +00001218#endif /* CONFIG_SPL_BUILD */