]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_nvedit.c
Tegra30: Add arch-tegra30 include files
[people/ms/u-boot.git] / common / cmd_nvedit.c
1 /*
2 * (C) Copyright 2000-2010
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>
7 *
8 * Copyright 2011 Freescale Semiconductor, Inc.
9 *
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
29 /*
30 * Support for persistent environment data
31 *
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.
37 *
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.
41 */
42
43 #include <common.h>
44 #include <command.h>
45 #include <environment.h>
46 #include <search.h>
47 #include <errno.h>
48 #include <malloc.h>
49 #include <watchdog.h>
50 #include <linux/stddef.h>
51 #include <asm/byteorder.h>
52
53 DECLARE_GLOBAL_DATA_PTR;
54
55 #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
56 !defined(CONFIG_ENV_IS_IN_FLASH) && \
57 !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
58 !defined(CONFIG_ENV_IS_IN_MMC) && \
59 !defined(CONFIG_ENV_IS_IN_FAT) && \
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) && \
64 !defined(CONFIG_ENV_IS_IN_REMOTE) && \
65 !defined(CONFIG_ENV_IS_NOWHERE)
66 # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
67 SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
68 #endif
69
70 /*
71 * Maximum expected input data size for import command
72 */
73 #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
74
75 /*
76 * This variable is incremented on each do_env_set(), so it can
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 */
82 static int env_id = 1;
83
84 int get_env_id(void)
85 {
86 return env_id;
87 }
88
89 #ifndef CONFIG_SPL_BUILD
90 /*
91 * Command interface: print one or all environment variables
92 *
93 * Returns 0 in case of error, or length of printed string
94 */
95 static int env_print(char *name, int flag)
96 {
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;
105 hsearch_r(e, FIND, &ep, &env_htab, flag);
106 if (ep == NULL)
107 return 0;
108 len = printf("%s=%s\n", ep->key, ep->data);
109 return len;
110 }
111
112 /* print whole list */
113 len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
114
115 if (len > 0) {
116 puts(res);
117 free(res);
118 return len;
119 }
120
121 /* should never happen */
122 return 0;
123 }
124
125 static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
126 char * const argv[])
127 {
128 int i;
129 int rcode = 0;
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 }
137
138 if (argc == 1) {
139 /* print all env vars */
140 rcode = env_print(NULL, env_flag);
141 if (!rcode)
142 return 1;
143 printf("\nEnvironment size: %d/%ld bytes\n",
144 rcode, (ulong)ENV_SIZE);
145 return 0;
146 }
147
148 /* print selected env vars */
149 env_flag &= ~H_HIDE_DOT;
150 for (i = 1; i < argc; ++i) {
151 int rc = env_print(argv[i], env_flag);
152 if (!rc) {
153 printf("## Error: \"%s\" not defined\n", argv[i]);
154 ++rcode;
155 }
156 }
157
158 return rcode;
159 }
160
161 #ifdef CONFIG_CMD_GREPENV
162 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
163 int argc, char * const argv[])
164 {
165 ENTRY *match;
166 unsigned char matched[env_htab.size / 8];
167 int rcode = 1, arg = 1, idx;
168
169 if (argc < 2)
170 return CMD_RET_USAGE;
171
172 memset(matched, 0, env_htab.size / 8);
173
174 while (arg <= argc) {
175 idx = 0;
176 while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
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
192 #endif /* CONFIG_SPL_BUILD */
193
194 /*
195 * Set a new environment variable,
196 * or replace or delete an existing one.
197 */
198 static int _do_env_set(int flag, int argc, char * const argv[])
199 {
200 int i, len;
201 char *name, *value, *s;
202 ENTRY e, *ep;
203 int env_flag = H_INTERACTIVE;
204
205 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);
221 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++;
231
232 /* Delete only ? */
233 if (argc < 3 || argv[2] == NULL) {
234 int rc = hdelete_r(name, &env_htab, env_flag);
235 return !rc;
236 }
237
238 /*
239 * Insert / replace new value
240 */
241 for (i = 2, len = 0; i < argc; ++i)
242 len += strlen(argv[i]) + 1;
243
244 value = malloc(len);
245 if (value == NULL) {
246 printf("## Can't malloc %d bytes\n", len);
247 return 1;
248 }
249 for (i = 2, s = value; i < argc; ++i) {
250 char *v = argv[i];
251
252 while ((*s++ = *v++) != '\0')
253 ;
254 *(s - 1) = ' ';
255 }
256 if (s != value)
257 *--s = '\0';
258
259 e.key = name;
260 e.data = value;
261 hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
262 free(value);
263 if (!ep) {
264 printf("## Error inserting \"%s\" variable, errno=%d\n",
265 name, errno);
266 return 1;
267 }
268
269 return 0;
270 }
271
272 int setenv(const char *varname, const char *varvalue)
273 {
274 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
275
276 if (varvalue == NULL || varvalue[0] == '\0')
277 return _do_env_set(0, 2, (char * const *)argv);
278 else
279 return _do_env_set(0, 3, (char * const *)argv);
280 }
281
282 /**
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 */
289 int 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 */
304 int setenv_addr(const char *varname, const void *addr)
305 {
306 char str[17];
307
308 sprintf(str, "%lx", (uintptr_t)addr);
309 return setenv(varname, str);
310 }
311
312 #ifndef CONFIG_SPL_BUILD
313 static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
314 {
315 if (argc < 2)
316 return CMD_RET_USAGE;
317
318 return _do_env_set(flag, argc, argv);
319 }
320
321 /*
322 * Prompt for environment variable
323 */
324 #if defined(CONFIG_CMD_ASKENV)
325 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
326 {
327 char message[CONFIG_SYS_CBSIZE];
328 int size = CONFIG_SYS_CBSIZE - 1;
329 int i, len, pos;
330 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
337 /* Check the syntax */
338 switch (argc) {
339 case 1:
340 return CMD_RET_USAGE;
341
342 case 2: /* env_ask envname */
343 sprintf(message, "Please enter '%s':", argv[1]);
344 break;
345
346 case 3: /* env_ask envname size */
347 sprintf(message, "Please enter '%s':", argv[1]);
348 size = simple_strtoul(argv[2], NULL, 10);
349 break;
350
351 default: /* env_ask envname message1 ... messagen size */
352 for (i = 2, pos = 0; i < argc - 1; i++) {
353 if (pos)
354 message[pos++] = ' ';
355
356 strcpy(message + pos, argv[i]);
357 pos += strlen(argv[i]);
358 }
359
360 message[pos] = '\0';
361 size = simple_strtoul(argv[argc - 1], NULL, 10);
362 break;
363 }
364
365 if (size >= CONFIG_SYS_CBSIZE)
366 size = CONFIG_SYS_CBSIZE - 1;
367
368 if (size <= 0)
369 return 1;
370
371 /* prompt for input */
372 len = readline(message);
373
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 */
384 return _do_env_set(flag, len, local_args);
385 }
386 #endif
387
388 #if defined(CONFIG_CMD_ENV_CALLBACK)
389 static 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
396 static 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 */
430 int 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
463 #if defined(CONFIG_CMD_ENV_FLAGS)
464 static int print_static_flags(const char *var_name, const char *flags)
465 {
466 enum env_flags_vartype type = env_flags_parse_vartype(flags);
467 enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
468
469 printf("\t%-20s %-20s %-20s\n", var_name,
470 env_flags_get_vartype_name(type),
471 env_flags_get_varaccess_name(access));
472
473 return 0;
474 }
475
476 static int print_active_flags(ENTRY *entry)
477 {
478 enum env_flags_vartype type;
479 enum env_flags_varaccess access;
480
481 if (entry->flags == 0)
482 return 0;
483
484 type = (enum env_flags_vartype)
485 (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
486 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));
490
491 return 0;
492 }
493
494 /*
495 * Print the flags available and what variables have flags
496 */
497 int 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
507 /* 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
515 /* Print the static flags that may exist */
516 puts("Static flags:\n");
517 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
518 "Variable Access");
519 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
520 "---------------");
521 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");
526 printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
527 "Variable Access");
528 printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
529 "---------------");
530 hwalk_r(&env_htab, print_active_flags);
531 return 0;
532 }
533 #endif
534
535 /*
536 * Interactively edit an environment variable
537 */
538 #if defined(CONFIG_CMD_EDITENV)
539 static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
540 char * const argv[])
541 {
542 char buffer[CONFIG_SYS_CBSIZE];
543 char *init_val;
544
545 if (argc < 2)
546 return CMD_RET_USAGE;
547
548 /* Set read buffer to initial value or empty sting */
549 init_val = getenv(argv[1]);
550 if (init_val)
551 sprintf(buffer, "%s", init_val);
552 else
553 buffer[0] = '\0';
554
555 readline_into_buffer("edit: ", buffer, 0);
556
557 return setenv(argv[1], buffer);
558 }
559 #endif /* CONFIG_CMD_EDITENV */
560 #endif /* CONFIG_SPL_BUILD */
561
562 /*
563 * Look up variable from environment,
564 * return address of storage for that variable,
565 * or NULL if not found
566 */
567 char *getenv(const char *name)
568 {
569 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
570 ENTRY e, *ep;
571
572 WATCHDOG_RESET();
573
574 e.key = name;
575 e.data = NULL;
576 hsearch_r(e, FIND, &ep, &env_htab, 0);
577
578 return ep ? ep->data : NULL;
579 }
580
581 /* restricted capabilities before import */
582 if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
583 return (char *)(gd->env_buf);
584
585 return NULL;
586 }
587
588 /*
589 * Look up variable from environment for restricted C runtime env.
590 */
591 int getenv_f(const char *name, char *buf, unsigned len)
592 {
593 int i, nxt;
594
595 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
596 int val, n;
597
598 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
599 if (nxt >= CONFIG_ENV_SIZE)
600 return -1;
601 }
602
603 val = envmatch((uchar *)name, i);
604 if (val < 0)
605 continue;
606
607 /* found; copy out */
608 for (n = 0; n < len; ++n, ++buf) {
609 *buf = env_get_char(val++);
610 if (*buf == '\0')
611 return n;
612 }
613
614 if (n)
615 *--buf = '\0';
616
617 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
618 len, name);
619
620 return n;
621 }
622
623 return -1;
624 }
625
626 /**
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 */
635 ulong 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
646 #ifndef CONFIG_SPL_BUILD
647 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
648 static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
649 char * const argv[])
650 {
651 printf("Saving Environment to %s...\n", env_name_spec);
652
653 return saveenv() ? 1 : 0;
654 }
655
656 U_BOOT_CMD(
657 saveenv, 1, 0, do_env_save,
658 "save environment variables to persistent storage",
659 ""
660 );
661 #endif
662 #endif /* CONFIG_SPL_BUILD */
663
664
665 /*
666 * 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.
670 * If the names match, return the index for the value2, else -1.
671 */
672 int envmatch(uchar *s1, int i2)
673 {
674 if (s1 == NULL)
675 return -1;
676
677 while (*s1 == env_get_char(i2++))
678 if (*s1++ == '=')
679 return i2;
680
681 if (*s1 == '\0' && env_get_char(i2-1) == '=')
682 return i2;
683
684 return -1;
685 }
686
687 #ifndef CONFIG_SPL_BUILD
688 static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
689 int argc, char * const argv[])
690 {
691 int all = 0, flag = 0;
692
693 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);
723 }
724
725 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
726 int argc, char * const argv[])
727 {
728 int env_flag = H_INTERACTIVE;
729 int ret = 0;
730
731 debug("Initial value for argc=%d\n", argc);
732 while (argc > 1 && **(argv + 1) == '-') {
733 char *arg = *++argv;
734
735 --argc;
736 while (*++arg) {
737 switch (*arg) {
738 case 'f': /* force */
739 env_flag |= H_FORCE;
740 break;
741 default:
742 return CMD_RET_USAGE;
743 }
744 }
745 }
746 debug("Final value for argc=%d\n", argc);
747
748 env_id++;
749
750 while (--argc > 0) {
751 char *name = *++argv;
752
753 if (!hdelete_r(name, &env_htab, env_flag))
754 ret = 1;
755 }
756
757 return ret;
758 }
759
760 #ifdef CONFIG_CMD_EXPORTENV
761 /*
762 * env export [-t | -b | -c] [-s size] addr [var ...]
763 * -t: export as text format; if size is given, data will be
764 * padded with '\0' bytes; if not, one terminating '\0'
765 * will be added (which is included in the "filesize"
766 * setting so you can for exmple copy this to flash and
767 * keep the termination).
768 * -b: export as binary format (name=value pairs separated by
769 * '\0', list end marked by double "\0\0")
770 * -c: export as checksum protected environment format as
771 * used for example by "saveenv" command
772 * -s size:
773 * size of output buffer
774 * addr: memory address where environment gets stored
775 * var... List of variable names that get included into the
776 * export. Without arguments, the whole environment gets
777 * exported.
778 *
779 * With "-c" and size is NOT given, then the export command will
780 * format the data as currently used for the persistent storage,
781 * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
782 * prepend a valid CRC32 checksum and, in case of resundant
783 * environment, a "current" redundancy flag. If size is given, this
784 * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
785 * checksum and redundancy flag will be inserted.
786 *
787 * With "-b" and "-t", always only the real data (including a
788 * terminating '\0' byte) will be written; here the optional size
789 * argument will be used to make sure not to overflow the user
790 * provided buffer; the command will abort if the size is not
791 * sufficient. Any remainign space will be '\0' padded.
792 *
793 * On successful return, the variable "filesize" will be set.
794 * Note that filesize includes the trailing/terminating '\0' byte(s).
795 *
796 * Usage szenario: create a text snapshot/backup of the current settings:
797 *
798 * => env export -t 100000
799 * => era ${backup_addr} +${filesize}
800 * => cp.b 100000 ${backup_addr} ${filesize}
801 *
802 * Re-import this snapshot, deleting all other settings:
803 *
804 * => env import -d -t ${backup_addr}
805 */
806 static int do_env_export(cmd_tbl_t *cmdtp, int flag,
807 int argc, char * const argv[])
808 {
809 char buf[32];
810 char *addr, *cmd, *res;
811 size_t size = 0;
812 ssize_t len;
813 env_t *envp;
814 char sep = '\n';
815 int chk = 0;
816 int fmt = 0;
817
818 cmd = *argv;
819
820 while (--argc > 0 && **++argv == '-') {
821 char *arg = *argv;
822 while (*++arg) {
823 switch (*arg) {
824 case 'b': /* raw binary format */
825 if (fmt++)
826 goto sep_err;
827 sep = '\0';
828 break;
829 case 'c': /* external checksum format */
830 if (fmt++)
831 goto sep_err;
832 sep = '\0';
833 chk = 1;
834 break;
835 case 's': /* size given */
836 if (--argc <= 0)
837 return cmd_usage(cmdtp);
838 size = simple_strtoul(*++argv, NULL, 16);
839 goto NXTARG;
840 case 't': /* text format */
841 if (fmt++)
842 goto sep_err;
843 sep = '\n';
844 break;
845 default:
846 return CMD_RET_USAGE;
847 }
848 }
849 NXTARG: ;
850 }
851
852 if (argc < 1)
853 return CMD_RET_USAGE;
854
855 addr = (char *)simple_strtoul(argv[0], NULL, 16);
856
857 if (size)
858 memset(addr, '\0', size);
859
860 argc--;
861 argv++;
862
863 if (sep) { /* export as text file */
864 len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
865 if (len < 0) {
866 error("Cannot export environment: errno = %d\n", errno);
867 return 1;
868 }
869 sprintf(buf, "%zX", (size_t)len);
870 setenv("filesize", buf);
871
872 return 0;
873 }
874
875 envp = (env_t *)addr;
876
877 if (chk) /* export as checksum protected block */
878 res = (char *)envp->data;
879 else /* export as raw binary data */
880 res = addr;
881
882 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
883 if (len < 0) {
884 error("Cannot export environment: errno = %d\n", errno);
885 return 1;
886 }
887
888 if (chk) {
889 envp->crc = crc32(0, envp->data, ENV_SIZE);
890 #ifdef CONFIG_ENV_ADDR_REDUND
891 envp->flags = ACTIVE_FLAG;
892 #endif
893 }
894 sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
895 setenv("filesize", buf);
896
897 return 0;
898
899 sep_err:
900 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
901 return 1;
902 }
903 #endif
904
905 #ifdef CONFIG_CMD_IMPORTENV
906 /*
907 * env import [-d] [-t | -b | -c] addr [size]
908 * -d: delete existing environment before importing;
909 * otherwise overwrite / append to existion definitions
910 * -t: assume text format; either "size" must be given or the
911 * text data must be '\0' terminated
912 * -b: assume binary format ('\0' separated, "\0\0" terminated)
913 * -c: assume checksum protected environment format
914 * addr: memory address to read from
915 * size: length of input data; if missing, proper '\0'
916 * termination is mandatory
917 */
918 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
919 int argc, char * const argv[])
920 {
921 char *cmd, *addr;
922 char sep = '\n';
923 int chk = 0;
924 int fmt = 0;
925 int del = 0;
926 size_t size;
927
928 cmd = *argv;
929
930 while (--argc > 0 && **++argv == '-') {
931 char *arg = *argv;
932 while (*++arg) {
933 switch (*arg) {
934 case 'b': /* raw binary format */
935 if (fmt++)
936 goto sep_err;
937 sep = '\0';
938 break;
939 case 'c': /* external checksum format */
940 if (fmt++)
941 goto sep_err;
942 sep = '\0';
943 chk = 1;
944 break;
945 case 't': /* text format */
946 if (fmt++)
947 goto sep_err;
948 sep = '\n';
949 break;
950 case 'd':
951 del = 1;
952 break;
953 default:
954 return CMD_RET_USAGE;
955 }
956 }
957 }
958
959 if (argc < 1)
960 return CMD_RET_USAGE;
961
962 if (!fmt)
963 printf("## Warning: defaulting to text format\n");
964
965 addr = (char *)simple_strtoul(argv[0], NULL, 16);
966
967 if (argc == 2) {
968 size = simple_strtoul(argv[1], NULL, 16);
969 } else {
970 char *s = addr;
971
972 size = 0;
973
974 while (size < MAX_ENV_SIZE) {
975 if ((*s == sep) && (*(s+1) == '\0'))
976 break;
977 ++s;
978 ++size;
979 }
980 if (size == MAX_ENV_SIZE) {
981 printf("## Warning: Input data exceeds %d bytes"
982 " - truncated\n", MAX_ENV_SIZE);
983 }
984 size += 2;
985 printf("## Info: input data size = %zu = 0x%zX\n", size, size);
986 }
987
988 if (chk) {
989 uint32_t crc;
990 env_t *ep = (env_t *)addr;
991
992 size -= offsetof(env_t, data);
993 memcpy(&crc, &ep->crc, sizeof(crc));
994
995 if (crc32(0, ep->data, size) != crc) {
996 puts("## Error: bad CRC, import failed\n");
997 return 1;
998 }
999 addr = (char *)ep->data;
1000 }
1001
1002 if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
1003 0, NULL) == 0) {
1004 error("Environment import failed: errno = %d\n", errno);
1005 return 1;
1006 }
1007 gd->flags |= GD_FLG_ENV_READY;
1008
1009 return 0;
1010
1011 sep_err:
1012 printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
1013 cmd);
1014 return 1;
1015 }
1016 #endif
1017
1018 /*
1019 * New command line interface: "env" command with subcommands
1020 */
1021 static cmd_tbl_t cmd_env_sub[] = {
1022 #if defined(CONFIG_CMD_ASKENV)
1023 U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
1024 #endif
1025 U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
1026 U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
1027 #if defined(CONFIG_CMD_EDITENV)
1028 U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
1029 #endif
1030 #if defined(CONFIG_CMD_ENV_CALLBACK)
1031 U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
1032 #endif
1033 #if defined(CONFIG_CMD_ENV_FLAGS)
1034 U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
1035 #endif
1036 #if defined(CONFIG_CMD_EXPORTENV)
1037 U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
1038 #endif
1039 #if defined(CONFIG_CMD_GREPENV)
1040 U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
1041 #endif
1042 #if defined(CONFIG_CMD_IMPORTENV)
1043 U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
1044 #endif
1045 U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
1046 #if defined(CONFIG_CMD_RUN)
1047 U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
1048 #endif
1049 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1050 U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
1051 #endif
1052 U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
1053 };
1054
1055 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1056 void env_reloc(void)
1057 {
1058 fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1059 }
1060 #endif
1061
1062 static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1063 {
1064 cmd_tbl_t *cp;
1065
1066 if (argc < 2)
1067 return CMD_RET_USAGE;
1068
1069 /* drop initial "env" arg */
1070 argc--;
1071 argv++;
1072
1073 cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
1074
1075 if (cp)
1076 return cp->cmd(cmdtp, flag, argc, argv);
1077
1078 return CMD_RET_USAGE;
1079 }
1080
1081 #ifdef CONFIG_SYS_LONGHELP
1082 static char env_help_text[] =
1083 #if defined(CONFIG_CMD_ASKENV)
1084 "ask name [message] [size] - ask for environment variable\nenv "
1085 #endif
1086 #if defined(CONFIG_CMD_ENV_CALLBACK)
1087 "callbacks - print callbacks and their associated variables\nenv "
1088 #endif
1089 "default [-f] -a - [forcibly] reset default environment\n"
1090 "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
1091 "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
1092 #if defined(CONFIG_CMD_EDITENV)
1093 "env edit name - edit environment variable\n"
1094 #endif
1095 #if defined(CONFIG_CMD_EXPORTENV)
1096 "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
1097 #endif
1098 #if defined(CONFIG_CMD_ENV_FLAGS)
1099 "env flags - print variables that have non-default flags\n"
1100 #endif
1101 #if defined(CONFIG_CMD_GREPENV)
1102 "env grep string [...] - search environment\n"
1103 #endif
1104 #if defined(CONFIG_CMD_IMPORTENV)
1105 "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
1106 #endif
1107 "env print [-a | name ...] - print environment\n"
1108 #if defined(CONFIG_CMD_RUN)
1109 "env run var [...] - run commands in an environment variable\n"
1110 #endif
1111 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1112 "env save - save environment\n"
1113 #endif
1114 "env set [-f] name [arg ...]\n";
1115 #endif
1116
1117 U_BOOT_CMD(
1118 env, CONFIG_SYS_MAXARGS, 1, do_env,
1119 "environment handling commands", env_help_text
1120 );
1121
1122 /*
1123 * Old command line interface, kept for compatibility
1124 */
1125
1126 #if defined(CONFIG_CMD_EDITENV)
1127 U_BOOT_CMD_COMPLETE(
1128 editenv, 2, 0, do_env_edit,
1129 "edit environment variable",
1130 "name\n"
1131 " - edit environment variable 'name'",
1132 var_complete
1133 );
1134 #endif
1135
1136 U_BOOT_CMD_COMPLETE(
1137 printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
1138 "print environment variables",
1139 "[-a]\n - print [all] values of all environment variables\n"
1140 "printenv name ...\n"
1141 " - print value of environment variable 'name'",
1142 var_complete
1143 );
1144
1145 #ifdef CONFIG_CMD_GREPENV
1146 U_BOOT_CMD_COMPLETE(
1147 grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
1148 "search environment variables",
1149 "string ...\n"
1150 " - list environment name=value pairs matching 'string'",
1151 var_complete
1152 );
1153 #endif
1154
1155 U_BOOT_CMD_COMPLETE(
1156 setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
1157 "set environment variables",
1158 "[-f] name value ...\n"
1159 " - [forcibly] set environment variable 'name' to 'value ...'\n"
1160 "setenv [-f] name\n"
1161 " - [forcibly] delete environment variable 'name'",
1162 var_complete
1163 );
1164
1165 #if defined(CONFIG_CMD_ASKENV)
1166
1167 U_BOOT_CMD(
1168 askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
1169 "get environment variables from stdin",
1170 "name [message] [size]\n"
1171 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1172 "askenv name\n"
1173 " - get environment variable 'name' from stdin\n"
1174 "askenv name size\n"
1175 " - get environment variable 'name' from stdin (max 'size' chars)\n"
1176 "askenv name [message] size\n"
1177 " - display 'message' string and get environment variable 'name'"
1178 "from stdin (max 'size' chars)"
1179 );
1180 #endif
1181
1182 #if defined(CONFIG_CMD_RUN)
1183 U_BOOT_CMD_COMPLETE(
1184 run, CONFIG_SYS_MAXARGS, 1, do_run,
1185 "run commands in an environment variable",
1186 "var [...]\n"
1187 " - run the commands in the environment variable(s) 'var'",
1188 var_complete
1189 );
1190 #endif
1191 #endif /* CONFIG_SPL_BUILD */