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