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