]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
a68d3ed0 | 2 | /* |
ea009d47 | 3 | * (C) Copyright 2000-2013 |
a68d3ed0 WD |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
5 | * | |
6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
7 | * Andreas Heppel <[email protected]> | |
a000b795 KP |
8 | * |
9 | * Copyright 2011 Freescale Semiconductor, Inc. | |
a68d3ed0 WD |
10 | */ |
11 | ||
ea882baf | 12 | /* |
a68d3ed0 WD |
13 | * Support for persistent environment data |
14 | * | |
ea882baf WD |
15 | * The "environment" is stored on external storage as a list of '\0' |
16 | * terminated "name=value" strings. The end of the list is marked by | |
fc0b5948 | 17 | * a double '\0'. The environment is preceded by a 32 bit CRC over |
ea882baf WD |
18 | * the data part and, in case of redundant environment, a byte of |
19 | * flags. | |
a68d3ed0 | 20 | * |
ea882baf WD |
21 | * This linearized representation will also be used before |
22 | * relocation, i. e. as long as we don't have a full C runtime | |
23 | * environment. After that, we use a hash table. | |
a68d3ed0 WD |
24 | */ |
25 | ||
26 | #include <common.h> | |
18d66533 | 27 | #include <cli.h> |
a68d3ed0 | 28 | #include <command.h> |
24b852a7 | 29 | #include <console.h> |
f1f0ae6a | 30 | #include <env.h> |
f3998fdc | 31 | #include <env_internal.h> |
f7ae49fc | 32 | #include <log.h> |
ea882baf WD |
33 | #include <search.h> |
34 | #include <errno.h> | |
246c6922 | 35 | #include <malloc.h> |
0eb25b61 | 36 | #include <mapmem.h> |
401d1c4f | 37 | #include <asm/global_data.h> |
cd93d625 | 38 | #include <linux/bitops.h> |
3db71108 | 39 | #include <u-boot/crc.h> |
a68d3ed0 WD |
40 | #include <linux/stddef.h> |
41 | #include <asm/byteorder.h> | |
fd37dac9 | 42 | #include <asm/io.h> |
a68d3ed0 | 43 | |
d87080b7 WD |
44 | DECLARE_GLOBAL_DATA_PTR; |
45 | ||
2643c85d PD |
46 | #if defined(CONFIG_ENV_IS_IN_EEPROM) || \ |
47 | defined(CONFIG_ENV_IS_IN_FLASH) || \ | |
48 | defined(CONFIG_ENV_IS_IN_MMC) || \ | |
49 | defined(CONFIG_ENV_IS_IN_FAT) || \ | |
50 | defined(CONFIG_ENV_IS_IN_EXT4) || \ | |
51 | defined(CONFIG_ENV_IS_IN_NAND) || \ | |
52 | defined(CONFIG_ENV_IS_IN_NVRAM) || \ | |
53 | defined(CONFIG_ENV_IS_IN_ONENAND) || \ | |
54 | defined(CONFIG_ENV_IS_IN_SATA) || \ | |
55 | defined(CONFIG_ENV_IS_IN_SPI_FLASH) || \ | |
56 | defined(CONFIG_ENV_IS_IN_REMOTE) || \ | |
57 | defined(CONFIG_ENV_IS_IN_UBI) | |
58 | ||
59 | #define ENV_IS_IN_DEVICE | |
60 | ||
61 | #endif | |
62 | ||
63 | #if !defined(ENV_IS_IN_DEVICE) && \ | |
f3c615b8 | 64 | !defined(CONFIG_ENV_IS_NOWHERE) |
7b7341d7 | 65 | # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|MMC|FAT|EXT4|\ |
d1b88cd3 | 66 | NAND|NVRAM|ONENAND|SATA|SPI_FLASH|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE |
a68d3ed0 WD |
67 | #endif |
68 | ||
ea882baf WD |
69 | /* |
70 | * Maximum expected input data size for import command | |
71 | */ | |
72 | #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */ | |
a68d3ed0 | 73 | |
da95427c | 74 | /* |
ea882baf | 75 | * This variable is incremented on each do_env_set(), so it can |
f1f0ae6a | 76 | * be used via env_get_id() as an indication, if the environment |
da95427c HS |
77 | * has changed or not. So it is possible to reread an environment |
78 | * variable only if the environment was changed ... done so for | |
79 | * example in NetInitLoop() | |
80 | */ | |
2f70c49e | 81 | static int env_id = 1; |
a68d3ed0 | 82 | |
f1f0ae6a | 83 | int env_get_id(void) |
2f70c49e HS |
84 | { |
85 | return env_id; | |
86 | } | |
a68d3ed0 | 87 | |
7ac2fe2d | 88 | #ifndef CONFIG_SPL_BUILD |
4c94f6c5 | 89 | /* |
ea882baf WD |
90 | * Command interface: print one or all environment variables |
91 | * | |
92 | * Returns 0 in case of error, or length of printed string | |
4c94f6c5 | 93 | */ |
be11235a | 94 | static int env_print(char *name, int flag) |
a68d3ed0 | 95 | { |
ea882baf | 96 | char *res = NULL; |
22a4a6c5 | 97 | ssize_t len; |
ea882baf WD |
98 | |
99 | if (name) { /* print a single name */ | |
dd2408ca | 100 | struct env_entry e, *ep; |
ea882baf WD |
101 | |
102 | e.key = name; | |
103 | e.data = NULL; | |
3f0d6807 | 104 | hsearch_r(e, ENV_FIND, &ep, &env_htab, flag); |
ea882baf WD |
105 | if (ep == NULL) |
106 | return 0; | |
f3c615b8 | 107 | len = printf("%s=%s\n", ep->key, ep->data); |
ea882baf WD |
108 | return len; |
109 | } | |
a68d3ed0 | 110 | |
ea882baf | 111 | /* print whole list */ |
be11235a | 112 | len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL); |
a68d3ed0 | 113 | |
ea882baf WD |
114 | if (len > 0) { |
115 | puts(res); | |
116 | free(res); | |
117 | return len; | |
a68d3ed0 WD |
118 | } |
119 | ||
ea882baf | 120 | /* should never happen */ |
22a4a6c5 | 121 | printf("## Error: cannot export environment\n"); |
ea882baf | 122 | return 0; |
4c94f6c5 | 123 | } |
a68d3ed0 | 124 | |
09140113 SG |
125 | static int do_env_print(struct cmd_tbl *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 | ||
49d81fdf AT |
132 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
133 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') | |
134 | return do_env_print_efi(cmdtp, flag, --argc, ++argv); | |
135 | #endif | |
136 | ||
be11235a JH |
137 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') { |
138 | argc--; | |
139 | argv++; | |
140 | env_flag &= ~H_HIDE_DOT; | |
141 | } | |
a68d3ed0 | 142 | |
4c94f6c5 MF |
143 | if (argc == 1) { |
144 | /* print all env vars */ | |
be11235a | 145 | rcode = env_print(NULL, env_flag); |
ea882baf | 146 | if (!rcode) |
4c94f6c5 MF |
147 | return 1; |
148 | printf("\nEnvironment size: %d/%ld bytes\n", | |
149 | rcode, (ulong)ENV_SIZE); | |
150 | return 0; | |
151 | } | |
a68d3ed0 | 152 | |
4c94f6c5 | 153 | /* print selected env vars */ |
be11235a | 154 | env_flag &= ~H_HIDE_DOT; |
4c94f6c5 | 155 | for (i = 1; i < argc; ++i) { |
be11235a | 156 | int rc = env_print(argv[i], env_flag); |
ea882baf WD |
157 | if (!rc) { |
158 | printf("## Error: \"%s\" not defined\n", argv[i]); | |
4c94f6c5 | 159 | ++rcode; |
a68d3ed0 WD |
160 | } |
161 | } | |
4c94f6c5 | 162 | |
a68d3ed0 WD |
163 | return rcode; |
164 | } | |
165 | ||
a000b795 | 166 | #ifdef CONFIG_CMD_GREPENV |
09140113 SG |
167 | static int do_env_grep(struct cmd_tbl *cmdtp, int flag, |
168 | int argc, char *const argv[]) | |
a000b795 | 169 | { |
5a31ea04 | 170 | char *res = NULL; |
be29df6a | 171 | int len, grep_how, grep_what; |
a000b795 KP |
172 | |
173 | if (argc < 2) | |
4c12eeb8 | 174 | return CMD_RET_USAGE; |
a000b795 | 175 | |
be29df6a WD |
176 | grep_how = H_MATCH_SUBSTR; /* default: substring search */ |
177 | grep_what = H_MATCH_BOTH; /* default: grep names and values */ | |
d87244d5 | 178 | |
9a832331 PA |
179 | while (--argc > 0 && **++argv == '-') { |
180 | char *arg = *argv; | |
d87244d5 WD |
181 | while (*++arg) { |
182 | switch (*arg) { | |
be29df6a WD |
183 | #ifdef CONFIG_REGEX |
184 | case 'e': /* use regex matching */ | |
185 | grep_how = H_MATCH_REGEX; | |
186 | break; | |
187 | #endif | |
d87244d5 | 188 | case 'n': /* grep for name */ |
be29df6a | 189 | grep_what = H_MATCH_KEY; |
d87244d5 WD |
190 | break; |
191 | case 'v': /* grep for value */ | |
be29df6a | 192 | grep_what = H_MATCH_DATA; |
d87244d5 WD |
193 | break; |
194 | case 'b': /* grep for both */ | |
be29df6a | 195 | grep_what = H_MATCH_BOTH; |
d87244d5 WD |
196 | break; |
197 | case '-': | |
198 | goto DONE; | |
199 | default: | |
200 | return CMD_RET_USAGE; | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | DONE: | |
5a31ea04 | 206 | len = hexport_r(&env_htab, '\n', |
be29df6a | 207 | flag | grep_what | grep_how, |
5a31ea04 | 208 | &res, 0, argc, argv); |
a000b795 | 209 | |
5a31ea04 WD |
210 | if (len > 0) { |
211 | puts(res); | |
212 | free(res); | |
a000b795 KP |
213 | } |
214 | ||
5a31ea04 WD |
215 | if (len < 2) |
216 | return 1; | |
217 | ||
218 | return 0; | |
a000b795 KP |
219 | } |
220 | #endif | |
7ac2fe2d | 221 | #endif /* CONFIG_SPL_BUILD */ |
a000b795 | 222 | |
c3f65258 GF |
223 | /* |
224 | * Set a new environment variable, | |
225 | * or replace or delete an existing one. | |
2598090b | 226 | */ |
09140113 | 227 | static int _do_env_set(int flag, int argc, char *const argv[], int env_flag) |
c3f65258 GF |
228 | { |
229 | int i, len; | |
230 | char *name, *value, *s; | |
dd2408ca | 231 | struct env_entry e, *ep; |
c3f65258 | 232 | |
24ab5a19 | 233 | debug("Initial value for argc=%d\n", argc); |
49d81fdf AT |
234 | |
235 | #if CONFIG_IS_ENABLED(CMD_NVEDIT_EFI) | |
236 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e') | |
237 | return do_env_set_efi(NULL, flag, --argc, ++argv); | |
238 | #endif | |
239 | ||
24ab5a19 JH |
240 | while (argc > 1 && **(argv + 1) == '-') { |
241 | char *arg = *++argv; | |
242 | ||
243 | --argc; | |
244 | while (*++arg) { | |
245 | switch (*arg) { | |
246 | case 'f': /* force */ | |
247 | env_flag |= H_FORCE; | |
248 | break; | |
249 | default: | |
250 | return CMD_RET_USAGE; | |
251 | } | |
252 | } | |
253 | } | |
254 | debug("Final value for argc=%d\n", argc); | |
c3f65258 | 255 | name = argv[1]; |
c3f65258 GF |
256 | |
257 | if (strchr(name, '=')) { | |
258 | printf("## Error: illegal character '='" | |
259 | "in variable name \"%s\"\n", name); | |
260 | return 1; | |
261 | } | |
262 | ||
263 | env_id++; | |
c3f65258 | 264 | |
a68d3ed0 | 265 | /* Delete only ? */ |
d09b1787 | 266 | if (argc < 3 || argv[2] == NULL) { |
24ab5a19 | 267 | int rc = hdelete_r(name, &env_htab, env_flag); |
96434a76 SG |
268 | |
269 | /* If the variable didn't exist, don't report an error */ | |
270 | return rc && rc != -ENOENT ? 1 : 0; | |
a68d3ed0 WD |
271 | } |
272 | ||
273 | /* | |
ea882baf | 274 | * Insert / replace new value |
a68d3ed0 | 275 | */ |
f3c615b8 | 276 | for (i = 2, len = 0; i < argc; ++i) |
a68d3ed0 | 277 | len += strlen(argv[i]) + 1; |
f3c615b8 ML |
278 | |
279 | value = malloc(len); | |
280 | if (value == NULL) { | |
ea882baf | 281 | printf("## Can't malloc %d bytes\n", len); |
a68d3ed0 WD |
282 | return 1; |
283 | } | |
f3c615b8 | 284 | for (i = 2, s = value; i < argc; ++i) { |
ea882baf | 285 | char *v = argv[i]; |
a68d3ed0 | 286 | |
ea882baf | 287 | while ((*s++ = *v++) != '\0') |
a68d3ed0 | 288 | ; |
d09b1787 | 289 | *(s - 1) = ' '; |
ea882baf WD |
290 | } |
291 | if (s != value) | |
292 | *--s = '\0'; | |
293 | ||
d09b1787 IG |
294 | e.key = name; |
295 | e.data = value; | |
3f0d6807 | 296 | hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag); |
ea882baf WD |
297 | free(value); |
298 | if (!ep) { | |
299 | printf("## Error inserting \"%s\" variable, errno=%d\n", | |
300 | name, errno); | |
301 | return 1; | |
a68d3ed0 | 302 | } |
a68d3ed0 | 303 | |
a68d3ed0 WD |
304 | return 0; |
305 | } | |
306 | ||
382bee57 | 307 | int env_set(const char *varname, const char *varvalue) |
a68d3ed0 | 308 | { |
84b5e802 WD |
309 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
310 | ||
a7eb1d66 JH |
311 | /* before import into hashtable */ |
312 | if (!(gd->flags & GD_FLG_ENV_READY)) | |
313 | return 1; | |
314 | ||
d09b1787 | 315 | if (varvalue == NULL || varvalue[0] == '\0') |
94b467b1 | 316 | return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); |
9ffd451a | 317 | else |
94b467b1 | 318 | return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); |
a68d3ed0 WD |
319 | } |
320 | ||
7ac2fe2d | 321 | #ifndef CONFIG_SPL_BUILD |
09140113 SG |
322 | static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc, |
323 | char *const argv[]) | |
a68d3ed0 | 324 | { |
47e26b1b | 325 | if (argc < 2) |
4c12eeb8 | 326 | return CMD_RET_USAGE; |
a68d3ed0 | 327 | |
94b467b1 | 328 | return _do_env_set(flag, argc, argv, H_INTERACTIVE); |
a68d3ed0 WD |
329 | } |
330 | ||
ea882baf | 331 | /* |
a68d3ed0 WD |
332 | * Prompt for environment variable |
333 | */ | |
c76fe474 | 334 | #if defined(CONFIG_CMD_ASKENV) |
09140113 | 335 | int do_env_ask(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
a68d3ed0 | 336 | { |
6d0f6bcf | 337 | char message[CONFIG_SYS_CBSIZE]; |
7d85591d | 338 | int i, len, pos, size; |
a68d3ed0 | 339 | char *local_args[4]; |
7d85591d | 340 | char *endptr; |
a68d3ed0 WD |
341 | |
342 | local_args[0] = argv[0]; | |
343 | local_args[1] = argv[1]; | |
344 | local_args[2] = NULL; | |
345 | local_args[3] = NULL; | |
346 | ||
7d85591d WD |
347 | /* |
348 | * Check the syntax: | |
349 | * | |
350 | * env_ask envname [message1 ...] [size] | |
351 | */ | |
352 | if (argc == 1) | |
4c12eeb8 | 353 | return CMD_RET_USAGE; |
a68d3ed0 | 354 | |
7d85591d WD |
355 | /* |
356 | * We test the last argument if it can be converted | |
357 | * into a decimal number. If yes, we assume it's | |
358 | * the size. Otherwise we echo it as part of the | |
359 | * message. | |
360 | */ | |
0b1284eb | 361 | i = dectoul(argv[argc - 1], &endptr); |
7d85591d WD |
362 | if (*endptr != '\0') { /* no size */ |
363 | size = CONFIG_SYS_CBSIZE - 1; | |
364 | } else { /* size given */ | |
365 | size = i; | |
366 | --argc; | |
367 | } | |
a68d3ed0 | 368 | |
7d85591d WD |
369 | if (argc <= 2) { |
370 | sprintf(message, "Please enter '%s': ", argv[1]); | |
371 | } else { | |
372 | /* env_ask envname message1 ... messagen [size] */ | |
bf52fcde | 373 | for (i = 2, pos = 0; i < argc && pos+1 < sizeof(message); i++) { |
f3c615b8 | 374 | if (pos) |
a68d3ed0 | 375 | message[pos++] = ' '; |
f3c615b8 | 376 | |
c667723f | 377 | strncpy(message + pos, argv[i], sizeof(message) - pos); |
a68d3ed0 WD |
378 | pos += strlen(argv[i]); |
379 | } | |
c667723f TR |
380 | if (pos < sizeof(message) - 1) { |
381 | message[pos++] = ' '; | |
382 | message[pos] = '\0'; | |
383 | } else | |
384 | message[CONFIG_SYS_CBSIZE - 1] = '\0'; | |
a68d3ed0 WD |
385 | } |
386 | ||
6d0f6bcf JCPV |
387 | if (size >= CONFIG_SYS_CBSIZE) |
388 | size = CONFIG_SYS_CBSIZE - 1; | |
a68d3ed0 WD |
389 | |
390 | if (size <= 0) | |
391 | return 1; | |
392 | ||
393 | /* prompt for input */ | |
e1bf824d | 394 | len = cli_readline(message); |
a68d3ed0 WD |
395 | |
396 | if (size < len) | |
397 | console_buffer[size] = '\0'; | |
398 | ||
399 | len = 2; | |
400 | if (console_buffer[0] != '\0') { | |
401 | local_args[2] = console_buffer; | |
402 | len = 3; | |
403 | } | |
404 | ||
405 | /* Continue calling setenv code */ | |
94b467b1 | 406 | return _do_env_set(flag, len, local_args, H_INTERACTIVE); |
a68d3ed0 | 407 | } |
90253178 | 408 | #endif |
a68d3ed0 | 409 | |
5e2b3e0c | 410 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
cca98fd6 JH |
411 | static int print_static_binding(const char *var_name, const char *callback_name, |
412 | void *priv) | |
5e2b3e0c JH |
413 | { |
414 | printf("\t%-20s %-20s\n", var_name, callback_name); | |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
dd2408ca | 419 | static int print_active_callback(struct env_entry *entry) |
5e2b3e0c JH |
420 | { |
421 | struct env_clbk_tbl *clbkp; | |
422 | int i; | |
423 | int num_callbacks; | |
424 | ||
425 | if (entry->callback == NULL) | |
426 | return 0; | |
427 | ||
428 | /* look up the callback in the linker-list */ | |
429 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); | |
430 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); | |
431 | i < num_callbacks; | |
432 | i++, clbkp++) { | |
433 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) | |
434 | if (entry->callback == clbkp->callback + gd->reloc_off) | |
435 | #else | |
436 | if (entry->callback == clbkp->callback) | |
437 | #endif | |
438 | break; | |
439 | } | |
440 | ||
441 | if (i == num_callbacks) | |
442 | /* this should probably never happen, but just in case... */ | |
443 | printf("\t%-20s %p\n", entry->key, entry->callback); | |
444 | else | |
445 | printf("\t%-20s %-20s\n", entry->key, clbkp->name); | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
450 | /* | |
451 | * Print the callbacks available and what they are bound to | |
452 | */ | |
09140113 SG |
453 | int do_env_callback(struct cmd_tbl *cmdtp, int flag, int argc, |
454 | char *const argv[]) | |
5e2b3e0c JH |
455 | { |
456 | struct env_clbk_tbl *clbkp; | |
457 | int i; | |
458 | int num_callbacks; | |
459 | ||
460 | /* Print the available callbacks */ | |
461 | puts("Available callbacks:\n"); | |
462 | puts("\tCallback Name\n"); | |
463 | puts("\t-------------\n"); | |
464 | num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk); | |
465 | for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk); | |
466 | i < num_callbacks; | |
467 | i++, clbkp++) | |
468 | printf("\t%s\n", clbkp->name); | |
469 | puts("\n"); | |
470 | ||
471 | /* Print the static bindings that may exist */ | |
472 | puts("Static callback bindings:\n"); | |
473 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); | |
474 | printf("\t%-20s %-20s\n", "-------------", "-------------"); | |
cca98fd6 | 475 | env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL); |
5e2b3e0c JH |
476 | puts("\n"); |
477 | ||
478 | /* walk through each variable and print the callback if it has one */ | |
479 | puts("Active callback bindings:\n"); | |
480 | printf("\t%-20s %-20s\n", "Variable Name", "Callback Name"); | |
481 | printf("\t%-20s %-20s\n", "-------------", "-------------"); | |
482 | hwalk_r(&env_htab, print_active_callback); | |
483 | return 0; | |
484 | } | |
485 | #endif | |
486 | ||
fffad71b | 487 | #if defined(CONFIG_CMD_ENV_FLAGS) |
cca98fd6 JH |
488 | static int print_static_flags(const char *var_name, const char *flags, |
489 | void *priv) | |
fffad71b JH |
490 | { |
491 | enum env_flags_vartype type = env_flags_parse_vartype(flags); | |
267541f7 | 492 | enum env_flags_varaccess access = env_flags_parse_varaccess(flags); |
fffad71b | 493 | |
267541f7 JH |
494 | printf("\t%-20s %-20s %-20s\n", var_name, |
495 | env_flags_get_vartype_name(type), | |
496 | env_flags_get_varaccess_name(access)); | |
fffad71b JH |
497 | |
498 | return 0; | |
499 | } | |
500 | ||
dd2408ca | 501 | static int print_active_flags(struct env_entry *entry) |
fffad71b JH |
502 | { |
503 | enum env_flags_vartype type; | |
267541f7 | 504 | enum env_flags_varaccess access; |
fffad71b JH |
505 | |
506 | if (entry->flags == 0) | |
507 | return 0; | |
508 | ||
509 | type = (enum env_flags_vartype) | |
510 | (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK); | |
267541f7 JH |
511 | access = env_flags_parse_varaccess_from_binflags(entry->flags); |
512 | printf("\t%-20s %-20s %-20s\n", entry->key, | |
513 | env_flags_get_vartype_name(type), | |
514 | env_flags_get_varaccess_name(access)); | |
fffad71b JH |
515 | |
516 | return 0; | |
517 | } | |
518 | ||
519 | /* | |
520 | * Print the flags available and what variables have flags | |
521 | */ | |
09140113 | 522 | int do_env_flags(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
fffad71b JH |
523 | { |
524 | /* Print the available variable types */ | |
525 | printf("Available variable type flags (position %d):\n", | |
526 | ENV_FLAGS_VARTYPE_LOC); | |
527 | puts("\tFlag\tVariable Type Name\n"); | |
528 | puts("\t----\t------------------\n"); | |
529 | env_flags_print_vartypes(); | |
530 | puts("\n"); | |
531 | ||
267541f7 JH |
532 | /* Print the available variable access types */ |
533 | printf("Available variable access flags (position %d):\n", | |
534 | ENV_FLAGS_VARACCESS_LOC); | |
535 | puts("\tFlag\tVariable Access Name\n"); | |
536 | puts("\t----\t--------------------\n"); | |
537 | env_flags_print_varaccess(); | |
538 | puts("\n"); | |
539 | ||
fffad71b JH |
540 | /* Print the static flags that may exist */ |
541 | puts("Static flags:\n"); | |
267541f7 JH |
542 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
543 | "Variable Access"); | |
544 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", | |
545 | "---------------"); | |
cca98fd6 | 546 | env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL); |
fffad71b JH |
547 | puts("\n"); |
548 | ||
549 | /* walk through each variable and print the flags if non-default */ | |
550 | puts("Active flags:\n"); | |
267541f7 JH |
551 | printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type", |
552 | "Variable Access"); | |
553 | printf("\t%-20s %-20s %-20s\n", "-------------", "-------------", | |
554 | "---------------"); | |
fffad71b JH |
555 | hwalk_r(&env_htab, print_active_flags); |
556 | return 0; | |
557 | } | |
558 | #endif | |
559 | ||
ea882baf | 560 | /* |
246c6922 PT |
561 | * Interactively edit an environment variable |
562 | */ | |
563 | #if defined(CONFIG_CMD_EDITENV) | |
09140113 SG |
564 | static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc, |
565 | char *const argv[]) | |
246c6922 PT |
566 | { |
567 | char buffer[CONFIG_SYS_CBSIZE]; | |
568 | char *init_val; | |
246c6922 | 569 | |
47e26b1b | 570 | if (argc < 2) |
4c12eeb8 | 571 | return CMD_RET_USAGE; |
246c6922 | 572 | |
94b467b1 JH |
573 | /* before import into hashtable */ |
574 | if (!(gd->flags & GD_FLG_ENV_READY)) | |
575 | return 1; | |
576 | ||
246c6922 | 577 | /* Set read buffer to initial value or empty sting */ |
00caae6d | 578 | init_val = env_get(argv[1]); |
246c6922 | 579 | if (init_val) |
5d49b4cd | 580 | snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val); |
246c6922 PT |
581 | else |
582 | buffer[0] = '\0'; | |
583 | ||
e1bf824d | 584 | if (cli_readline_into_buffer("edit: ", buffer, 0) < 0) |
18a3cce9 | 585 | return 1; |
246c6922 | 586 | |
94b467b1 JH |
587 | if (buffer[0] == '\0') { |
588 | const char * const _argv[3] = { "setenv", argv[1], NULL }; | |
589 | ||
590 | return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE); | |
591 | } else { | |
592 | const char * const _argv[4] = { "setenv", argv[1], buffer, | |
593 | NULL }; | |
594 | ||
595 | return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE); | |
596 | } | |
246c6922 PT |
597 | } |
598 | #endif /* CONFIG_CMD_EDITENV */ | |
91a76751 | 599 | |
2643c85d | 600 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
09140113 SG |
601 | static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc, |
602 | char *const argv[]) | |
a68d3ed0 | 603 | { |
01510091 | 604 | return env_save() ? 1 : 0; |
a68d3ed0 | 605 | } |
8bde7f77 | 606 | |
ba69dc26 | 607 | U_BOOT_CMD( |
ea882baf | 608 | saveenv, 1, 0, do_env_save, |
2fb2604d | 609 | "save environment variables to persistent storage", |
a89c33db | 610 | "" |
ba69dc26 | 611 | ); |
cd121bdb FW |
612 | |
613 | #if defined(CONFIG_CMD_ERASEENV) | |
09140113 SG |
614 | static int do_env_erase(struct cmd_tbl *cmdtp, int flag, int argc, |
615 | char *const argv[]) | |
cd121bdb FW |
616 | { |
617 | return env_erase() ? 1 : 0; | |
618 | } | |
619 | ||
620 | U_BOOT_CMD( | |
621 | eraseenv, 1, 0, do_env_erase, | |
622 | "erase environment variables from persistent storage", | |
623 | "" | |
624 | ); | |
625 | #endif | |
a68d3ed0 | 626 | #endif |
0115dd3a PD |
627 | |
628 | #if defined(CONFIG_CMD_NVEDIT_LOAD) | |
629 | static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc, | |
630 | char *const argv[]) | |
631 | { | |
632 | return env_reload() ? 1 : 0; | |
633 | } | |
634 | #endif | |
a97d22eb PD |
635 | |
636 | #if defined(CONFIG_CMD_NVEDIT_SELECT) | |
637 | static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc, | |
638 | char *const argv[]) | |
639 | { | |
640 | return env_select(argv[1]) ? 1 : 0; | |
641 | } | |
642 | #endif | |
643 | ||
7ac2fe2d | 644 | #endif /* CONFIG_SPL_BUILD */ |
a68d3ed0 | 645 | |
7ac2fe2d | 646 | #ifndef CONFIG_SPL_BUILD |
09140113 SG |
647 | static int do_env_default(struct cmd_tbl *cmdtp, int flag, |
648 | int argc, char *const argv[]) | |
ea882baf | 649 | { |
5a04264e | 650 | int all = 0, env_flag = H_INTERACTIVE; |
f3c615b8 | 651 | |
b64b7c3d GF |
652 | debug("Initial value for argc=%d\n", argc); |
653 | while (--argc > 0 && **++argv == '-') { | |
654 | char *arg = *argv; | |
655 | ||
656 | while (*++arg) { | |
657 | switch (*arg) { | |
658 | case 'a': /* default all */ | |
659 | all = 1; | |
660 | break; | |
661 | case 'f': /* force */ | |
30091494 | 662 | env_flag |= H_FORCE; |
b64b7c3d GF |
663 | break; |
664 | default: | |
665 | return cmd_usage(cmdtp); | |
666 | } | |
667 | } | |
668 | } | |
669 | debug("Final value for argc=%d\n", argc); | |
670 | if (all && (argc == 0)) { | |
671 | /* Reset the whole environment */ | |
0ac7d722 | 672 | env_set_default("## Resetting to default environment\n", |
c5d548a9 | 673 | env_flag); |
b64b7c3d GF |
674 | return 0; |
675 | } | |
676 | if (!all && (argc > 0)) { | |
677 | /* Reset individual variables */ | |
0b9d8a05 | 678 | env_set_default_vars(argc, argv, env_flag); |
b64b7c3d GF |
679 | return 0; |
680 | } | |
681 | ||
682 | return cmd_usage(cmdtp); | |
ea882baf WD |
683 | } |
684 | ||
09140113 SG |
685 | static int do_env_delete(struct cmd_tbl *cmdtp, int flag, |
686 | int argc, char *const argv[]) | |
ea882baf | 687 | { |
9d8d661d JH |
688 | int env_flag = H_INTERACTIVE; |
689 | int ret = 0; | |
690 | ||
691 | debug("Initial value for argc=%d\n", argc); | |
692 | while (argc > 1 && **(argv + 1) == '-') { | |
693 | char *arg = *++argv; | |
694 | ||
695 | --argc; | |
696 | while (*++arg) { | |
697 | switch (*arg) { | |
698 | case 'f': /* force */ | |
699 | env_flag |= H_FORCE; | |
700 | break; | |
701 | default: | |
702 | return CMD_RET_USAGE; | |
703 | } | |
704 | } | |
705 | } | |
706 | debug("Final value for argc=%d\n", argc); | |
707 | ||
708 | env_id++; | |
709 | ||
710 | while (--argc > 0) { | |
711 | char *name = *++argv; | |
712 | ||
96434a76 | 713 | if (hdelete_r(name, &env_htab, env_flag)) |
9d8d661d JH |
714 | ret = 1; |
715 | } | |
716 | ||
717 | return ret; | |
ea882baf WD |
718 | } |
719 | ||
0c79cda0 | 720 | #ifdef CONFIG_CMD_EXPORTENV |
ea882baf | 721 | /* |
37f2fe74 | 722 | * env export [-t | -b | -c] [-s size] addr [var ...] |
ea882baf WD |
723 | * -t: export as text format; if size is given, data will be |
724 | * padded with '\0' bytes; if not, one terminating '\0' | |
725 | * will be added (which is included in the "filesize" | |
726 | * setting so you can for exmple copy this to flash and | |
727 | * keep the termination). | |
728 | * -b: export as binary format (name=value pairs separated by | |
729 | * '\0', list end marked by double "\0\0") | |
730 | * -c: export as checksum protected environment format as | |
731 | * used for example by "saveenv" command | |
37f2fe74 WD |
732 | * -s size: |
733 | * size of output buffer | |
ea882baf | 734 | * addr: memory address where environment gets stored |
37f2fe74 WD |
735 | * var... List of variable names that get included into the |
736 | * export. Without arguments, the whole environment gets | |
737 | * exported. | |
ea882baf WD |
738 | * |
739 | * With "-c" and size is NOT given, then the export command will | |
740 | * format the data as currently used for the persistent storage, | |
741 | * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and | |
fc0b5948 | 742 | * prepend a valid CRC32 checksum and, in case of redundant |
ea882baf WD |
743 | * environment, a "current" redundancy flag. If size is given, this |
744 | * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 | |
745 | * checksum and redundancy flag will be inserted. | |
746 | * | |
747 | * With "-b" and "-t", always only the real data (including a | |
748 | * terminating '\0' byte) will be written; here the optional size | |
749 | * argument will be used to make sure not to overflow the user | |
750 | * provided buffer; the command will abort if the size is not | |
fc0b5948 | 751 | * sufficient. Any remaining space will be '\0' padded. |
ea882baf WD |
752 | * |
753 | * On successful return, the variable "filesize" will be set. | |
754 | * Note that filesize includes the trailing/terminating '\0' byte(s). | |
755 | * | |
fc0b5948 | 756 | * Usage scenario: create a text snapshot/backup of the current settings: |
ea882baf WD |
757 | * |
758 | * => env export -t 100000 | |
759 | * => era ${backup_addr} +${filesize} | |
760 | * => cp.b 100000 ${backup_addr} ${filesize} | |
761 | * | |
762 | * Re-import this snapshot, deleting all other settings: | |
763 | * | |
764 | * => env import -d -t ${backup_addr} | |
765 | */ | |
09140113 SG |
766 | static int do_env_export(struct cmd_tbl *cmdtp, int flag, |
767 | int argc, char *const argv[]) | |
ea882baf WD |
768 | { |
769 | char buf[32]; | |
fd37dac9 SG |
770 | ulong addr; |
771 | char *ptr, *cmd, *res; | |
37f2fe74 | 772 | size_t size = 0; |
ea882baf WD |
773 | ssize_t len; |
774 | env_t *envp; | |
775 | char sep = '\n'; | |
776 | int chk = 0; | |
777 | int fmt = 0; | |
778 | ||
779 | cmd = *argv; | |
780 | ||
781 | while (--argc > 0 && **++argv == '-') { | |
782 | char *arg = *argv; | |
783 | while (*++arg) { | |
784 | switch (*arg) { | |
785 | case 'b': /* raw binary format */ | |
786 | if (fmt++) | |
787 | goto sep_err; | |
788 | sep = '\0'; | |
789 | break; | |
790 | case 'c': /* external checksum format */ | |
791 | if (fmt++) | |
792 | goto sep_err; | |
793 | sep = '\0'; | |
794 | chk = 1; | |
795 | break; | |
37f2fe74 WD |
796 | case 's': /* size given */ |
797 | if (--argc <= 0) | |
798 | return cmd_usage(cmdtp); | |
7e5f460e | 799 | size = hextoul(*++argv, NULL); |
37f2fe74 | 800 | goto NXTARG; |
ea882baf WD |
801 | case 't': /* text format */ |
802 | if (fmt++) | |
803 | goto sep_err; | |
804 | sep = '\n'; | |
805 | break; | |
806 | default: | |
4c12eeb8 | 807 | return CMD_RET_USAGE; |
ea882baf WD |
808 | } |
809 | } | |
37f2fe74 | 810 | NXTARG: ; |
ea882baf WD |
811 | } |
812 | ||
f3c615b8 | 813 | if (argc < 1) |
4c12eeb8 | 814 | return CMD_RET_USAGE; |
8bde7f77 | 815 | |
7e5f460e | 816 | addr = hextoul(argv[0], NULL); |
fd37dac9 | 817 | ptr = map_sysmem(addr, size); |
ea882baf | 818 | |
37f2fe74 | 819 | if (size) |
fd37dac9 | 820 | memset(ptr, '\0', size); |
37f2fe74 WD |
821 | |
822 | argc--; | |
823 | argv++; | |
ea882baf WD |
824 | |
825 | if (sep) { /* export as text file */ | |
ea009d47 WD |
826 | len = hexport_r(&env_htab, sep, |
827 | H_MATCH_KEY | H_MATCH_IDENT, | |
fd37dac9 | 828 | &ptr, size, argc, argv); |
ea882baf | 829 | if (len < 0) { |
6c90f623 QS |
830 | pr_err("## Error: Cannot export environment: errno = %d\n", |
831 | errno); | |
ea882baf WD |
832 | return 1; |
833 | } | |
8c3aff52 | 834 | sprintf(buf, "%zX", (size_t)len); |
382bee57 | 835 | env_set("filesize", buf); |
ea882baf WD |
836 | |
837 | return 0; | |
838 | } | |
839 | ||
fd37dac9 | 840 | envp = (env_t *)ptr; |
ea882baf WD |
841 | |
842 | if (chk) /* export as checksum protected block */ | |
843 | res = (char *)envp->data; | |
844 | else /* export as raw binary data */ | |
fd37dac9 | 845 | res = ptr; |
ea882baf | 846 | |
ea009d47 WD |
847 | len = hexport_r(&env_htab, '\0', |
848 | H_MATCH_KEY | H_MATCH_IDENT, | |
849 | &res, ENV_SIZE, argc, argv); | |
ea882baf | 850 | if (len < 0) { |
6c90f623 QS |
851 | pr_err("## Error: Cannot export environment: errno = %d\n", |
852 | errno); | |
ea882baf WD |
853 | return 1; |
854 | } | |
855 | ||
856 | if (chk) { | |
d71b029d NS |
857 | envp->crc = crc32(0, envp->data, |
858 | size ? size - offsetof(env_t, data) : ENV_SIZE); | |
ea882baf | 859 | #ifdef CONFIG_ENV_ADDR_REDUND |
d3716dd6 | 860 | envp->flags = ENV_REDUND_ACTIVE; |
ea882baf WD |
861 | #endif |
862 | } | |
018f5303 | 863 | env_set_hex("filesize", len + offsetof(env_t, data)); |
ea882baf WD |
864 | |
865 | return 0; | |
866 | ||
867 | sep_err: | |
6c90f623 QS |
868 | printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", |
869 | cmd); | |
ea882baf WD |
870 | return 1; |
871 | } | |
0c79cda0 | 872 | #endif |
ea882baf | 873 | |
0c79cda0 | 874 | #ifdef CONFIG_CMD_IMPORTENV |
ea882baf | 875 | /* |
eaf73472 QS |
876 | * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] |
877 | * -d: delete existing environment before importing if no var is | |
878 | * passed; if vars are passed, if one var is in the current | |
879 | * environment but not in the environment at addr, delete var from | |
880 | * current environment; | |
fc0b5948 | 881 | * otherwise overwrite / append to existing definitions |
ea882baf WD |
882 | * -t: assume text format; either "size" must be given or the |
883 | * text data must be '\0' terminated | |
ecd1446f AH |
884 | * -r: handle CRLF like LF, that means exported variables with |
885 | * a content which ends with \r won't get imported. Used | |
886 | * to import text files created with editors which are using CRLF | |
887 | * for line endings. Only effective in addition to -t. | |
ea882baf WD |
888 | * -b: assume binary format ('\0' separated, "\0\0" terminated) |
889 | * -c: assume checksum protected environment format | |
890 | * addr: memory address to read from | |
891 | * size: length of input data; if missing, proper '\0' | |
892 | * termination is mandatory | |
eaf73472 QS |
893 | * if var is set and size should be missing (i.e. '\0' |
894 | * termination), set size to '-' | |
895 | * var... List of the names of the only variables that get imported from | |
896 | * the environment at address 'addr'. Without arguments, the whole | |
897 | * environment gets imported. | |
ea882baf | 898 | */ |
09140113 SG |
899 | static int do_env_import(struct cmd_tbl *cmdtp, int flag, |
900 | int argc, char *const argv[]) | |
ea882baf | 901 | { |
fd37dac9 SG |
902 | ulong addr; |
903 | char *cmd, *ptr; | |
ea882baf WD |
904 | char sep = '\n'; |
905 | int chk = 0; | |
906 | int fmt = 0; | |
907 | int del = 0; | |
ecd1446f | 908 | int crlf_is_lf = 0; |
eaf73472 | 909 | int wl = 0; |
ea882baf WD |
910 | size_t size; |
911 | ||
912 | cmd = *argv; | |
913 | ||
914 | while (--argc > 0 && **++argv == '-') { | |
915 | char *arg = *argv; | |
916 | while (*++arg) { | |
917 | switch (*arg) { | |
918 | case 'b': /* raw binary format */ | |
919 | if (fmt++) | |
920 | goto sep_err; | |
921 | sep = '\0'; | |
922 | break; | |
923 | case 'c': /* external checksum format */ | |
924 | if (fmt++) | |
925 | goto sep_err; | |
926 | sep = '\0'; | |
927 | chk = 1; | |
928 | break; | |
929 | case 't': /* text format */ | |
930 | if (fmt++) | |
931 | goto sep_err; | |
932 | sep = '\n'; | |
933 | break; | |
ecd1446f AH |
934 | case 'r': /* handle CRLF like LF */ |
935 | crlf_is_lf = 1; | |
936 | break; | |
ea882baf WD |
937 | case 'd': |
938 | del = 1; | |
939 | break; | |
940 | default: | |
4c12eeb8 | 941 | return CMD_RET_USAGE; |
ea882baf WD |
942 | } |
943 | } | |
944 | } | |
945 | ||
f3c615b8 | 946 | if (argc < 1) |
4c12eeb8 | 947 | return CMD_RET_USAGE; |
ea882baf WD |
948 | |
949 | if (!fmt) | |
950 | printf("## Warning: defaulting to text format\n"); | |
951 | ||
ecd1446f AH |
952 | if (sep != '\n' && crlf_is_lf ) |
953 | crlf_is_lf = 0; | |
954 | ||
7e5f460e | 955 | addr = hextoul(argv[0], NULL); |
fd37dac9 | 956 | ptr = map_sysmem(addr, 0); |
ea882baf | 957 | |
eaf73472 | 958 | if (argc >= 2 && strcmp(argv[1], "-")) { |
7e5f460e | 959 | size = hextoul(argv[1], NULL); |
eaf73472 | 960 | } else if (chk) { |
3775dcd9 TR |
961 | puts("## Error: external checksum format must pass size\n"); |
962 | return CMD_RET_FAILURE; | |
ea882baf | 963 | } else { |
fd37dac9 | 964 | char *s = ptr; |
ea882baf WD |
965 | |
966 | size = 0; | |
967 | ||
968 | while (size < MAX_ENV_SIZE) { | |
969 | if ((*s == sep) && (*(s+1) == '\0')) | |
970 | break; | |
971 | ++s; | |
972 | ++size; | |
973 | } | |
974 | if (size == MAX_ENV_SIZE) { | |
975 | printf("## Warning: Input data exceeds %d bytes" | |
976 | " - truncated\n", MAX_ENV_SIZE); | |
977 | } | |
d3f80c77 | 978 | size += 2; |
79afc88d | 979 | printf("## Info: input data size = %zu = 0x%zX\n", size, size); |
ea882baf WD |
980 | } |
981 | ||
eaf73472 QS |
982 | if (argc > 2) |
983 | wl = 1; | |
984 | ||
ea882baf WD |
985 | if (chk) { |
986 | uint32_t crc; | |
fd37dac9 | 987 | env_t *ep = (env_t *)ptr; |
ea882baf | 988 | |
142775a5 PA |
989 | if (size <= offsetof(env_t, data)) { |
990 | printf("## Error: Invalid size 0x%zX\n", size); | |
991 | return 1; | |
992 | } | |
993 | ||
ea882baf WD |
994 | size -= offsetof(env_t, data); |
995 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
996 | ||
997 | if (crc32(0, ep->data, size) != crc) { | |
998 | puts("## Error: bad CRC, import failed\n"); | |
999 | return 1; | |
1000 | } | |
fd37dac9 | 1001 | ptr = (char *)ep->data; |
ea882baf WD |
1002 | } |
1003 | ||
eaf73472 QS |
1004 | if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, |
1005 | crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) { | |
6c90f623 QS |
1006 | pr_err("## Error: Environment import failed: errno = %d\n", |
1007 | errno); | |
ea882baf WD |
1008 | return 1; |
1009 | } | |
1010 | gd->flags |= GD_FLG_ENV_READY; | |
1011 | ||
1012 | return 0; | |
1013 | ||
1014 | sep_err: | |
1015 | printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", | |
1016 | cmd); | |
1017 | return 1; | |
1018 | } | |
0c79cda0 | 1019 | #endif |
ea882baf | 1020 | |
ec57bd74 SDR |
1021 | #if defined(CONFIG_CMD_NVEDIT_INDIRECT) |
1022 | static int do_env_indirect(struct cmd_tbl *cmdtp, int flag, | |
1023 | int argc, char *const argv[]) | |
1024 | { | |
1025 | char *to = argv[1]; | |
1026 | char *from = argv[2]; | |
1027 | char *default_value = NULL; | |
1028 | int ret = 0; | |
1029 | ||
1030 | if (argc < 3 || argc > 4) { | |
1031 | return CMD_RET_USAGE; | |
1032 | } | |
1033 | ||
1034 | if (argc == 4) { | |
1035 | default_value = argv[3]; | |
1036 | } | |
1037 | ||
1038 | if (env_get(from) == NULL && default_value == NULL) { | |
1039 | printf("## env indirect: Environment variable for <from> (%s) does not exist.\n", from); | |
1040 | ||
1041 | return CMD_RET_FAILURE; | |
1042 | } | |
1043 | ||
1044 | if (env_get(from) == NULL) { | |
1045 | ret = env_set(to, default_value); | |
1046 | } | |
1047 | else { | |
1048 | ret = env_set(to, env_get(from)); | |
1049 | } | |
1050 | ||
1051 | if (ret == 0) { | |
1052 | return CMD_RET_SUCCESS; | |
1053 | } | |
1054 | else { | |
1055 | return CMD_RET_FAILURE; | |
1056 | } | |
1057 | } | |
1058 | #endif | |
1059 | ||
8e92120b LR |
1060 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
1061 | /* | |
1062 | * print_env_info - print environment information | |
1063 | */ | |
1064 | static int print_env_info(void) | |
1065 | { | |
1066 | const char *value; | |
1067 | ||
1068 | /* print environment validity value */ | |
1069 | switch (gd->env_valid) { | |
1070 | case ENV_INVALID: | |
1071 | value = "invalid"; | |
1072 | break; | |
1073 | case ENV_VALID: | |
1074 | value = "valid"; | |
1075 | break; | |
1076 | case ENV_REDUND: | |
1077 | value = "redundant"; | |
1078 | break; | |
1079 | default: | |
1080 | value = "unknown"; | |
1081 | break; | |
1082 | } | |
1083 | printf("env_valid = %s\n", value); | |
1084 | ||
1085 | /* print environment ready flag */ | |
1086 | value = gd->flags & GD_FLG_ENV_READY ? "true" : "false"; | |
1087 | printf("env_ready = %s\n", value); | |
1088 | ||
1089 | /* print environment using default flag */ | |
1090 | value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false"; | |
1091 | printf("env_use_default = %s\n", value); | |
1092 | ||
1093 | return CMD_RET_SUCCESS; | |
1094 | } | |
1095 | ||
1096 | #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */ | |
1097 | #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */ | |
1098 | ||
1099 | /* | |
1100 | * env info - display environment information | |
1101 | * env info [-d] - evaluate whether default environment is used | |
1102 | * env info [-p] - evaluate whether environment can be persisted | |
6718ebd0 PD |
1103 | * Add [-q] - quiet mode, use only for command result, for test by example: |
1104 | * test env info -p -d -q | |
8e92120b | 1105 | */ |
09140113 SG |
1106 | static int do_env_info(struct cmd_tbl *cmdtp, int flag, |
1107 | int argc, char *const argv[]) | |
8e92120b LR |
1108 | { |
1109 | int eval_flags = 0; | |
1110 | int eval_results = 0; | |
6718ebd0 | 1111 | bool quiet = false; |
2f96b323 PD |
1112 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
1113 | enum env_location loc; | |
1114 | #endif | |
8e92120b LR |
1115 | |
1116 | /* display environment information */ | |
1117 | if (argc <= 1) | |
1118 | return print_env_info(); | |
1119 | ||
1120 | /* process options */ | |
1121 | while (--argc > 0 && **++argv == '-') { | |
1122 | char *arg = *argv; | |
1123 | ||
1124 | while (*++arg) { | |
1125 | switch (*arg) { | |
1126 | case 'd': | |
1127 | eval_flags |= ENV_INFO_IS_DEFAULT; | |
1128 | break; | |
1129 | case 'p': | |
1130 | eval_flags |= ENV_INFO_IS_PERSISTED; | |
1131 | break; | |
6718ebd0 PD |
1132 | case 'q': |
1133 | quiet = true; | |
1134 | break; | |
8e92120b LR |
1135 | default: |
1136 | return CMD_RET_USAGE; | |
1137 | } | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | /* evaluate whether default environment is used */ | |
1142 | if (eval_flags & ENV_INFO_IS_DEFAULT) { | |
1143 | if (gd->flags & GD_FLG_ENV_DEFAULT) { | |
6718ebd0 PD |
1144 | if (!quiet) |
1145 | printf("Default environment is used\n"); | |
8e92120b LR |
1146 | eval_results |= ENV_INFO_IS_DEFAULT; |
1147 | } else { | |
6718ebd0 PD |
1148 | if (!quiet) |
1149 | printf("Environment was loaded from persistent storage\n"); | |
8e92120b LR |
1150 | } |
1151 | } | |
1152 | ||
1153 | /* evaluate whether environment can be persisted */ | |
1154 | if (eval_flags & ENV_INFO_IS_PERSISTED) { | |
8b5206d9 | 1155 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
2f96b323 PD |
1156 | loc = env_get_location(ENVOP_SAVE, gd->env_load_prio); |
1157 | if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) { | |
1158 | if (!quiet) | |
1159 | printf("Environment can be persisted\n"); | |
1160 | eval_results |= ENV_INFO_IS_PERSISTED; | |
1161 | } else { | |
1162 | if (!quiet) | |
1163 | printf("Environment cannot be persisted\n"); | |
1164 | } | |
8e92120b | 1165 | #else |
6718ebd0 PD |
1166 | if (!quiet) |
1167 | printf("Environment cannot be persisted\n"); | |
8e92120b LR |
1168 | #endif |
1169 | } | |
1170 | ||
1171 | /* The result of evaluations is combined with AND */ | |
1172 | if (eval_flags != eval_results) | |
1173 | return CMD_RET_FAILURE; | |
1174 | ||
1175 | return CMD_RET_SUCCESS; | |
1176 | } | |
1177 | #endif | |
1178 | ||
88733e2c | 1179 | #if defined(CONFIG_CMD_ENV_EXISTS) |
09140113 SG |
1180 | static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc, |
1181 | char *const argv[]) | |
88733e2c | 1182 | { |
dd2408ca | 1183 | struct env_entry e, *ep; |
88733e2c AR |
1184 | |
1185 | if (argc < 2) | |
1186 | return CMD_RET_USAGE; | |
1187 | ||
1188 | e.key = argv[1]; | |
1189 | e.data = NULL; | |
3f0d6807 | 1190 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
88733e2c AR |
1191 | |
1192 | return (ep == NULL) ? 1 : 0; | |
1193 | } | |
1194 | #endif | |
1195 | ||
ea882baf WD |
1196 | /* |
1197 | * New command line interface: "env" command with subcommands | |
1198 | */ | |
09140113 | 1199 | static struct cmd_tbl cmd_env_sub[] = { |
ea882baf WD |
1200 | #if defined(CONFIG_CMD_ASKENV) |
1201 | U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""), | |
1202 | #endif | |
1203 | U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""), | |
9d8d661d | 1204 | U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""), |
ea882baf WD |
1205 | #if defined(CONFIG_CMD_EDITENV) |
1206 | U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""), | |
1207 | #endif | |
5e2b3e0c JH |
1208 | #if defined(CONFIG_CMD_ENV_CALLBACK) |
1209 | U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""), | |
1210 | #endif | |
fffad71b JH |
1211 | #if defined(CONFIG_CMD_ENV_FLAGS) |
1212 | U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""), | |
1213 | #endif | |
0c79cda0 | 1214 | #if defined(CONFIG_CMD_EXPORTENV) |
ea882baf | 1215 | U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""), |
0c79cda0 | 1216 | #endif |
a000b795 KP |
1217 | #if defined(CONFIG_CMD_GREPENV) |
1218 | U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""), | |
1219 | #endif | |
0c79cda0 | 1220 | #if defined(CONFIG_CMD_IMPORTENV) |
ea882baf | 1221 | U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""), |
8e92120b | 1222 | #endif |
ec57bd74 SDR |
1223 | #if defined(CONFIG_CMD_NVEDIT_INDIRECT) |
1224 | U_BOOT_CMD_MKENT(indirect, 3, 0, do_env_indirect, "", ""), | |
1225 | #endif | |
8e92120b | 1226 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
6718ebd0 | 1227 | U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""), |
0115dd3a PD |
1228 | #endif |
1229 | #if defined(CONFIG_CMD_NVEDIT_LOAD) | |
1230 | U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""), | |
0c79cda0 | 1231 | #endif |
ea882baf WD |
1232 | U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""), |
1233 | #if defined(CONFIG_CMD_RUN) | |
1234 | U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""), | |
1235 | #endif | |
2643c85d | 1236 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
ea882baf | 1237 | U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""), |
cd121bdb FW |
1238 | #if defined(CONFIG_CMD_ERASEENV) |
1239 | U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""), | |
1240 | #endif | |
a97d22eb PD |
1241 | #endif |
1242 | #if defined(CONFIG_CMD_NVEDIT_SELECT) | |
1243 | U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""), | |
ea882baf WD |
1244 | #endif |
1245 | U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""), | |
88733e2c AR |
1246 | #if defined(CONFIG_CMD_ENV_EXISTS) |
1247 | U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""), | |
1248 | #endif | |
ea882baf WD |
1249 | }; |
1250 | ||
2e5167cc | 1251 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f HS |
1252 | void env_reloc(void) |
1253 | { | |
1254 | fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); | |
1255 | } | |
1256 | #endif | |
1257 | ||
09140113 | 1258 | static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
ea882baf | 1259 | { |
09140113 | 1260 | struct cmd_tbl *cp; |
ea882baf | 1261 | |
5904da02 | 1262 | if (argc < 2) |
4c12eeb8 | 1263 | return CMD_RET_USAGE; |
5904da02 | 1264 | |
ea882baf WD |
1265 | /* drop initial "env" arg */ |
1266 | argc--; | |
1267 | argv++; | |
1268 | ||
1269 | cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub)); | |
1270 | ||
1271 | if (cp) | |
1272 | return cp->cmd(cmdtp, flag, argc, argv); | |
1273 | ||
4c12eeb8 | 1274 | return CMD_RET_USAGE; |
ea882baf WD |
1275 | } |
1276 | ||
088f1b19 KP |
1277 | #ifdef CONFIG_SYS_LONGHELP |
1278 | static char env_help_text[] = | |
ea882baf WD |
1279 | #if defined(CONFIG_CMD_ASKENV) |
1280 | "ask name [message] [size] - ask for environment variable\nenv " | |
5e2b3e0c JH |
1281 | #endif |
1282 | #if defined(CONFIG_CMD_ENV_CALLBACK) | |
1283 | "callbacks - print callbacks and their associated variables\nenv " | |
ea882baf | 1284 | #endif |
b64b7c3d GF |
1285 | "default [-f] -a - [forcibly] reset default environment\n" |
1286 | "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" | |
9d8d661d | 1287 | "env delete [-f] var [...] - [forcibly] delete variable(s)\n" |
ea882baf WD |
1288 | #if defined(CONFIG_CMD_EDITENV) |
1289 | "env edit name - edit environment variable\n" | |
1290 | #endif | |
88733e2c AR |
1291 | #if defined(CONFIG_CMD_ENV_EXISTS) |
1292 | "env exists name - tests for existence of variable\n" | |
1293 | #endif | |
4796bc4b | 1294 | #if defined(CONFIG_CMD_EXPORTENV) |
37f2fe74 | 1295 | "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n" |
4796bc4b | 1296 | #endif |
fffad71b JH |
1297 | #if defined(CONFIG_CMD_ENV_FLAGS) |
1298 | "env flags - print variables that have non-default flags\n" | |
1299 | #endif | |
a000b795 | 1300 | #if defined(CONFIG_CMD_GREPENV) |
be29df6a WD |
1301 | #ifdef CONFIG_REGEX |
1302 | "env grep [-e] [-n | -v | -b] string [...] - search environment\n" | |
1303 | #else | |
d87244d5 | 1304 | "env grep [-n | -v | -b] string [...] - search environment\n" |
a000b795 | 1305 | #endif |
be29df6a | 1306 | #endif |
4796bc4b | 1307 | #if defined(CONFIG_CMD_IMPORTENV) |
eaf73472 | 1308 | "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n" |
8e92120b | 1309 | #endif |
ec57bd74 SDR |
1310 | #if defined(CONFIG_CMD_NVEDIT_INDIRECT) |
1311 | "env indirect <to> <from> [default] - sets <to> to the value of <from>, using [default] when unset\n" | |
1312 | #endif | |
8e92120b LR |
1313 | #if defined(CONFIG_CMD_NVEDIT_INFO) |
1314 | "env info - display environment information\n" | |
6718ebd0 PD |
1315 | "env info [-d] [-p] [-q] - evaluate environment information\n" |
1316 | " \"-d\": default environment is used\n" | |
1317 | " \"-p\": environment can be persisted\n" | |
1318 | " \"-q\": quiet output\n" | |
4796bc4b | 1319 | #endif |
be11235a | 1320 | "env print [-a | name ...] - print environment\n" |
49d81fdf | 1321 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
c70f4481 | 1322 | "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n" |
49d81fdf | 1323 | #endif |
ea882baf WD |
1324 | #if defined(CONFIG_CMD_RUN) |
1325 | "env run var [...] - run commands in an environment variable\n" | |
1326 | #endif | |
2643c85d | 1327 | #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) |
ea882baf | 1328 | "env save - save environment\n" |
cd121bdb FW |
1329 | #if defined(CONFIG_CMD_ERASEENV) |
1330 | "env erase - erase environment\n" | |
1331 | #endif | |
49d81fdf | 1332 | #endif |
0115dd3a PD |
1333 | #if defined(CONFIG_CMD_NVEDIT_LOAD) |
1334 | "env load - load environment\n" | |
a97d22eb PD |
1335 | #endif |
1336 | #if defined(CONFIG_CMD_NVEDIT_SELECT) | |
1337 | "env select [target] - select environment target\n" | |
0115dd3a | 1338 | #endif |
49d81fdf | 1339 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
8f0ac536 | 1340 | "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n" |
051aa89f | 1341 | " - set UEFI variable; unset if '-i' or 'arg' not specified\n" |
d798a9b5 | 1342 | #endif |
088f1b19 KP |
1343 | "env set [-f] name [arg ...]\n"; |
1344 | #endif | |
1345 | ||
1346 | U_BOOT_CMD( | |
1347 | env, CONFIG_SYS_MAXARGS, 1, do_env, | |
1348 | "environment handling commands", env_help_text | |
ea882baf WD |
1349 | ); |
1350 | ||
1351 | /* | |
1352 | * Old command line interface, kept for compatibility | |
1353 | */ | |
8bde7f77 | 1354 | |
246c6922 | 1355 | #if defined(CONFIG_CMD_EDITENV) |
722b061b | 1356 | U_BOOT_CMD_COMPLETE( |
ea882baf | 1357 | editenv, 2, 0, do_env_edit, |
246c6922 PT |
1358 | "edit environment variable", |
1359 | "name\n" | |
722b061b MF |
1360 | " - edit environment variable 'name'", |
1361 | var_complete | |
246c6922 PT |
1362 | ); |
1363 | #endif | |
1364 | ||
722b061b | 1365 | U_BOOT_CMD_COMPLETE( |
ea882baf | 1366 | printenv, CONFIG_SYS_MAXARGS, 1, do_env_print, |
2fb2604d | 1367 | "print environment variables", |
be11235a | 1368 | "[-a]\n - print [all] values of all environment variables\n" |
49d81fdf | 1369 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
c70f4481 | 1370 | "printenv -e [-guid guid][-n] [name ...]\n" |
49d81fdf | 1371 | " - print UEFI variable 'name' or all the variables\n" |
c70f4481 | 1372 | " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" |
051aa89f | 1373 | " \"-n\": suppress dumping variable's value\n" |
49d81fdf | 1374 | #endif |
8bde7f77 | 1375 | "printenv name ...\n" |
722b061b MF |
1376 | " - print value of environment variable 'name'", |
1377 | var_complete | |
8bde7f77 WD |
1378 | ); |
1379 | ||
a000b795 KP |
1380 | #ifdef CONFIG_CMD_GREPENV |
1381 | U_BOOT_CMD_COMPLETE( | |
1382 | grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep, | |
1383 | "search environment variables", | |
be29df6a WD |
1384 | #ifdef CONFIG_REGEX |
1385 | "[-e] [-n | -v | -b] string ...\n" | |
1386 | #else | |
d87244d5 | 1387 | "[-n | -v | -b] string ...\n" |
be29df6a | 1388 | #endif |
d87244d5 | 1389 | " - list environment name=value pairs matching 'string'\n" |
be29df6a WD |
1390 | #ifdef CONFIG_REGEX |
1391 | " \"-e\": enable regular expressions;\n" | |
1392 | #endif | |
d87244d5 WD |
1393 | " \"-n\": search variable names; \"-v\": search values;\n" |
1394 | " \"-b\": search both names and values (default)", | |
a000b795 KP |
1395 | var_complete |
1396 | ); | |
1397 | #endif | |
1398 | ||
722b061b | 1399 | U_BOOT_CMD_COMPLETE( |
ea882baf | 1400 | setenv, CONFIG_SYS_MAXARGS, 0, do_env_set, |
2fb2604d | 1401 | "set environment variables", |
49d81fdf | 1402 | #if defined(CONFIG_CMD_NVEDIT_EFI) |
e50e2878 | 1403 | "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n" |
8f0ac536 | 1404 | " [-i addr:size name], or [name [value ...]]\n" |
49d81fdf | 1405 | " - set UEFI variable 'name' to 'value' ...'\n" |
c70f4481 | 1406 | " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n" |
051aa89f AT |
1407 | " \"-nv\": set non-volatile attribute\n" |
1408 | " \"-bs\": set boot-service attribute\n" | |
1409 | " \"-rt\": set runtime attribute\n" | |
e50e2878 | 1410 | " \"-at\": set time-based authentication attribute\n" |
051aa89f AT |
1411 | " \"-a\": append-write\n" |
1412 | " \"-i addr,size\": use <addr,size> as variable's value\n" | |
1413 | " \"-v\": verbose message\n" | |
49d81fdf AT |
1414 | " - delete UEFI variable 'name' if 'value' not specified\n" |
1415 | #endif | |
1416 | "setenv [-f] name value ...\n" | |
24ab5a19 JH |
1417 | " - [forcibly] set environment variable 'name' to 'value ...'\n" |
1418 | "setenv [-f] name\n" | |
1419 | " - [forcibly] delete environment variable 'name'", | |
722b061b | 1420 | var_complete |
8bde7f77 WD |
1421 | ); |
1422 | ||
c76fe474 | 1423 | #if defined(CONFIG_CMD_ASKENV) |
8bde7f77 | 1424 | |
0d498393 | 1425 | U_BOOT_CMD( |
ea882baf | 1426 | askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask, |
2fb2604d | 1427 | "get environment variables from stdin", |
8bde7f77 | 1428 | "name [message] [size]\n" |
7d85591d | 1429 | " - get environment variable 'name' from stdin (max 'size' chars)" |
8bde7f77 | 1430 | ); |
90253178 | 1431 | #endif |
8bde7f77 | 1432 | |
c76fe474 | 1433 | #if defined(CONFIG_CMD_RUN) |
722b061b | 1434 | U_BOOT_CMD_COMPLETE( |
6d0f6bcf | 1435 | run, CONFIG_SYS_MAXARGS, 1, do_run, |
2fb2604d | 1436 | "run commands in an environment variable", |
8bde7f77 | 1437 | "var [...]\n" |
722b061b MF |
1438 | " - run the commands in the environment variable(s) 'var'", |
1439 | var_complete | |
8bde7f77 | 1440 | ); |
90253178 | 1441 | #endif |
7ac2fe2d | 1442 | #endif /* CONFIG_SPL_BUILD */ |