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>
11 #include <bootstage.h>
14 #include <env_internal.h>
17 #include <asm/global_data.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 env_set() function is defined in cmd/nvedit.c, since it calls
40 * _do_env_set(), whis is a static function in that file.
42 * int env_set(const char *varname, const char *varvalue);
46 * Set an environment variable to an integer value
48 * @param varname Environment variable to set
49 * @param value Value to set it to
50 * Return: 0 if ok, 1 on error
52 int env_set_ulong(const char *varname, ulong value)
54 /* TODO: this should be unsigned */
55 char *str = simple_itoa(value);
57 return env_set(varname, str);
61 * Set an environment variable to an value in hex
63 * @param varname Environment variable to set
64 * @param value Value to set it to
65 * Return: 0 if ok, 1 on error
67 int env_set_hex(const char *varname, ulong value)
71 sprintf(str, "%lx", value);
72 return env_set(varname, str);
75 ulong env_get_hex(const char *varname, ulong default_val)
83 value = hextoul(s, &endp);
90 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
92 string_to_enetaddr(env_get(name), enetaddr);
93 return is_valid_ethaddr(enetaddr);
96 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
98 char buf[ARP_HLEN_ASCII + 1];
100 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
103 sprintf(buf, "%pM", enetaddr);
105 return env_set(name, buf);
109 * Look up variable from environment,
110 * return address of storage for that variable,
111 * or NULL if not found
113 char *env_get(const char *name)
115 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
116 struct env_entry e, *ep;
122 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
124 return ep ? ep->data : NULL;
127 /* restricted capabilities before import */
128 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
129 return (char *)(gd->env_buf);
135 * Like env_get, but prints an error if envvar isn't defined in the
136 * environment. It always returns what env_get does, so it can be used in
137 * place of env_get without changing error handling otherwise.
139 char *from_env(const char *envvar)
143 ret = env_get(envvar);
146 printf("missing environment variable: %s\n", envvar);
151 static int env_get_from_linear(const char *env, const char *name, char *buf,
157 if (name == NULL || *name == '\0')
160 name_len = strlen(name);
162 for (p = env; *p != '\0'; p = end + 1) {
166 for (end = p; *end != '\0'; ++end)
167 if (end - env >= CONFIG_ENV_SIZE)
170 if (strncmp(name, p, name_len) || p[name_len] != '=')
172 value = &p[name_len + 1];
175 memcpy(buf, value, min(len, res + 1));
179 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
190 * Look up variable from environment for restricted C runtime env.
192 int env_get_f(const char *name, char *buf, unsigned len)
196 if (gd->env_valid == ENV_INVALID)
197 env = default_environment;
199 env = (const char *)gd->env_addr;
201 return env_get_from_linear(env, name, buf, len);
205 * Decode the integer value of an environment variable and return it.
207 * @param name Name of environment variable
208 * @param base Number base to use (normally 10, or 16 for hex)
209 * @param default_val Default value to return if the variable is not
211 * Return: the decoded value, or default_val if not found
213 ulong env_get_ulong(const char *name, int base, ulong default_val)
216 * We can use env_get() here, even before relocation, since the
217 * environment variable value is an integer and thus short.
219 const char *str = env_get(name);
221 return str ? simple_strtoul(str, NULL, base) : default_val;
225 * Read an environment variable as a boolean
226 * Return -1 if variable does not exist (default to true)
228 int env_get_yesno(const char *var)
230 char *s = env_get(var);
234 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
238 bool env_get_autostart(void)
240 return env_get_yesno("autostart") == 1;
244 * Look up the variable from the default environment
246 char *env_get_default(const char *name)
248 if (env_get_from_linear(default_environment, name,
249 (char *)(gd->env_buf),
250 sizeof(gd->env_buf)) >= 0)
251 return (char *)(gd->env_buf);
256 void env_set_default(const char *s, int flags)
259 if ((flags & H_INTERACTIVE) == 0) {
260 printf("*** Warning - %s, "
261 "using default environment\n\n", s);
266 debug("Using default environment\n");
270 if (himport_r(&env_htab, default_environment,
271 sizeof(default_environment), '\0', flags, 0,
273 pr_err("## Error: Environment import failed: errno = %d\n",
278 gd->flags |= GD_FLG_ENV_READY;
279 gd->flags |= GD_FLG_ENV_DEFAULT;
283 /* [re]set individual variables to their value in the default environment */
284 int env_set_default_vars(int nvars, char * const vars[], int flags)
287 * Special use-case: import from default environment
288 * (and use \0 as a separator)
290 flags |= H_NOCLEAR | H_DEFAULT;
291 return himport_r(&env_htab, default_environment,
292 sizeof(default_environment), '\0',
293 flags, 0, nvars, vars);
297 * Check if CRC is valid and (if yes) import the environment.
298 * Note that "buf" may or may not be aligned.
300 int env_import(const char *buf, int check, int flags)
302 env_t *ep = (env_t *)buf;
307 memcpy(&crc, &ep->crc, sizeof(crc));
309 if (crc32(0, ep->data, ENV_SIZE) != crc) {
310 env_set_default("bad CRC", 0);
311 return -ENOMSG; /* needed for env_load() */
315 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
317 gd->flags |= GD_FLG_ENV_READY;
321 pr_err("Cannot import environment: errno = %d\n", errno);
323 env_set_default("import failed", 0);
328 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
329 static unsigned char env_flags;
331 int env_check_redund(const char *buf1, int buf1_read_fail,
332 const char *buf2, int buf2_read_fail)
334 int crc1_ok = 0, crc2_ok = 0;
335 env_t *tmp_env1, *tmp_env2;
337 tmp_env1 = (env_t *)buf1;
338 tmp_env2 = (env_t *)buf2;
340 if (buf1_read_fail && buf2_read_fail) {
341 puts("*** Error - No Valid Environment Area found\n");
343 } else if (buf1_read_fail || buf2_read_fail) {
344 puts("*** Warning - some problems detected ");
345 puts("reading environment; recovered successfully\n");
349 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
352 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
355 if (!crc1_ok && !crc2_ok) {
356 return -ENOMSG; /* needed for env_load() */
357 } else if (crc1_ok && !crc2_ok) {
358 gd->env_valid = ENV_VALID;
359 } else if (!crc1_ok && crc2_ok) {
360 gd->env_valid = ENV_REDUND;
362 /* both ok - check serial */
363 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
364 gd->env_valid = ENV_REDUND;
365 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
366 gd->env_valid = ENV_VALID;
367 else if (tmp_env1->flags > tmp_env2->flags)
368 gd->env_valid = ENV_VALID;
369 else if (tmp_env2->flags > tmp_env1->flags)
370 gd->env_valid = ENV_REDUND;
371 else /* flags are equal - almost impossible */
372 gd->env_valid = ENV_VALID;
378 int env_import_redund(const char *buf1, int buf1_read_fail,
379 const char *buf2, int buf2_read_fail,
385 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
388 env_set_default("bad env area", 0);
390 } else if (ret == -ENOMSG) {
391 env_set_default("bad CRC", 0);
395 if (gd->env_valid == ENV_VALID)
400 env_flags = ep->flags;
402 return env_import((char *)ep, 0, flags);
404 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
406 /* Export the environment and generate CRC for it. */
407 int env_export(env_t *env_out)
412 res = (char *)env_out->data;
413 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
415 pr_err("Cannot export environment: errno = %d\n", errno);
419 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
421 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
422 env_out->flags = ++env_flags; /* increase the serial */
428 void env_relocate(void)
430 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
433 env_htab.change_ok += gd->reloc_off;
435 if (gd->env_valid == ENV_INVALID) {
436 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
437 /* Environment not changable */
438 env_set_default(NULL, 0);
440 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
441 env_set_default("bad CRC", 0);
448 #ifdef CONFIG_AUTO_COMPLETE
449 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
452 struct env_entry *match;
457 * When doing $ completion, the first character should
458 * obviously be a '$'.
466 * The second one, if present, should be a '{', as some
467 * configuration of the u-boot shell expand ${var} but not
472 else if (var[0] != '\0')
481 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
482 int vallen = strlen(match->key) + 1;
484 if (found >= maxv - 2 ||
485 bufsz < vallen + (dollar_comp ? 3 : 0))
490 /* Add the '${' prefix to each var when doing $ completion. */
497 memcpy(buf, match->key, vallen);
503 * This one is a bit odd: vallen already contains the
504 * '\0' character but we need to add the '}' suffix,
505 * hence the buf - 1 here. strcpy() will add the '\0'
506 * character just after '}'. buf is then incremented
507 * to account for the extra '}' we just added.
509 strcpy(buf - 1, "}");
514 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
517 cmdv[found++] = dollar_comp ? "${...}" : "...";
524 #ifdef CONFIG_ENV_IMPORT_FDT
525 void env_import_fdt(void)
532 path = env_get("env_fdt_path");
533 if (!path || !path[0])
536 node = ofnode_path(path);
537 if (!ofnode_valid(node)) {
538 printf("Warning: device tree node '%s' not found\n", path);
542 for (res = ofnode_get_first_property(node, &prop);
544 res = ofnode_get_next_property(&prop)) {
545 const char *name, *val;
547 val = ofnode_get_property_by_prop(&prop, &name, NULL);