]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_nvedit.c
env: Add environment variable flags
[people/ms/u-boot.git] / common / cmd_nvedit.c
CommitLineData
a68d3ed0 1/*
ea882baf 2 * (C) Copyright 2000-2010
a68d3ed0
WD
3 * 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>
a000b795
KP
7 *
8 * Copyright 2011 Freescale Semiconductor, Inc.
9 *
a68d3ed0
WD
10 * 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
ea882baf 29/*
a68d3ed0
WD
30 * Support for persistent environment data
31 *
ea882baf
WD
32 * 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.
a68d3ed0 37 *
ea882baf
WD
38 * 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.
a68d3ed0
WD
41 */
42
43#include <common.h>
44#include <command.h>
45#include <environment.h>
ea882baf
WD
46#include <search.h>
47#include <errno.h>
246c6922 48#include <malloc.h>
2a3cb020 49#include <watchdog.h>
a68d3ed0
WD
50#include <linux/stddef.h>
51#include <asm/byteorder.h>
a68d3ed0 52
d87080b7
WD
53DECLARE_GLOBAL_DATA_PTR;
54
f3c615b8
ML
55#if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
56 !defined(CONFIG_ENV_IS_IN_FLASH) && \
57 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
f3c615b8 58 !defined(CONFIG_ENV_IS_IN_MMC) && \
57210c7c 59 !defined(CONFIG_ENV_IS_IN_FAT) && \
f3c615b8
ML
60 !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) && \
0a85a9e7 64 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
f3c615b8 65 !defined(CONFIG_ENV_IS_NOWHERE)
75eb82ec 66# error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
b37d41aa 67SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
a68d3ed0
WD
68#endif
69
ea882baf
WD
70/*
71 * Maximum expected input data size for import command
72 */
73#define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
a68d3ed0 74
da95427c 75/*
ea882baf 76 * This variable is incremented on each do_env_set(), so it can
da95427c
HS
77 * 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 */
2f70c49e 82static int env_id = 1;
a68d3ed0 83
f3c615b8 84int get_env_id(void)
2f70c49e
HS
85{
86 return env_id;
87}
a68d3ed0 88
7ac2fe2d 89#ifndef CONFIG_SPL_BUILD
4c94f6c5 90/*
ea882baf
WD
91 * Command interface: print one or all environment variables
92 *
93 * Returns 0 in case of error, or length of printed string
4c94f6c5 94 */
be11235a 95static int env_print(char *name, int flag)
a68d3ed0 96{
ea882baf
WD
97 char *res = NULL;
98 size_t len;
99
100 if (name) { /* print a single name */
101 ENTRY e, *ep;
102
103 e.key = name;
104 e.data = NULL;
be11235a 105 hsearch_r(e, FIND, &ep, &env_htab, flag);
ea882baf
WD
106 if (ep == NULL)
107 return 0;
f3c615b8 108 len = printf("%s=%s\n", ep->key, ep->data);
ea882baf
WD
109 return len;
110 }
a68d3ed0 111
ea882baf 112 /* print whole list */
be11235a 113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
a68d3ed0 114
ea882baf
WD
115 if (len > 0) {
116 puts(res);
117 free(res);
118 return len;
a68d3ed0
WD
119 }
120
ea882baf
WD
121 /* should never happen */
122 return 0;
4c94f6c5 123}
a68d3ed0 124
088f1b19
KP
125static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
126 char * const argv[])
4c94f6c5
MF
127{
128 int i;
129 int rcode = 0;
be11235a
JH
130 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 }
a68d3ed0 137
4c94f6c5
MF
138 if (argc == 1) {
139 /* print all env vars */
be11235a 140 rcode = env_print(NULL, env_flag);
ea882baf 141 if (!rcode)
4c94f6c5
MF
142 return 1;
143 printf("\nEnvironment size: %d/%ld bytes\n",
144 rcode, (ulong)ENV_SIZE);
145 return 0;
146 }
a68d3ed0 147
4c94f6c5 148 /* print selected env vars */
be11235a 149 env_flag &= ~H_HIDE_DOT;
4c94f6c5 150 for (i = 1; i < argc; ++i) {
be11235a 151 int rc = env_print(argv[i], env_flag);
ea882baf
WD
152 if (!rc) {
153 printf("## Error: \"%s\" not defined\n", argv[i]);
4c94f6c5 154 ++rcode;
a68d3ed0
WD
155 }
156 }
4c94f6c5 157
a68d3ed0
WD
158 return rcode;
159}
160
a000b795 161#ifdef CONFIG_CMD_GREPENV
d09b1787
IG
162static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
163 int argc, char * const argv[])
a000b795
KP
164{
165 ENTRY *match;
166 unsigned char matched[env_htab.size / 8];
167 int rcode = 1, arg = 1, idx;
168
169 if (argc < 2)
4c12eeb8 170 return CMD_RET_USAGE;
a000b795
KP
171
172 memset(matched, 0, env_htab.size / 8);
173
174 while (arg <= argc) {
175 idx = 0;
068f158f 176 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
a000b795
KP
177 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
7ac2fe2d 192#endif /* CONFIG_SPL_BUILD */
a000b795 193
c3f65258
GF
194/*
195 * Set a new environment variable,
196 * or replace or delete an existing one.
2598090b 197 */
088f1b19 198static int _do_env_set(int flag, int argc, char * const argv[])
c3f65258
GF
199{
200 int i, len;
201 char *name, *value, *s;
202 ENTRY e, *ep;
203
204 name = argv[1];
205 value = argv[2];
206
207 if (strchr(name, '=')) {
208 printf("## Error: illegal character '='"
209 "in variable name \"%s\"\n", name);
210 return 1;
211 }
212
213 env_id++;
c3f65258 214
a68d3ed0 215 /* Delete only ? */
d09b1787 216 if (argc < 3 || argv[2] == NULL) {
c4e0057f 217 int rc = hdelete_r(name, &env_htab, H_INTERACTIVE);
ea882baf 218 return !rc;
a68d3ed0
WD
219 }
220
221 /*
ea882baf 222 * Insert / replace new value
a68d3ed0 223 */
f3c615b8 224 for (i = 2, len = 0; i < argc; ++i)
a68d3ed0 225 len += strlen(argv[i]) + 1;
f3c615b8
ML
226
227 value = malloc(len);
228 if (value == NULL) {
ea882baf 229 printf("## Can't malloc %d bytes\n", len);
a68d3ed0
WD
230 return 1;
231 }
f3c615b8 232 for (i = 2, s = value; i < argc; ++i) {
ea882baf 233 char *v = argv[i];
a68d3ed0 234
ea882baf 235 while ((*s++ = *v++) != '\0')
a68d3ed0 236 ;
d09b1787 237 *(s - 1) = ' ';
ea882baf
WD
238 }
239 if (s != value)
240 *--s = '\0';
241
d09b1787
IG
242 e.key = name;
243 e.data = value;
c4e0057f 244 hsearch_r(e, ENTER, &ep, &env_htab, H_INTERACTIVE);
ea882baf
WD
245 free(value);
246 if (!ep) {
247 printf("## Error inserting \"%s\" variable, errno=%d\n",
248 name, errno);
249 return 1;
a68d3ed0 250 }
a68d3ed0 251
a68d3ed0
WD
252 return 0;
253}
254
84b5e802 255int setenv(const char *varname, const char *varvalue)
a68d3ed0 256{
84b5e802
WD
257 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
258
d09b1787 259 if (varvalue == NULL || varvalue[0] == '\0')
84b5e802 260 return _do_env_set(0, 2, (char * const *)argv);
9ffd451a 261 else
84b5e802 262 return _do_env_set(0, 3, (char * const *)argv);
a68d3ed0
WD
263}
264
d67f10ce
SG
265/**
266 * Set an environment variable to an integer value
267 *
268 * @param varname Environmet variable to set
269 * @param value Value to set it to
270 * @return 0 if ok, 1 on error
271 */
272int setenv_ulong(const char *varname, ulong value)
273{
274 /* TODO: this should be unsigned */
275 char *str = simple_itoa(value);
276
277 return setenv(varname, str);
278}
279
280/**
281 * Set an environment variable to an address in hex
282 *
283 * @param varname Environmet variable to set
284 * @param addr Value to set it to
285 * @return 0 if ok, 1 on error
286 */
287int setenv_addr(const char *varname, const void *addr)
288{
289 char str[17];
290
79afc88d 291 sprintf(str, "%lx", (uintptr_t)addr);
d67f10ce
SG
292 return setenv(varname, str);
293}
294
7ac2fe2d 295#ifndef CONFIG_SPL_BUILD
088f1b19 296static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a68d3ed0 297{
47e26b1b 298 if (argc < 2)
4c12eeb8 299 return CMD_RET_USAGE;
a68d3ed0 300
ea882baf 301 return _do_env_set(flag, argc, argv);
a68d3ed0
WD
302}
303
ea882baf 304/*
a68d3ed0
WD
305 * Prompt for environment variable
306 */
c76fe474 307#if defined(CONFIG_CMD_ASKENV)
f3c615b8 308int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a68d3ed0 309{
6d0f6bcf
JCPV
310 char message[CONFIG_SYS_CBSIZE];
311 int size = CONFIG_SYS_CBSIZE - 1;
ea882baf 312 int i, len, pos;
a68d3ed0
WD
313 char *local_args[4];
314
315 local_args[0] = argv[0];
316 local_args[1] = argv[1];
317 local_args[2] = NULL;
318 local_args[3] = NULL;
319
a68d3ed0
WD
320 /* Check the syntax */
321 switch (argc) {
322 case 1:
4c12eeb8 323 return CMD_RET_USAGE;
a68d3ed0 324
ea882baf
WD
325 case 2: /* env_ask envname */
326 sprintf(message, "Please enter '%s':", argv[1]);
a68d3ed0
WD
327 break;
328
ea882baf
WD
329 case 3: /* env_ask envname size */
330 sprintf(message, "Please enter '%s':", argv[1]);
331 size = simple_strtoul(argv[2], NULL, 10);
a68d3ed0
WD
332 break;
333
ea882baf 334 default: /* env_ask envname message1 ... messagen size */
f3c615b8
ML
335 for (i = 2, pos = 0; i < argc - 1; i++) {
336 if (pos)
a68d3ed0 337 message[pos++] = ' ';
f3c615b8 338
d09b1787 339 strcpy(message + pos, argv[i]);
a68d3ed0
WD
340 pos += strlen(argv[i]);
341 }
d09b1787 342
a68d3ed0 343 message[pos] = '\0';
ea882baf 344 size = simple_strtoul(argv[argc - 1], NULL, 10);
a68d3ed0
WD
345 break;
346 }
347
6d0f6bcf
JCPV
348 if (size >= CONFIG_SYS_CBSIZE)
349 size = CONFIG_SYS_CBSIZE - 1;
a68d3ed0
WD
350
351 if (size <= 0)
352 return 1;
353
354 /* prompt for input */
ea882baf 355 len = readline(message);
a68d3ed0
WD
356
357 if (size < len)
358 console_buffer[size] = '\0';
359
360 len = 2;
361 if (console_buffer[0] != '\0') {
362 local_args[2] = console_buffer;
363 len = 3;
364 }
365
366 /* Continue calling setenv code */
ea882baf 367 return _do_env_set(flag, len, local_args);
a68d3ed0 368}
90253178 369#endif
a68d3ed0 370
5e2b3e0c
JH
371#if defined(CONFIG_CMD_ENV_CALLBACK)
372static int print_static_binding(const char *var_name, const char *callback_name)
373{
374 printf("\t%-20s %-20s\n", var_name, callback_name);
375
376 return 0;
377}
378
379static int print_active_callback(ENTRY *entry)
380{
381 struct env_clbk_tbl *clbkp;
382 int i;
383 int num_callbacks;
384
385 if (entry->callback == NULL)
386 return 0;
387
388 /* look up the callback in the linker-list */
389 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
390 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
391 i < num_callbacks;
392 i++, clbkp++) {
393#if defined(CONFIG_NEEDS_MANUAL_RELOC)
394 if (entry->callback == clbkp->callback + gd->reloc_off)
395#else
396 if (entry->callback == clbkp->callback)
397#endif
398 break;
399 }
400
401 if (i == num_callbacks)
402 /* this should probably never happen, but just in case... */
403 printf("\t%-20s %p\n", entry->key, entry->callback);
404 else
405 printf("\t%-20s %-20s\n", entry->key, clbkp->name);
406
407 return 0;
408}
409
410/*
411 * Print the callbacks available and what they are bound to
412 */
413int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
414{
415 struct env_clbk_tbl *clbkp;
416 int i;
417 int num_callbacks;
418
419 /* Print the available callbacks */
420 puts("Available callbacks:\n");
421 puts("\tCallback Name\n");
422 puts("\t-------------\n");
423 num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
424 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
425 i < num_callbacks;
426 i++, clbkp++)
427 printf("\t%s\n", clbkp->name);
428 puts("\n");
429
430 /* Print the static bindings that may exist */
431 puts("Static callback bindings:\n");
432 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
433 printf("\t%-20s %-20s\n", "-------------", "-------------");
434 env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
435 puts("\n");
436
437 /* walk through each variable and print the callback if it has one */
438 puts("Active callback bindings:\n");
439 printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
440 printf("\t%-20s %-20s\n", "-------------", "-------------");
441 hwalk_r(&env_htab, print_active_callback);
442 return 0;
443}
444#endif
445
ea882baf 446/*
246c6922
PT
447 * Interactively edit an environment variable
448 */
449#if defined(CONFIG_CMD_EDITENV)
088f1b19
KP
450static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
451 char * const argv[])
246c6922
PT
452{
453 char buffer[CONFIG_SYS_CBSIZE];
454 char *init_val;
246c6922 455
47e26b1b 456 if (argc < 2)
4c12eeb8 457 return CMD_RET_USAGE;
246c6922
PT
458
459 /* Set read buffer to initial value or empty sting */
460 init_val = getenv(argv[1]);
461 if (init_val)
7fcd9bbd 462 sprintf(buffer, "%s", init_val);
246c6922
PT
463 else
464 buffer[0] = '\0';
465
9c348311 466 readline_into_buffer("edit: ", buffer, 0);
246c6922
PT
467
468 return setenv(argv[1], buffer);
469}
470#endif /* CONFIG_CMD_EDITENV */
7ac2fe2d 471#endif /* CONFIG_SPL_BUILD */
246c6922 472
ea882baf 473/*
a68d3ed0
WD
474 * Look up variable from environment,
475 * return address of storage for that variable,
476 * or NULL if not found
477 */
84b5e802 478char *getenv(const char *name)
a68d3ed0 479{
d09b1787 480 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
ea882baf 481 ENTRY e, *ep;
a68d3ed0 482
91a76751 483 WATCHDOG_RESET();
2a3cb020 484
d09b1787
IG
485 e.key = name;
486 e.data = NULL;
c4e0057f 487 hsearch_r(e, FIND, &ep, &env_htab, 0);
91a76751 488
f3c615b8 489 return ep ? ep->data : NULL;
a68d3ed0
WD
490 }
491
ea882baf 492 /* restricted capabilities before import */
ea882baf
WD
493 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
494 return (char *)(gd->env_buf);
91a76751 495
ea882baf 496 return NULL;
a68d3ed0
WD
497}
498
ea882baf
WD
499/*
500 * Look up variable from environment for restricted C runtime env.
501 */
84b5e802 502int getenv_f(const char *name, char *buf, unsigned len)
a68d3ed0
WD
503{
504 int i, nxt;
505
d09b1787 506 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
a68d3ed0
WD
507 int val, n;
508
f3c615b8
ML
509 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
510 if (nxt >= CONFIG_ENV_SIZE)
511 return -1;
a68d3ed0 512 }
f3c615b8
ML
513
514 val = envmatch((uchar *)name, i);
515 if (val < 0)
a68d3ed0 516 continue;
9ed4a958 517
a68d3ed0 518 /* found; copy out */
f3c615b8 519 for (n = 0; n < len; ++n, ++buf) {
d09b1787
IG
520 *buf = env_get_char(val++);
521 if (*buf == '\0')
9ed4a958
WD
522 return n;
523 }
524
525 if (n)
526 *--buf = '\0';
527
a02a884b
WD
528 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
529 len, name);
9ed4a958
WD
530
531 return n;
a68d3ed0 532 }
d09b1787 533
f3c615b8 534 return -1;
a68d3ed0
WD
535}
536
4a9b4131
SG
537/**
538 * Decode the integer value of an environment variable and return it.
539 *
540 * @param name Name of environemnt variable
541 * @param base Number base to use (normally 10, or 16 for hex)
542 * @param default_val Default value to return if the variable is not
543 * found
544 * @return the decoded value, or default_val if not found
545 */
546ulong getenv_ulong(const char *name, int base, ulong default_val)
547{
548 /*
549 * We can use getenv() here, even before relocation, since the
550 * environment variable value is an integer and thus short.
551 */
552 const char *str = getenv(name);
553
554 return str ? simple_strtoul(str, NULL, base) : default_val;
555}
556
7ac2fe2d 557#ifndef CONFIG_SPL_BUILD
bdab39d3 558#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
088f1b19
KP
559static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
560 char * const argv[])
a68d3ed0 561{
f3c615b8 562 printf("Saving Environment to %s...\n", env_name_spec);
a68d3ed0 563
f3c615b8 564 return saveenv() ? 1 : 0;
a68d3ed0 565}
8bde7f77 566
ba69dc26 567U_BOOT_CMD(
ea882baf 568 saveenv, 1, 0, do_env_save,
2fb2604d 569 "save environment variables to persistent storage",
a89c33db 570 ""
ba69dc26 571);
a68d3ed0 572#endif
7ac2fe2d 573#endif /* CONFIG_SPL_BUILD */
a68d3ed0
WD
574
575
ea882baf 576/*
a68d3ed0
WD
577 * Match a name / name=value pair
578 *
579 * s1 is either a simple 'name', or a 'name=value' pair.
580 * i2 is the environment index for a 'name2=value2' pair.
d09b1787 581 * If the names match, return the index for the value2, else -1.
a68d3ed0 582 */
f3c615b8 583int envmatch(uchar *s1, int i2)
a68d3ed0 584{
586197df
JH
585 if (s1 == NULL)
586 return -1;
587
a68d3ed0
WD
588 while (*s1 == env_get_char(i2++))
589 if (*s1++ == '=')
f3c615b8 590 return i2;
d09b1787 591
a68d3ed0 592 if (*s1 == '\0' && env_get_char(i2-1) == '=')
f3c615b8 593 return i2;
d09b1787 594
f3c615b8 595 return -1;
a68d3ed0 596}
8bde7f77 597
7ac2fe2d 598#ifndef CONFIG_SPL_BUILD
b64b7c3d 599static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
d09b1787 600 int argc, char * const argv[])
ea882baf 601{
b64b7c3d 602 int all = 0, flag = 0;
f3c615b8 603
b64b7c3d
GF
604 debug("Initial value for argc=%d\n", argc);
605 while (--argc > 0 && **++argv == '-') {
606 char *arg = *argv;
607
608 while (*++arg) {
609 switch (*arg) {
610 case 'a': /* default all */
611 all = 1;
612 break;
613 case 'f': /* force */
614 flag |= H_FORCE;
615 break;
616 default:
617 return cmd_usage(cmdtp);
618 }
619 }
620 }
621 debug("Final value for argc=%d\n", argc);
622 if (all && (argc == 0)) {
623 /* Reset the whole environment */
624 set_default_env("## Resetting to default environment\n");
625 return 0;
626 }
627 if (!all && (argc > 0)) {
628 /* Reset individual variables */
629 set_default_vars(argc, argv);
630 return 0;
631 }
632
633 return cmd_usage(cmdtp);
ea882baf
WD
634}
635
d09b1787
IG
636static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
637 int argc, char * const argv[])
ea882baf
WD
638{
639 printf("Not implemented yet\n");
640 return 0;
641}
642
0c79cda0 643#ifdef CONFIG_CMD_EXPORTENV
ea882baf 644/*
37f2fe74 645 * env export [-t | -b | -c] [-s size] addr [var ...]
ea882baf
WD
646 * -t: export as text format; if size is given, data will be
647 * padded with '\0' bytes; if not, one terminating '\0'
648 * will be added (which is included in the "filesize"
649 * setting so you can for exmple copy this to flash and
650 * keep the termination).
651 * -b: export as binary format (name=value pairs separated by
652 * '\0', list end marked by double "\0\0")
653 * -c: export as checksum protected environment format as
654 * used for example by "saveenv" command
37f2fe74
WD
655 * -s size:
656 * size of output buffer
ea882baf 657 * addr: memory address where environment gets stored
37f2fe74
WD
658 * var... List of variable names that get included into the
659 * export. Without arguments, the whole environment gets
660 * exported.
ea882baf
WD
661 *
662 * With "-c" and size is NOT given, then the export command will
663 * format the data as currently used for the persistent storage,
664 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
665 * prepend a valid CRC32 checksum and, in case of resundant
666 * environment, a "current" redundancy flag. If size is given, this
667 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
668 * checksum and redundancy flag will be inserted.
669 *
670 * With "-b" and "-t", always only the real data (including a
671 * terminating '\0' byte) will be written; here the optional size
672 * argument will be used to make sure not to overflow the user
673 * provided buffer; the command will abort if the size is not
674 * sufficient. Any remainign space will be '\0' padded.
675 *
676 * On successful return, the variable "filesize" will be set.
677 * Note that filesize includes the trailing/terminating '\0' byte(s).
678 *
679 * Usage szenario: create a text snapshot/backup of the current settings:
680 *
681 * => env export -t 100000
682 * => era ${backup_addr} +${filesize}
683 * => cp.b 100000 ${backup_addr} ${filesize}
684 *
685 * Re-import this snapshot, deleting all other settings:
686 *
687 * => env import -d -t ${backup_addr}
688 */
d09b1787
IG
689static int do_env_export(cmd_tbl_t *cmdtp, int flag,
690 int argc, char * const argv[])
ea882baf
WD
691{
692 char buf[32];
693 char *addr, *cmd, *res;
37f2fe74 694 size_t size = 0;
ea882baf
WD
695 ssize_t len;
696 env_t *envp;
697 char sep = '\n';
698 int chk = 0;
699 int fmt = 0;
700
701 cmd = *argv;
702
703 while (--argc > 0 && **++argv == '-') {
704 char *arg = *argv;
705 while (*++arg) {
706 switch (*arg) {
707 case 'b': /* raw binary format */
708 if (fmt++)
709 goto sep_err;
710 sep = '\0';
711 break;
712 case 'c': /* external checksum format */
713 if (fmt++)
714 goto sep_err;
715 sep = '\0';
716 chk = 1;
717 break;
37f2fe74
WD
718 case 's': /* size given */
719 if (--argc <= 0)
720 return cmd_usage(cmdtp);
721 size = simple_strtoul(*++argv, NULL, 16);
722 goto NXTARG;
ea882baf
WD
723 case 't': /* text format */
724 if (fmt++)
725 goto sep_err;
726 sep = '\n';
727 break;
728 default:
4c12eeb8 729 return CMD_RET_USAGE;
ea882baf
WD
730 }
731 }
37f2fe74 732NXTARG: ;
ea882baf
WD
733 }
734
f3c615b8 735 if (argc < 1)
4c12eeb8 736 return CMD_RET_USAGE;
8bde7f77 737
ea882baf
WD
738 addr = (char *)simple_strtoul(argv[0], NULL, 16);
739
37f2fe74 740 if (size)
ea882baf 741 memset(addr, '\0', size);
37f2fe74
WD
742
743 argc--;
744 argv++;
ea882baf
WD
745
746 if (sep) { /* export as text file */
be11235a 747 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
ea882baf 748 if (len < 0) {
d09b1787 749 error("Cannot export environment: errno = %d\n", errno);
ea882baf
WD
750 return 1;
751 }
8c3aff52 752 sprintf(buf, "%zX", (size_t)len);
ea882baf
WD
753 setenv("filesize", buf);
754
755 return 0;
756 }
757
758 envp = (env_t *)addr;
759
760 if (chk) /* export as checksum protected block */
761 res = (char *)envp->data;
762 else /* export as raw binary data */
763 res = addr;
764
be11235a 765 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
ea882baf 766 if (len < 0) {
d09b1787 767 error("Cannot export environment: errno = %d\n", errno);
ea882baf
WD
768 return 1;
769 }
770
771 if (chk) {
d09b1787 772 envp->crc = crc32(0, envp->data, ENV_SIZE);
ea882baf
WD
773#ifdef CONFIG_ENV_ADDR_REDUND
774 envp->flags = ACTIVE_FLAG;
775#endif
776 }
f3c615b8 777 sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
ea882baf
WD
778 setenv("filesize", buf);
779
780 return 0;
781
782sep_err:
d09b1787 783 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
ea882baf
WD
784 return 1;
785}
0c79cda0 786#endif
ea882baf 787
0c79cda0 788#ifdef CONFIG_CMD_IMPORTENV
ea882baf
WD
789/*
790 * env import [-d] [-t | -b | -c] addr [size]
791 * -d: delete existing environment before importing;
792 * otherwise overwrite / append to existion definitions
793 * -t: assume text format; either "size" must be given or the
794 * text data must be '\0' terminated
795 * -b: assume binary format ('\0' separated, "\0\0" terminated)
796 * -c: assume checksum protected environment format
797 * addr: memory address to read from
798 * size: length of input data; if missing, proper '\0'
799 * termination is mandatory
800 */
d09b1787
IG
801static int do_env_import(cmd_tbl_t *cmdtp, int flag,
802 int argc, char * const argv[])
ea882baf
WD
803{
804 char *cmd, *addr;
805 char sep = '\n';
806 int chk = 0;
807 int fmt = 0;
808 int del = 0;
809 size_t size;
810
811 cmd = *argv;
812
813 while (--argc > 0 && **++argv == '-') {
814 char *arg = *argv;
815 while (*++arg) {
816 switch (*arg) {
817 case 'b': /* raw binary format */
818 if (fmt++)
819 goto sep_err;
820 sep = '\0';
821 break;
822 case 'c': /* external checksum format */
823 if (fmt++)
824 goto sep_err;
825 sep = '\0';
826 chk = 1;
827 break;
828 case 't': /* text format */
829 if (fmt++)
830 goto sep_err;
831 sep = '\n';
832 break;
833 case 'd':
834 del = 1;
835 break;
836 default:
4c12eeb8 837 return CMD_RET_USAGE;
ea882baf
WD
838 }
839 }
840 }
841
f3c615b8 842 if (argc < 1)
4c12eeb8 843 return CMD_RET_USAGE;
ea882baf
WD
844
845 if (!fmt)
846 printf("## Warning: defaulting to text format\n");
847
848 addr = (char *)simple_strtoul(argv[0], NULL, 16);
849
850 if (argc == 2) {
851 size = simple_strtoul(argv[1], NULL, 16);
852 } else {
853 char *s = addr;
854
855 size = 0;
856
857 while (size < MAX_ENV_SIZE) {
858 if ((*s == sep) && (*(s+1) == '\0'))
859 break;
860 ++s;
861 ++size;
862 }
863 if (size == MAX_ENV_SIZE) {
864 printf("## Warning: Input data exceeds %d bytes"
865 " - truncated\n", MAX_ENV_SIZE);
866 }
d3f80c77 867 size += 2;
79afc88d 868 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
ea882baf
WD
869 }
870
871 if (chk) {
872 uint32_t crc;
873 env_t *ep = (env_t *)addr;
874
875 size -= offsetof(env_t, data);
876 memcpy(&crc, &ep->crc, sizeof(crc));
877
878 if (crc32(0, ep->data, size) != crc) {
879 puts("## Error: bad CRC, import failed\n");
880 return 1;
881 }
882 addr = (char *)ep->data;
883 }
884
348b1f1c 885 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
c4e0057f 886 0, NULL) == 0) {
ea882baf
WD
887 error("Environment import failed: errno = %d\n", errno);
888 return 1;
889 }
890 gd->flags |= GD_FLG_ENV_READY;
891
892 return 0;
893
894sep_err:
895 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
896 cmd);
897 return 1;
898}
0c79cda0 899#endif
ea882baf 900
ea882baf
WD
901/*
902 * New command line interface: "env" command with subcommands
903 */
904static cmd_tbl_t cmd_env_sub[] = {
905#if defined(CONFIG_CMD_ASKENV)
906 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
907#endif
908 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
909 U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
910#if defined(CONFIG_CMD_EDITENV)
911 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
912#endif
5e2b3e0c
JH
913#if defined(CONFIG_CMD_ENV_CALLBACK)
914 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
915#endif
0c79cda0 916#if defined(CONFIG_CMD_EXPORTENV)
ea882baf 917 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
0c79cda0 918#endif
a000b795
KP
919#if defined(CONFIG_CMD_GREPENV)
920 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
921#endif
0c79cda0 922#if defined(CONFIG_CMD_IMPORTENV)
ea882baf 923 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
0c79cda0 924#endif
ea882baf
WD
925 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
926#if defined(CONFIG_CMD_RUN)
927 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
928#endif
929#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
930 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
931#endif
932 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
933};
934
2e5167cc 935#if defined(CONFIG_NEEDS_MANUAL_RELOC)
60f7da1f
HS
936void env_reloc(void)
937{
938 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
939}
940#endif
941
f3c615b8 942static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea882baf
WD
943{
944 cmd_tbl_t *cp;
945
5904da02 946 if (argc < 2)
4c12eeb8 947 return CMD_RET_USAGE;
5904da02 948
ea882baf
WD
949 /* drop initial "env" arg */
950 argc--;
951 argv++;
952
953 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
954
955 if (cp)
956 return cp->cmd(cmdtp, flag, argc, argv);
957
4c12eeb8 958 return CMD_RET_USAGE;
ea882baf
WD
959}
960
088f1b19
KP
961#ifdef CONFIG_SYS_LONGHELP
962static char env_help_text[] =
ea882baf
WD
963#if defined(CONFIG_CMD_ASKENV)
964 "ask name [message] [size] - ask for environment variable\nenv "
5e2b3e0c
JH
965#endif
966#if defined(CONFIG_CMD_ENV_CALLBACK)
967 "callbacks - print callbacks and their associated variables\nenv "
ea882baf 968#endif
b64b7c3d
GF
969 "default [-f] -a - [forcibly] reset default environment\n"
970 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
ea882baf
WD
971#if defined(CONFIG_CMD_EDITENV)
972 "env edit name - edit environment variable\n"
973#endif
4796bc4b 974#if defined(CONFIG_CMD_EXPORTENV)
37f2fe74 975 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
4796bc4b 976#endif
a000b795
KP
977#if defined(CONFIG_CMD_GREPENV)
978 "env grep string [...] - search environment\n"
979#endif
4796bc4b 980#if defined(CONFIG_CMD_IMPORTENV)
a000b795 981 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
4796bc4b 982#endif
be11235a 983 "env print [-a | name ...] - print environment\n"
ea882baf
WD
984#if defined(CONFIG_CMD_RUN)
985 "env run var [...] - run commands in an environment variable\n"
986#endif
d798a9b5 987#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
ea882baf 988 "env save - save environment\n"
d798a9b5 989#endif
088f1b19
KP
990 "env set [-f] name [arg ...]\n";
991#endif
992
993U_BOOT_CMD(
994 env, CONFIG_SYS_MAXARGS, 1, do_env,
995 "environment handling commands", env_help_text
ea882baf
WD
996);
997
998/*
999 * Old command line interface, kept for compatibility
1000 */
8bde7f77 1001
246c6922 1002#if defined(CONFIG_CMD_EDITENV)
722b061b 1003U_BOOT_CMD_COMPLETE(
ea882baf 1004 editenv, 2, 0, do_env_edit,
246c6922
PT
1005 "edit environment variable",
1006 "name\n"
722b061b
MF
1007 " - edit environment variable 'name'",
1008 var_complete
246c6922
PT
1009);
1010#endif
1011
722b061b 1012U_BOOT_CMD_COMPLETE(
ea882baf 1013 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
2fb2604d 1014 "print environment variables",
be11235a 1015 "[-a]\n - print [all] values of all environment variables\n"
8bde7f77 1016 "printenv name ...\n"
722b061b
MF
1017 " - print value of environment variable 'name'",
1018 var_complete
8bde7f77
WD
1019);
1020
a000b795
KP
1021#ifdef CONFIG_CMD_GREPENV
1022U_BOOT_CMD_COMPLETE(
1023 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1024 "search environment variables",
1025 "string ...\n"
1026 " - list environment name=value pairs matching 'string'",
1027 var_complete
1028);
1029#endif
1030
722b061b 1031U_BOOT_CMD_COMPLETE(
ea882baf 1032 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
2fb2604d 1033 "set environment variables",
8bde7f77
WD
1034 "name value ...\n"
1035 " - set environment variable 'name' to 'value ...'\n"
1036 "setenv name\n"
722b061b
MF
1037 " - delete environment variable 'name'",
1038 var_complete
8bde7f77
WD
1039);
1040
c76fe474 1041#if defined(CONFIG_CMD_ASKENV)
8bde7f77 1042
0d498393 1043U_BOOT_CMD(
ea882baf 1044 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
2fb2604d 1045 "get environment variables from stdin",
8bde7f77
WD
1046 "name [message] [size]\n"
1047 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1048 "askenv name\n"
1049 " - get environment variable 'name' from stdin\n"
1050 "askenv name size\n"
1051 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1052 "askenv name [message] size\n"
1053 " - display 'message' string and get environment variable 'name'"
a89c33db 1054 "from stdin (max 'size' chars)"
8bde7f77 1055);
90253178 1056#endif
8bde7f77 1057
c76fe474 1058#if defined(CONFIG_CMD_RUN)
722b061b 1059U_BOOT_CMD_COMPLETE(
6d0f6bcf 1060 run, CONFIG_SYS_MAXARGS, 1, do_run,
2fb2604d 1061 "run commands in an environment variable",
8bde7f77 1062 "var [...]\n"
722b061b
MF
1063 " - run the commands in the environment variable(s) 'var'",
1064 var_complete
8bde7f77 1065);
90253178 1066#endif
7ac2fe2d 1067#endif /* CONFIG_SPL_BUILD */