Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 2 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 3 | * (C) Copyright 2000-2010 |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Andreas Heppel <aheppel@sysgo.de> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 11 | #include <bootstage.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 12 | #include <command.h> |
Simon Glass | 3f989e7b | 2019-08-01 09:46:44 -0600 | [diff] [blame] | 13 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 14 | #include <env_internal.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 8bef79b | 2019-11-14 12:57:19 -0700 | [diff] [blame] | 16 | #include <sort.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 18 | #include <linux/stddef.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 19 | #include <search.h> |
| 20 | #include <errno.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 21 | #include <malloc.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 22 | #include <u-boot/crc.h> |
Rasmus Villemoes | 95fd977 | 2021-04-21 11:06:54 +0200 | [diff] [blame] | 23 | #include <dm/ofnode.h> |
Marek Behún | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 24 | #include <net.h> |
| 25 | #include <watchdog.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 26 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 29 | /************************************************************************ |
| 30 | * Default settings to be used when no valid environment is found |
| 31 | */ |
Joe Hershberger | ddd8418 | 2012-10-12 08:48:51 +0000 | [diff] [blame] | 32 | #include <env_default.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 33 | |
Gerlando Falauto | c598359 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 34 | struct hsearch_data env_htab = { |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 35 | .change_ok = env_flags_validate, |
Gerlando Falauto | c598359 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 36 | }; |
Mike Frysinger | 2eb1573 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 37 | |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 38 | /* |
Marek Behún | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 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. |
| 41 | * |
| 42 | * int env_set(const char *varname, const char *varvalue); |
| 43 | */ |
| 44 | |
| 45 | /** |
| 46 | * Set an environment variable to an integer value |
| 47 | * |
| 48 | * @param varname Environment variable to set |
| 49 | * @param value Value to set it to |
| 50 | * @return 0 if ok, 1 on error |
| 51 | */ |
| 52 | int env_set_ulong(const char *varname, ulong value) |
| 53 | { |
| 54 | /* TODO: this should be unsigned */ |
| 55 | char *str = simple_itoa(value); |
| 56 | |
| 57 | return env_set(varname, str); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set an environment variable to an value in hex |
| 62 | * |
| 63 | * @param varname Environment variable to set |
| 64 | * @param value Value to set it to |
| 65 | * @return 0 if ok, 1 on error |
| 66 | */ |
| 67 | int env_set_hex(const char *varname, ulong value) |
| 68 | { |
| 69 | char str[17]; |
| 70 | |
| 71 | sprintf(str, "%lx", value); |
| 72 | return env_set(varname, str); |
| 73 | } |
| 74 | |
| 75 | ulong env_get_hex(const char *varname, ulong default_val) |
| 76 | { |
| 77 | const char *s; |
| 78 | ulong value; |
| 79 | char *endp; |
| 80 | |
| 81 | s = env_get(varname); |
| 82 | if (s) |
| 83 | value = hextoul(s, &endp); |
| 84 | if (!s || endp == s) |
| 85 | return default_val; |
| 86 | |
| 87 | return value; |
| 88 | } |
| 89 | |
| 90 | int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) |
| 91 | { |
| 92 | string_to_enetaddr(env_get(name), enetaddr); |
| 93 | return is_valid_ethaddr(enetaddr); |
| 94 | } |
| 95 | |
| 96 | int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) |
| 97 | { |
| 98 | char buf[ARP_HLEN_ASCII + 1]; |
| 99 | |
| 100 | if (eth_env_get_enetaddr(name, (uint8_t *)buf)) |
| 101 | return -EEXIST; |
| 102 | |
| 103 | sprintf(buf, "%pM", enetaddr); |
| 104 | |
| 105 | return env_set(name, buf); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Look up variable from environment, |
| 110 | * return address of storage for that variable, |
| 111 | * or NULL if not found |
| 112 | */ |
| 113 | char *env_get(const char *name) |
| 114 | { |
| 115 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
| 116 | struct env_entry e, *ep; |
| 117 | |
| 118 | WATCHDOG_RESET(); |
| 119 | |
| 120 | e.key = name; |
| 121 | e.data = NULL; |
| 122 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
| 123 | |
| 124 | return ep ? ep->data : NULL; |
| 125 | } |
| 126 | |
| 127 | /* restricted capabilities before import */ |
Marek Behún | e8459c1 | 2021-11-04 00:23:22 +0100 | [diff] [blame^] | 128 | if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0) |
Marek Behún | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 129 | return (char *)(gd->env_buf); |
| 130 | |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* |
| 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. |
| 138 | */ |
| 139 | char *from_env(const char *envvar) |
| 140 | { |
| 141 | char *ret; |
| 142 | |
| 143 | ret = env_get(envvar); |
| 144 | |
| 145 | if (!ret) |
| 146 | printf("missing environment variable: %s\n", envvar); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Look up variable from environment for restricted C runtime env. |
| 153 | */ |
| 154 | int env_get_f(const char *name, char *buf, unsigned len) |
| 155 | { |
| 156 | const char *env, *p, *end; |
| 157 | size_t name_len; |
| 158 | |
| 159 | if (name == NULL || *name == '\0') |
| 160 | return -1; |
| 161 | |
| 162 | name_len = strlen(name); |
| 163 | |
| 164 | if (gd->env_valid == ENV_INVALID) |
Marek Behún | c5cbbe3 | 2021-10-22 15:47:24 +0200 | [diff] [blame] | 165 | env = default_environment; |
Marek Behún | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 166 | else |
| 167 | env = (const char *)gd->env_addr; |
| 168 | |
| 169 | for (p = env; *p != '\0'; p = end + 1) { |
| 170 | const char *value; |
| 171 | unsigned res; |
| 172 | |
| 173 | for (end = p; *end != '\0'; ++end) |
| 174 | if (end - env >= CONFIG_ENV_SIZE) |
| 175 | return -1; |
| 176 | |
| 177 | if (strncmp(name, p, name_len) || p[name_len] != '=') |
| 178 | continue; |
| 179 | value = &p[name_len + 1]; |
| 180 | |
| 181 | res = end - value; |
| 182 | memcpy(buf, value, min(len, res + 1)); |
| 183 | |
| 184 | if (len <= res) { |
| 185 | buf[len - 1] = '\0'; |
| 186 | printf("env_buf [%u bytes] too small for value of \"%s\"\n", |
| 187 | len, name); |
| 188 | } |
| 189 | |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Decode the integer value of an environment variable and return it. |
| 198 | * |
| 199 | * @param name Name of environment variable |
| 200 | * @param base Number base to use (normally 10, or 16 for hex) |
| 201 | * @param default_val Default value to return if the variable is not |
| 202 | * found |
| 203 | * @return the decoded value, or default_val if not found |
| 204 | */ |
| 205 | ulong env_get_ulong(const char *name, int base, ulong default_val) |
| 206 | { |
| 207 | /* |
| 208 | * We can use env_get() here, even before relocation, since the |
| 209 | * environment variable value is an integer and thus short. |
| 210 | */ |
| 211 | const char *str = env_get(name); |
| 212 | |
| 213 | return str ? simple_strtoul(str, NULL, base) : default_val; |
| 214 | } |
| 215 | |
| 216 | /* |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 217 | * Read an environment variable as a boolean |
| 218 | * Return -1 if variable does not exist (default to true) |
| 219 | */ |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 220 | int env_get_yesno(const char *var) |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 221 | { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 222 | char *s = env_get(var); |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 223 | |
| 224 | if (s == NULL) |
| 225 | return -1; |
| 226 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? |
| 227 | 1 : 0; |
| 228 | } |
| 229 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 230 | /* |
| 231 | * Look up the variable from the default environment |
| 232 | */ |
Simon Glass | 723806c | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 233 | char *env_get_default(const char *name) |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 234 | { |
| 235 | char *ret_val; |
| 236 | unsigned long really_valid = gd->env_valid; |
| 237 | unsigned long real_gd_flags = gd->flags; |
| 238 | |
| 239 | /* Pretend that the image is bad. */ |
| 240 | gd->flags &= ~GD_FLG_ENV_READY; |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 241 | gd->env_valid = ENV_INVALID; |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 242 | ret_val = env_get(name); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 243 | gd->env_valid = really_valid; |
| 244 | gd->flags = real_gd_flags; |
| 245 | return ret_val; |
| 246 | } |
| 247 | |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 248 | void env_set_default(const char *s, int flags) |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 249 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 250 | if (s) { |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 251 | if ((flags & H_INTERACTIVE) == 0) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 252 | printf("*** Warning - %s, " |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 253 | "using default environment\n\n", s); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 254 | } else { |
| 255 | puts(s); |
| 256 | } |
| 257 | } else { |
Maxime Ripard | 58ae999 | 2018-01-23 21:16:57 +0100 | [diff] [blame] | 258 | debug("Using default environment\n"); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Marek Vasut | ef9bef2 | 2020-07-07 20:51:34 +0200 | [diff] [blame] | 261 | flags |= H_DEFAULT; |
Marek Behún | c5cbbe3 | 2021-10-22 15:47:24 +0200 | [diff] [blame] | 262 | if (himport_r(&env_htab, default_environment, |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 263 | sizeof(default_environment), '\0', flags, 0, |
Marek Behún | c9db4c5 | 2021-11-04 00:23:21 +0100 | [diff] [blame] | 264 | 0, NULL) == 0) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 265 | pr_err("## Error: Environment import failed: errno = %d\n", |
| 266 | errno); |
Marek Behún | c9db4c5 | 2021-11-04 00:23:21 +0100 | [diff] [blame] | 267 | return; |
| 268 | } |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 269 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 270 | gd->flags |= GD_FLG_ENV_READY; |
Michal Simek | 340b0e3 | 2016-05-30 16:06:54 +0200 | [diff] [blame] | 271 | gd->flags |= GD_FLG_ENV_DEFAULT; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 274 | |
| 275 | /* [re]set individual variables to their value in the default environment */ |
Simon Glass | 0b9d8a0 | 2019-08-01 09:46:56 -0600 | [diff] [blame] | 276 | int env_set_default_vars(int nvars, char * const vars[], int flags) |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 277 | { |
| 278 | /* |
| 279 | * Special use-case: import from default environment |
| 280 | * (and use \0 as a separator) |
| 281 | */ |
Marek Vasut | ef9bef2 | 2020-07-07 20:51:34 +0200 | [diff] [blame] | 282 | flags |= H_NOCLEAR | H_DEFAULT; |
Marek Behún | c5cbbe3 | 2021-10-22 15:47:24 +0200 | [diff] [blame] | 283 | return himport_r(&env_htab, default_environment, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 284 | sizeof(default_environment), '\0', |
Yaniv Levinsky | 477f811 | 2018-06-24 19:16:55 +0300 | [diff] [blame] | 285 | flags, 0, nvars, vars); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 288 | /* |
| 289 | * Check if CRC is valid and (if yes) import the environment. |
| 290 | * Note that "buf" may or may not be aligned. |
| 291 | */ |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 292 | int env_import(const char *buf, int check, int flags) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 293 | { |
| 294 | env_t *ep = (env_t *)buf; |
| 295 | |
| 296 | if (check) { |
| 297 | uint32_t crc; |
| 298 | |
| 299 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 300 | |
| 301 | if (crc32(0, ep->data, ENV_SIZE) != crc) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 302 | env_set_default("bad CRC", 0); |
Sam Protsenko | 49d04c5 | 2019-01-18 21:19:03 +0200 | [diff] [blame] | 303 | return -ENOMSG; /* needed for env_load() */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 307 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 308 | 0, NULL)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 309 | gd->flags |= GD_FLG_ENV_READY; |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 310 | return 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 313 | pr_err("Cannot import environment: errno = %d\n", errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 314 | |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 315 | env_set_default("import failed", 0); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 316 | |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 317 | return -EIO; |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 320 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
| 321 | static unsigned char env_flags; |
| 322 | |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 323 | int env_check_redund(const char *buf1, int buf1_read_fail, |
| 324 | const char *buf2, int buf2_read_fail) |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 325 | { |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 326 | int crc1_ok = 0, crc2_ok = 0; |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 327 | env_t *tmp_env1, *tmp_env2; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 328 | |
| 329 | tmp_env1 = (env_t *)buf1; |
| 330 | tmp_env2 = (env_t *)buf2; |
| 331 | |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 332 | if (buf1_read_fail && buf2_read_fail) { |
| 333 | puts("*** Error - No Valid Environment Area found\n"); |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 334 | return -EIO; |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 335 | } else if (buf1_read_fail || buf2_read_fail) { |
| 336 | puts("*** Warning - some problems detected "); |
| 337 | puts("reading environment; recovered successfully\n"); |
| 338 | } |
| 339 | |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 340 | if (!buf1_read_fail) |
| 341 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == |
| 342 | tmp_env1->crc; |
| 343 | if (!buf2_read_fail) |
| 344 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == |
| 345 | tmp_env2->crc; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 346 | |
| 347 | if (!crc1_ok && !crc2_ok) { |
Sam Protsenko | 49d04c5 | 2019-01-18 21:19:03 +0200 | [diff] [blame] | 348 | return -ENOMSG; /* needed for env_load() */ |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 349 | } else if (crc1_ok && !crc2_ok) { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 350 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 351 | } else if (!crc1_ok && crc2_ok) { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 352 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 353 | } else { |
| 354 | /* both ok - check serial */ |
| 355 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 356 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 357 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 358 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 359 | else if (tmp_env1->flags > tmp_env2->flags) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 360 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 361 | else if (tmp_env2->flags > tmp_env1->flags) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 362 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 363 | else /* flags are equal - almost impossible */ |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 364 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 365 | } |
| 366 | |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int env_import_redund(const char *buf1, int buf1_read_fail, |
| 371 | const char *buf2, int buf2_read_fail, |
| 372 | int flags) |
| 373 | { |
| 374 | env_t *ep; |
| 375 | int ret; |
| 376 | |
| 377 | ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail); |
| 378 | |
| 379 | if (ret == -EIO) { |
| 380 | env_set_default("bad env area", 0); |
| 381 | return -EIO; |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 382 | } else if (ret == -ENOMSG) { |
| 383 | env_set_default("bad CRC", 0); |
| 384 | return -ENOMSG; |
| 385 | } |
| 386 | |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 387 | if (gd->env_valid == ENV_VALID) |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 388 | ep = (env_t *)buf1; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 389 | else |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 390 | ep = (env_t *)buf2; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 391 | |
| 392 | env_flags = ep->flags; |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 393 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 394 | return env_import((char *)ep, 0, flags); |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 395 | } |
| 396 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
| 397 | |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 398 | /* Export the environment and generate CRC for it. */ |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 399 | int env_export(env_t *env_out) |
| 400 | { |
| 401 | char *res; |
| 402 | ssize_t len; |
| 403 | |
| 404 | res = (char *)env_out->data; |
| 405 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
| 406 | if (len < 0) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 407 | pr_err("Cannot export environment: errno = %d\n", errno); |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 408 | return 1; |
| 409 | } |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 410 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 411 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
| 412 | |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 413 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
| 414 | env_out->flags = ++env_flags; /* increase the serial */ |
| 415 | #endif |
| 416 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 420 | void env_relocate(void) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 421 | { |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 422 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 60f7da1 | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 423 | env_reloc(); |
Siva Durga Prasad Paladugu | 7bcdf19 | 2018-04-13 07:57:21 +0200 | [diff] [blame] | 424 | env_fix_drivers(); |
Tom Rini | d90fc9c | 2019-10-04 12:21:33 -0400 | [diff] [blame] | 425 | env_htab.change_ok += gd->reloc_off; |
Heiko Schocher | 60f7da1 | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 426 | #endif |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 427 | if (gd->env_valid == ENV_INVALID) { |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 428 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
| 429 | /* Environment not changable */ |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 430 | env_set_default(NULL, 0); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 431 | #else |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 432 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 433 | env_set_default("bad CRC", 0); |
Lei Wen | d259079 | 2010-10-10 12:36:40 +0800 | [diff] [blame] | 434 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 435 | } else { |
Simon Glass | 310fb14 | 2017-08-03 12:22:07 -0600 | [diff] [blame] | 436 | env_load(); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 437 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 438 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 439 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 440 | #ifdef CONFIG_AUTO_COMPLETE |
| 441 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf, |
| 442 | bool dollar_comp) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 443 | { |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 444 | struct env_entry *match; |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 445 | int found, idx; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 446 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 447 | if (dollar_comp) { |
| 448 | /* |
| 449 | * When doing $ completion, the first character should |
| 450 | * obviously be a '$'. |
| 451 | */ |
| 452 | if (var[0] != '$') |
| 453 | return 0; |
| 454 | |
| 455 | var++; |
| 456 | |
| 457 | /* |
| 458 | * The second one, if present, should be a '{', as some |
| 459 | * configuration of the u-boot shell expand ${var} but not |
| 460 | * $var. |
| 461 | */ |
| 462 | if (var[0] == '{') |
| 463 | var++; |
| 464 | else if (var[0] != '\0') |
| 465 | return 0; |
| 466 | } |
| 467 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 468 | idx = 0; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 469 | found = 0; |
| 470 | cmdv[0] = NULL; |
| 471 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 472 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 473 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
| 474 | int vallen = strlen(match->key) + 1; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 475 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 476 | if (found >= maxv - 2 || |
| 477 | bufsz < vallen + (dollar_comp ? 3 : 0)) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 478 | break; |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 479 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 480 | cmdv[found++] = buf; |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 481 | |
| 482 | /* Add the '${' prefix to each var when doing $ completion. */ |
| 483 | if (dollar_comp) { |
| 484 | strcpy(buf, "${"); |
| 485 | buf += 2; |
| 486 | bufsz -= 3; |
| 487 | } |
| 488 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 489 | memcpy(buf, match->key, vallen); |
| 490 | buf += vallen; |
| 491 | bufsz -= vallen; |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 492 | |
| 493 | if (dollar_comp) { |
| 494 | /* |
| 495 | * This one is a bit odd: vallen already contains the |
| 496 | * '\0' character but we need to add the '}' suffix, |
| 497 | * hence the buf - 1 here. strcpy() will add the '\0' |
| 498 | * character just after '}'. buf is then incremented |
| 499 | * to account for the extra '}' we just added. |
| 500 | */ |
| 501 | strcpy(buf - 1, "}"); |
| 502 | buf++; |
| 503 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 506 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
| 507 | |
| 508 | if (idx) |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 509 | cmdv[found++] = dollar_comp ? "${...}" : "..."; |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 510 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 511 | cmdv[found] = NULL; |
| 512 | return found; |
| 513 | } |
| 514 | #endif |
Rasmus Villemoes | 95fd977 | 2021-04-21 11:06:54 +0200 | [diff] [blame] | 515 | |
| 516 | #ifdef CONFIG_ENV_IMPORT_FDT |
| 517 | void env_import_fdt(void) |
| 518 | { |
| 519 | const char *path; |
| 520 | struct ofprop prop; |
| 521 | ofnode node; |
| 522 | int res; |
| 523 | |
| 524 | path = env_get("env_fdt_path"); |
| 525 | if (!path || !path[0]) |
| 526 | return; |
| 527 | |
| 528 | node = ofnode_path(path); |
| 529 | if (!ofnode_valid(node)) { |
| 530 | printf("Warning: device tree node '%s' not found\n", path); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | for (res = ofnode_get_first_property(node, &prop); |
| 535 | !res; |
| 536 | res = ofnode_get_next_property(&prop)) { |
| 537 | const char *name, *val; |
| 538 | |
| 539 | val = ofnode_get_property_by_prop(&prop, &name, NULL); |
| 540 | env_set(name, val); |
| 541 | } |
| 542 | } |
| 543 | #endif |