]> Git Repo - J-u-boot.git/blame - env/common.c
env: allow to copy value from default environment into a buffer
[J-u-boot.git] / env / common.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
05706fdd 2/*
ea882baf 3 * (C) Copyright 2000-2010
05706fdd
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]>
05706fdd
WD
8 */
9
10#include <common.h>
52f24238 11#include <bootstage.h>
05706fdd 12#include <command.h>
3f989e7b 13#include <env.h>
f3998fdc 14#include <env_internal.h>
f7ae49fc 15#include <log.h>
8bef79bf 16#include <sort.h>
401d1c4f 17#include <asm/global_data.h>
1e94b46f 18#include <linux/printk.h>
05706fdd 19#include <linux/stddef.h>
ea882baf
WD
20#include <search.h>
21#include <errno.h>
05706fdd 22#include <malloc.h>
3db71108 23#include <u-boot/crc.h>
95fd9772 24#include <dm/ofnode.h>
f2315664
MB
25#include <net.h>
26#include <watchdog.h>
05706fdd 27
d87080b7
WD
28DECLARE_GLOBAL_DATA_PTR;
29
05706fdd
WD
30/************************************************************************
31 * Default settings to be used when no valid environment is found
32 */
ddd8418f 33#include <env_default.h>
05706fdd 34
c5983592 35struct hsearch_data env_htab = {
2598090b 36 .change_ok = env_flags_validate,
c5983592 37};
2eb1573f 38
f2315664 39/*
d9721925
TR
40 * This variable is incremented each time we set an environment variable so we
41 * can be check via env_get_id() to see if the environment has changed or not.
42 * This makes it possible to reread an environment variable only if the
43 * environment was changed, typically used by networking code.
f2315664 44 */
d9721925
TR
45static int env_id = 1;
46
47int env_get_id(void)
48{
49 return env_id;
50}
51
52void env_inc_id(void)
53{
54 env_id++;
55}
56
57int env_do_env_set(int flag, int argc, char *const argv[], int env_flag)
58{
59 int i, len;
60 char *name, *value, *s;
61 struct env_entry e, *ep;
62
63 debug("Initial value for argc=%d\n", argc);
64
65#if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
66 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
67 return do_env_set_efi(NULL, flag, --argc, ++argv);
68#endif
69
70 while (argc > 1 && **(argv + 1) == '-') {
71 char *arg = *++argv;
72
73 --argc;
74 while (*++arg) {
75 switch (*arg) {
76 case 'f': /* force */
77 env_flag |= H_FORCE;
78 break;
79 default:
80 return CMD_RET_USAGE;
81 }
82 }
83 }
84 debug("Final value for argc=%d\n", argc);
85 name = argv[1];
86
87 if (strchr(name, '=')) {
88 printf("## Error: illegal character '='"
89 "in variable name \"%s\"\n", name);
90 return 1;
91 }
92
93 env_inc_id();
94
95 /* Delete only ? */
96 if (argc < 3 || argv[2] == NULL) {
97 int rc = hdelete_r(name, &env_htab, env_flag);
98
99 /* If the variable didn't exist, don't report an error */
100 return rc && rc != -ENOENT ? 1 : 0;
101 }
102
103 /*
104 * Insert / replace new value
105 */
106 for (i = 2, len = 0; i < argc; ++i)
107 len += strlen(argv[i]) + 1;
108
109 value = malloc(len);
110 if (value == NULL) {
111 printf("## Can't malloc %d bytes\n", len);
112 return 1;
113 }
114 for (i = 2, s = value; i < argc; ++i) {
115 char *v = argv[i];
116
117 while ((*s++ = *v++) != '\0')
118 ;
119 *(s - 1) = ' ';
120 }
121 if (s != value)
122 *--s = '\0';
123
124 e.key = name;
125 e.data = value;
126 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
127 free(value);
128 if (!ep) {
129 printf("## Error inserting \"%s\" variable, errno=%d\n",
130 name, errno);
131 return 1;
132 }
133
134 return 0;
135}
136
137int env_set(const char *varname, const char *varvalue)
138{
139 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
140
141 /* before import into hashtable */
142 if (!(gd->flags & GD_FLG_ENV_READY))
143 return 1;
144
145 if (varvalue == NULL || varvalue[0] == '\0')
146 return env_do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
147 else
148 return env_do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
149}
f2315664
MB
150
151/**
152 * Set an environment variable to an integer value
153 *
154 * @param varname Environment variable to set
155 * @param value Value to set it to
185f812c 156 * Return: 0 if ok, 1 on error
f2315664
MB
157 */
158int env_set_ulong(const char *varname, ulong value)
159{
160 /* TODO: this should be unsigned */
161 char *str = simple_itoa(value);
162
163 return env_set(varname, str);
164}
165
166/**
167 * Set an environment variable to an value in hex
168 *
169 * @param varname Environment variable to set
170 * @param value Value to set it to
185f812c 171 * Return: 0 if ok, 1 on error
f2315664
MB
172 */
173int env_set_hex(const char *varname, ulong value)
174{
175 char str[17];
176
177 sprintf(str, "%lx", value);
178 return env_set(varname, str);
179}
180
181ulong env_get_hex(const char *varname, ulong default_val)
182{
183 const char *s;
184 ulong value;
185 char *endp;
186
187 s = env_get(varname);
188 if (s)
189 value = hextoul(s, &endp);
190 if (!s || endp == s)
191 return default_val;
192
193 return value;
194}
195
196int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
197{
198 string_to_enetaddr(env_get(name), enetaddr);
199 return is_valid_ethaddr(enetaddr);
200}
201
202int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
203{
204 char buf[ARP_HLEN_ASCII + 1];
205
206 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
207 return -EEXIST;
208
209 sprintf(buf, "%pM", enetaddr);
210
211 return env_set(name, buf);
212}
213
214/*
215 * Look up variable from environment,
216 * return address of storage for that variable,
217 * or NULL if not found
218 */
219char *env_get(const char *name)
220{
221 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
222 struct env_entry e, *ep;
223
29caf930 224 schedule();
f2315664
MB
225
226 e.key = name;
227 e.data = NULL;
228 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
229
230 return ep ? ep->data : NULL;
231 }
232
233 /* restricted capabilities before import */
e8459c12 234 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
f2315664
MB
235 return (char *)(gd->env_buf);
236
237 return NULL;
238}
239
240/*
241 * Like env_get, but prints an error if envvar isn't defined in the
242 * environment. It always returns what env_get does, so it can be used in
243 * place of env_get without changing error handling otherwise.
244 */
245char *from_env(const char *envvar)
246{
247 char *ret;
248
249 ret = env_get(envvar);
250
251 if (!ret)
252 printf("missing environment variable: %s\n", envvar);
253
254 return ret;
255}
256
4e7c8b2a
MB
257static int env_get_from_linear(const char *env, const char *name, char *buf,
258 unsigned len)
f2315664 259{
4e7c8b2a 260 const char *p, *end;
f2315664
MB
261 size_t name_len;
262
263 if (name == NULL || *name == '\0')
264 return -1;
265
266 name_len = strlen(name);
267
f2315664
MB
268 for (p = env; *p != '\0'; p = end + 1) {
269 const char *value;
270 unsigned res;
271
272 for (end = p; *end != '\0'; ++end)
273 if (end - env >= CONFIG_ENV_SIZE)
274 return -1;
275
276 if (strncmp(name, p, name_len) || p[name_len] != '=')
277 continue;
278 value = &p[name_len + 1];
279
280 res = end - value;
281 memcpy(buf, value, min(len, res + 1));
282
283 if (len <= res) {
284 buf[len - 1] = '\0';
285 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
286 len, name);
287 }
288
289 return res;
290 }
291
292 return -1;
293}
294
4e7c8b2a
MB
295/*
296 * Look up variable from environment for restricted C runtime env.
297 */
298int env_get_f(const char *name, char *buf, unsigned len)
299{
300 const char *env;
301
302 if (gd->env_valid == ENV_INVALID)
303 env = default_environment;
304 else
305 env = (const char *)gd->env_addr;
306
307 return env_get_from_linear(env, name, buf, len);
308}
309
f2315664
MB
310/**
311 * Decode the integer value of an environment variable and return it.
312 *
313 * @param name Name of environment variable
314 * @param base Number base to use (normally 10, or 16 for hex)
315 * @param default_val Default value to return if the variable is not
316 * found
185f812c 317 * Return: the decoded value, or default_val if not found
f2315664
MB
318 */
319ulong env_get_ulong(const char *name, int base, ulong default_val)
320{
321 /*
322 * We can use env_get() here, even before relocation, since the
323 * environment variable value is an integer and thus short.
324 */
325 const char *str = env_get(name);
326
327 return str ? simple_strtoul(str, NULL, base) : default_val;
328}
329
ec8a252c
JH
330/*
331 * Read an environment variable as a boolean
332 * Return -1 if variable does not exist (default to true)
333 */
bfebc8c9 334int env_get_yesno(const char *var)
ec8a252c 335{
00caae6d 336 char *s = env_get(var);
ec8a252c
JH
337
338 if (s == NULL)
339 return -1;
340 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
341 1 : 0;
342}
343
78398652
SG
344bool env_get_autostart(void)
345{
346 return env_get_yesno("autostart") == 1;
347}
348
267541f7
JH
349/*
350 * Look up the variable from the default environment
351 */
723806cc 352char *env_get_default(const char *name)
267541f7 353{
4e7c8b2a
MB
354 if (env_get_from_linear(default_environment, name,
355 (char *)(gd->env_buf),
356 sizeof(gd->env_buf)) >= 0)
357 return (char *)(gd->env_buf);
358
359 return NULL;
267541f7
JH
360}
361
23c1ab92
QS
362/*
363 * Look up the variable from the default environment and store its value in buf
364 */
365int env_get_default_into(const char *name, char *buf, unsigned int len)
366{
367 return env_get_from_linear(default_environment, name, buf, len);
368}
369
0ac7d722 370void env_set_default(const char *s, int flags)
5bb12dbd 371{
ea882baf 372 if (s) {
c5d548a9 373 if ((flags & H_INTERACTIVE) == 0) {
ea882baf 374 printf("*** Warning - %s, "
c5d548a9 375 "using default environment\n\n", s);
ea882baf
WD
376 } else {
377 puts(s);
378 }
379 } else {
58ae9990 380 debug("Using default environment\n");
ea882baf
WD
381 }
382
ef9bef2b 383 flags |= H_DEFAULT;
c5cbbe35 384 if (himport_r(&env_htab, default_environment,
ecd1446f 385 sizeof(default_environment), '\0', flags, 0,
c9db4c54 386 0, NULL) == 0) {
6c90f623
QS
387 pr_err("## Error: Environment import failed: errno = %d\n",
388 errno);
c9db4c54
MB
389 return;
390 }
27aafe98 391
ea882baf 392 gd->flags |= GD_FLG_ENV_READY;
340b0e3b 393 gd->flags |= GD_FLG_ENV_DEFAULT;
5bb12dbd
HW
394}
395
b64b7c3d
GF
396
397/* [re]set individual variables to their value in the default environment */
0b9d8a05 398int env_set_default_vars(int nvars, char * const vars[], int flags)
b64b7c3d
GF
399{
400 /*
401 * Special use-case: import from default environment
402 * (and use \0 as a separator)
403 */
ef9bef2b 404 flags |= H_NOCLEAR | H_DEFAULT;
c5cbbe35 405 return himport_r(&env_htab, default_environment,
c4e0057f 406 sizeof(default_environment), '\0',
477f8116 407 flags, 0, nvars, vars);
b64b7c3d
GF
408}
409
ea882baf
WD
410/*
411 * Check if CRC is valid and (if yes) import the environment.
412 * Note that "buf" may or may not be aligned.
413 */
890feeca 414int env_import(const char *buf, int check, int flags)
05706fdd 415{
ea882baf 416 env_t *ep = (env_t *)buf;
05706fdd 417
ea882baf
WD
418 if (check) {
419 uint32_t crc;
420
421 memcpy(&crc, &ep->crc, sizeof(crc));
422
423 if (crc32(0, ep->data, ENV_SIZE) != crc) {
0ac7d722 424 env_set_default("bad CRC", 0);
49d04c58 425 return -ENOMSG; /* needed for env_load() */
ea882baf
WD
426 }
427 }
428
890feeca 429 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
c4e0057f 430 0, NULL)) {
ea882baf 431 gd->flags |= GD_FLG_ENV_READY;
42a1820b 432 return 0;
ea882baf 433 }
05706fdd 434
9b643e31 435 pr_err("Cannot import environment: errno = %d\n", errno);
ea882baf 436
0ac7d722 437 env_set_default("import failed", 0);
ea882baf 438
42a1820b 439 return -EIO;
ea882baf
WD
440}
441
76768f5f
FA
442#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
443static unsigned char env_flags;
444
1229533a
HS
445int env_check_redund(const char *buf1, int buf1_read_fail,
446 const char *buf2, int buf2_read_fail)
76768f5f 447{
b9c3052f 448 int crc1_ok = 0, crc2_ok = 0;
1229533a 449 env_t *tmp_env1, *tmp_env2;
76768f5f
FA
450
451 tmp_env1 = (env_t *)buf1;
452 tmp_env2 = (env_t *)buf2;
453
31f044bd
SG
454 if (buf1_read_fail && buf2_read_fail) {
455 puts("*** Error - No Valid Environment Area found\n");
b9c3052f 456 return -EIO;
31f044bd
SG
457 } else if (buf1_read_fail || buf2_read_fail) {
458 puts("*** Warning - some problems detected ");
459 puts("reading environment; recovered successfully\n");
460 }
461
b9c3052f
BM
462 if (!buf1_read_fail)
463 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
464 tmp_env1->crc;
465 if (!buf2_read_fail)
466 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
467 tmp_env2->crc;
76768f5f
FA
468
469 if (!crc1_ok && !crc2_ok) {
4dc5e262 470 gd->env_valid = ENV_INVALID;
49d04c58 471 return -ENOMSG; /* needed for env_load() */
76768f5f 472 } else if (crc1_ok && !crc2_ok) {
2d7cb5b4 473 gd->env_valid = ENV_VALID;
76768f5f 474 } else if (!crc1_ok && crc2_ok) {
2d7cb5b4 475 gd->env_valid = ENV_REDUND;
76768f5f
FA
476 } else {
477 /* both ok - check serial */
478 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
2d7cb5b4 479 gd->env_valid = ENV_REDUND;
76768f5f 480 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
2d7cb5b4 481 gd->env_valid = ENV_VALID;
76768f5f 482 else if (tmp_env1->flags > tmp_env2->flags)
2d7cb5b4 483 gd->env_valid = ENV_VALID;
76768f5f 484 else if (tmp_env2->flags > tmp_env1->flags)
2d7cb5b4 485 gd->env_valid = ENV_REDUND;
76768f5f 486 else /* flags are equal - almost impossible */
2d7cb5b4 487 gd->env_valid = ENV_VALID;
76768f5f
FA
488 }
489
1229533a
HS
490 return 0;
491}
492
493int env_import_redund(const char *buf1, int buf1_read_fail,
494 const char *buf2, int buf2_read_fail,
495 int flags)
496{
497 env_t *ep;
498 int ret;
499
500 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
501
502 if (ret == -EIO) {
503 env_set_default("bad env area", 0);
504 return -EIO;
1229533a
HS
505 } else if (ret == -ENOMSG) {
506 env_set_default("bad CRC", 0);
507 return -ENOMSG;
508 }
509
2d7cb5b4 510 if (gd->env_valid == ENV_VALID)
1229533a 511 ep = (env_t *)buf1;
76768f5f 512 else
1229533a 513 ep = (env_t *)buf2;
76768f5f
FA
514
515 env_flags = ep->flags;
1229533a 516
890feeca 517 return env_import((char *)ep, 0, flags);
76768f5f
FA
518}
519#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
520
fc0b5948 521/* Export the environment and generate CRC for it. */
7ce1526e
MV
522int env_export(env_t *env_out)
523{
524 char *res;
525 ssize_t len;
526
527 res = (char *)env_out->data;
528 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
529 if (len < 0) {
9b643e31 530 pr_err("Cannot export environment: errno = %d\n", errno);
7ce1526e
MV
531 return 1;
532 }
a4223b74 533
7ce1526e
MV
534 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
535
76768f5f
FA
536#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
537 env_out->flags = ++env_flags; /* increase the serial */
538#endif
539
7ce1526e
MV
540 return 0;
541}
542
27aafe98 543void env_relocate(void)
ea882baf 544{
2d7cb5b4 545 if (gd->env_valid == ENV_INVALID) {
7ac2fe2d
IY
546#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
547 /* Environment not changable */
0ac7d722 548 env_set_default(NULL, 0);
05706fdd 549#else
770605e4 550 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
0ac7d722 551 env_set_default("bad CRC", 0);
d259079d 552#endif
ea882baf 553 } else {
310fb14b 554 env_load();
05706fdd 555 }
05706fdd 556}
04a85b3b 557
03dcf17d
BB
558#ifdef CONFIG_AUTO_COMPLETE
559int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
560 bool dollar_comp)
04a85b3b 561{
dd2408ca 562 struct env_entry *match;
560d424b 563 int found, idx;
04a85b3b 564
03dcf17d
BB
565 if (dollar_comp) {
566 /*
567 * When doing $ completion, the first character should
568 * obviously be a '$'.
569 */
570 if (var[0] != '$')
571 return 0;
572
573 var++;
574
575 /*
576 * The second one, if present, should be a '{', as some
577 * configuration of the u-boot shell expand ${var} but not
578 * $var.
579 */
580 if (var[0] == '{')
581 var++;
582 else if (var[0] != '\0')
583 return 0;
584 }
585
560d424b 586 idx = 0;
04a85b3b
WD
587 found = 0;
588 cmdv[0] = NULL;
589
03dcf17d 590
560d424b
MF
591 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
592 int vallen = strlen(match->key) + 1;
04a85b3b 593
03dcf17d
BB
594 if (found >= maxv - 2 ||
595 bufsz < vallen + (dollar_comp ? 3 : 0))
04a85b3b 596 break;
560d424b 597
04a85b3b 598 cmdv[found++] = buf;
03dcf17d
BB
599
600 /* Add the '${' prefix to each var when doing $ completion. */
601 if (dollar_comp) {
602 strcpy(buf, "${");
603 buf += 2;
604 bufsz -= 3;
605 }
606
560d424b
MF
607 memcpy(buf, match->key, vallen);
608 buf += vallen;
609 bufsz -= vallen;
03dcf17d
BB
610
611 if (dollar_comp) {
612 /*
613 * This one is a bit odd: vallen already contains the
614 * '\0' character but we need to add the '}' suffix,
615 * hence the buf - 1 here. strcpy() will add the '\0'
616 * character just after '}'. buf is then incremented
617 * to account for the extra '}' we just added.
618 */
619 strcpy(buf - 1, "}");
620 buf++;
621 }
04a85b3b
WD
622 }
623
560d424b
MF
624 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
625
626 if (idx)
03dcf17d 627 cmdv[found++] = dollar_comp ? "${...}" : "...";
27aafe98 628
04a85b3b
WD
629 cmdv[found] = NULL;
630 return found;
631}
632#endif
95fd9772
RV
633
634#ifdef CONFIG_ENV_IMPORT_FDT
635void env_import_fdt(void)
636{
637 const char *path;
638 struct ofprop prop;
639 ofnode node;
640 int res;
641
642 path = env_get("env_fdt_path");
643 if (!path || !path[0])
644 return;
645
646 node = ofnode_path(path);
647 if (!ofnode_valid(node)) {
648 printf("Warning: device tree node '%s' not found\n", path);
649 return;
650 }
651
4b1f5714 652 for (res = ofnode_first_property(node, &prop);
95fd9772 653 !res;
4b1f5714 654 res = ofnode_next_property(&prop)) {
95fd9772
RV
655 const char *name, *val;
656
92432246 657 val = ofprop_get_property(&prop, &name, NULL);
95fd9772
RV
658 env_set(name, val);
659 }
660}
661#endif
This page took 0.686379 seconds and 4 git commands to generate.