1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
10 #include <bootstage.h>
13 #include <env_internal.h>
16 #include <asm/global_data.h>
17 #include <linux/printk.h>
18 #include <linux/stddef.h>
22 #include <u-boot/crc.h>
23 #include <dm/ofnode.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 /************************************************************************
30 * Default settings to be used when no valid environment is found
32 #include <env_default.h>
34 struct hsearch_data env_htab = {
35 .change_ok = env_flags_validate,
39 * This variable is incremented each time we set an environment variable so we
40 * can be check via env_get_id() to see if the environment has changed or not.
41 * This makes it possible to reread an environment variable only if the
42 * environment was changed, typically used by networking code.
44 static int env_id = 1;
56 int env_do_env_set(int flag, int argc, char *const argv[], int env_flag)
59 char *name, *value, *s;
60 struct env_entry e, *ep;
62 debug("Initial value for argc=%d\n", argc);
64 #if !IS_ENABLED(CONFIG_SPL_BUILD) && IS_ENABLED(CONFIG_CMD_NVEDIT_EFI)
65 if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'e')
66 return do_env_set_efi(NULL, flag, --argc, ++argv);
69 while (argc > 1 && **(argv + 1) == '-') {
83 debug("Final value for argc=%d\n", argc);
86 if (strchr(name, '=')) {
87 printf("## Error: illegal character '=' "
88 "in variable name \"%s\"\n", name);
95 if (argc < 3 || argv[2] == NULL) {
96 int rc = hdelete_r(name, &env_htab, env_flag);
98 /* If the variable didn't exist, don't report an error */
99 return rc && rc != -ENOENT ? 1 : 0;
103 * Insert / replace new value
105 for (i = 2, len = 0; i < argc; ++i)
106 len += strlen(argv[i]) + 1;
110 printf("## Can't malloc %d bytes\n", len);
113 for (i = 2, s = value; i < argc; ++i) {
116 while ((*s++ = *v++) != '\0')
125 hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
128 printf("## Error inserting \"%s\" variable, errno=%d\n",
136 int env_set(const char *varname, const char *varvalue)
138 const char * const argv[4] = { "setenv", varname, varvalue, NULL };
140 /* before import into hashtable */
141 if (!(gd->flags & GD_FLG_ENV_READY))
144 if (varvalue == NULL || varvalue[0] == '\0')
145 return env_do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
147 return env_do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
151 * Set an environment variable to an integer value
153 * @param varname Environment variable to set
154 * @param value Value to set it to
155 * Return: 0 if ok, 1 on error
157 int env_set_ulong(const char *varname, ulong value)
159 /* TODO: this should be unsigned */
160 char *str = simple_itoa(value);
162 return env_set(varname, str);
166 * Set an environment variable to an value in hex
168 * @param varname Environment variable to set
169 * @param value Value to set it to
170 * Return: 0 if ok, 1 on error
172 int env_set_hex(const char *varname, ulong value)
176 sprintf(str, "%lx", value);
177 return env_set(varname, str);
180 ulong env_get_hex(const char *varname, ulong default_val)
186 s = env_get(varname);
188 value = hextoul(s, &endp);
195 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
197 string_to_enetaddr(env_get(name), enetaddr);
198 return is_valid_ethaddr(enetaddr);
201 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
203 char buf[ARP_HLEN_ASCII + 1];
205 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
208 sprintf(buf, "%pM", enetaddr);
210 return env_set(name, buf);
214 * Look up variable from environment,
215 * return address of storage for that variable,
216 * or NULL if not found
218 char *env_get(const char *name)
220 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
221 struct env_entry e, *ep;
227 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
229 return ep ? ep->data : NULL;
232 /* restricted capabilities before import */
233 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
234 return (char *)(gd->env_buf);
240 * Like env_get, but prints an error if envvar isn't defined in the
241 * environment. It always returns what env_get does, so it can be used in
242 * place of env_get without changing error handling otherwise.
244 char *from_env(const char *envvar)
248 ret = env_get(envvar);
251 printf("missing environment variable: %s\n", envvar);
256 static int env_get_from_linear(const char *env, const char *name, char *buf,
262 if (name == NULL || *name == '\0')
265 name_len = strlen(name);
267 for (p = env; *p != '\0'; p = end + 1) {
271 for (end = p; *end != '\0'; ++end)
272 if (end - env >= CONFIG_ENV_SIZE)
275 if (strncmp(name, p, name_len) || p[name_len] != '=')
277 value = &p[name_len + 1];
280 memcpy(buf, value, min(len, res + 1));
284 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
295 * Look up variable from environment for restricted C runtime env.
297 int env_get_f(const char *name, char *buf, unsigned len)
301 if (gd->env_valid == ENV_INVALID)
302 env = default_environment;
304 env = (const char *)gd->env_addr;
306 return env_get_from_linear(env, name, buf, len);
310 * Decode the integer value of an environment variable and return it.
312 * @param name Name of environment variable
313 * @param base Number base to use (normally 10, or 16 for hex)
314 * @param default_val Default value to return if the variable is not
316 * Return: the decoded value, or default_val if not found
318 ulong env_get_ulong(const char *name, int base, ulong default_val)
321 * We can use env_get() here, even before relocation, since the
322 * environment variable value is an integer and thus short.
324 const char *str = env_get(name);
326 return str ? simple_strtoul(str, NULL, base) : default_val;
330 * Read an environment variable as a boolean
331 * Return -1 if variable does not exist (default to true)
333 int env_get_yesno(const char *var)
335 char *s = env_get(var);
339 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
343 bool env_get_autostart(void)
345 return env_get_yesno("autostart") == 1;
349 * Look up the variable from the default environment
351 char *env_get_default(const char *name)
355 ret = env_get_default_into(name, (char *)(gd->env_buf),
356 sizeof(gd->env_buf));
358 return (char *)(gd->env_buf);
364 * Look up the variable from the default environment and store its value in buf
366 int env_get_default_into(const char *name, char *buf, unsigned int len)
368 return env_get_from_linear(default_environment, name, buf, len);
371 void env_set_default(const char *s, int flags)
374 if ((flags & H_INTERACTIVE) == 0) {
375 printf("*** Warning - %s, "
376 "using default environment\n\n", s);
381 debug("Using default environment\n");
385 if (himport_r(&env_htab, default_environment,
386 sizeof(default_environment), '\0', flags, 0,
388 pr_err("## Error: Environment import failed: errno = %d\n",
393 gd->flags |= GD_FLG_ENV_READY;
394 gd->flags |= GD_FLG_ENV_DEFAULT;
397 /* [re]set individual variables to their value in the default environment */
398 int env_set_default_vars(int nvars, char * const vars[], int flags)
401 * Special use-case: import from default environment
402 * (and use \0 as a separator)
404 flags |= H_NOCLEAR | H_DEFAULT;
405 return himport_r(&env_htab, default_environment,
406 sizeof(default_environment), '\0',
407 flags, 0, nvars, vars);
411 * Check if CRC is valid and (if yes) import the environment.
412 * Note that "buf" may or may not be aligned.
414 int env_import(const char *buf, int check, int flags)
416 env_t *ep = (env_t *)buf;
421 memcpy(&crc, &ep->crc, sizeof(crc));
423 if (crc32(0, ep->data, ENV_SIZE) != crc) {
424 env_set_default("bad CRC", 0);
425 return -ENOMSG; /* needed for env_load() */
429 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
431 gd->flags |= GD_FLG_ENV_READY;
435 pr_err("Cannot import environment: errno = %d\n", errno);
437 env_set_default("import failed", 0);
442 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
443 static unsigned char env_flags;
445 int env_check_redund(const char *buf1, int buf1_read_fail,
446 const char *buf2, int buf2_read_fail)
448 int crc1_ok = 0, crc2_ok = 0;
449 env_t *tmp_env1, *tmp_env2;
451 tmp_env1 = (env_t *)buf1;
452 tmp_env2 = (env_t *)buf2;
454 if (buf1_read_fail && buf2_read_fail) {
455 puts("*** Error - No Valid Environment Area found\n");
457 } else if (buf1_read_fail || buf2_read_fail) {
458 puts("*** Warning - some problems detected ");
459 puts("reading environment; recovered successfully\n");
463 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
466 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
469 if (!crc1_ok && !crc2_ok) {
470 gd->env_valid = ENV_INVALID;
471 return -ENOMSG; /* needed for env_load() */
472 } else if (crc1_ok && !crc2_ok) {
473 gd->env_valid = ENV_VALID;
474 } else if (!crc1_ok && crc2_ok) {
475 gd->env_valid = ENV_REDUND;
477 /* both ok - check serial */
478 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
479 gd->env_valid = ENV_REDUND;
480 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
481 gd->env_valid = ENV_VALID;
482 else if (tmp_env1->flags > tmp_env2->flags)
483 gd->env_valid = ENV_VALID;
484 else if (tmp_env2->flags > tmp_env1->flags)
485 gd->env_valid = ENV_REDUND;
486 else /* flags are equal - almost impossible */
487 gd->env_valid = ENV_VALID;
493 int env_import_redund(const char *buf1, int buf1_read_fail,
494 const char *buf2, int buf2_read_fail,
500 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
503 env_set_default("bad env area", 0);
505 } else if (ret == -ENOMSG) {
506 env_set_default("bad CRC", 0);
510 if (gd->env_valid == ENV_VALID)
515 env_flags = ep->flags;
517 return env_import((char *)ep, 0, flags);
519 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
521 /* Export the environment and generate CRC for it. */
522 int env_export(env_t *env_out)
527 res = (char *)env_out->data;
528 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
530 pr_err("Cannot export environment: errno = %d\n", errno);
534 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
536 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
537 env_out->flags = ++env_flags; /* increase the serial */
543 void env_relocate(void)
545 if (gd->env_valid == ENV_INVALID) {
546 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
547 /* Environment not changable */
548 env_set_default(NULL, 0);
550 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
551 env_set_default("bad CRC", 0);
558 #ifdef CONFIG_AUTO_COMPLETE
559 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
562 struct env_entry *match;
567 * When doing $ completion, the first character should
568 * obviously be a '$'.
576 * The second one, if present, should be a '{', as some
577 * configuration of the u-boot shell expand ${var} but not
582 else if (var[0] != '\0')
590 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
591 int vallen = strlen(match->key) + 1;
593 if (found >= maxv - 2 ||
594 bufsz < vallen + (dollar_comp ? 3 : 0))
599 /* Add the '${' prefix to each var when doing $ completion. */
606 memcpy(buf, match->key, vallen);
612 * This one is a bit odd: vallen already contains the
613 * '\0' character but we need to add the '}' suffix,
614 * hence the buf - 1 here. strcpy() will add the '\0'
615 * character just after '}'. buf is then incremented
616 * to account for the extra '}' we just added.
618 strcpy(buf - 1, "}");
623 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
626 cmdv[found++] = dollar_comp ? "${...}" : "...";
633 #ifdef CONFIG_ENV_IMPORT_FDT
634 void env_import_fdt(void)
641 path = env_get("env_fdt_path");
642 if (!path || !path[0])
645 node = ofnode_path(path);
646 if (!ofnode_valid(node)) {
647 printf("Warning: device tree node '%s' not found\n", path);
651 for (res = ofnode_first_property(node, &prop);
653 res = ofnode_next_property(&prop)) {
654 const char *name, *val;
656 val = ofprop_get_property(&prop, &name, NULL);