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 | |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 10 | #include <bootstage.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 3f989e7b | 2019-08-01 09:46:44 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 8bef79b | 2019-11-14 12:57:19 -0700 | [diff] [blame] | 15 | #include <sort.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 17 | #include <linux/printk.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 | /* |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 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. |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 43 | */ |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 44 | static int env_id = 1; |
| 45 | |
| 46 | int env_get_id(void) |
| 47 | { |
| 48 | return env_id; |
| 49 | } |
| 50 | |
| 51 | void env_inc_id(void) |
| 52 | { |
| 53 | env_id++; |
| 54 | } |
| 55 | |
| 56 | int env_do_env_set(int flag, int argc, char *const argv[], int env_flag) |
| 57 | { |
| 58 | int i, len; |
| 59 | char *name, *value, *s; |
| 60 | struct env_entry e, *ep; |
| 61 | |
| 62 | debug("Initial value for argc=%d\n", argc); |
| 63 | |
| 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); |
| 67 | #endif |
| 68 | |
| 69 | while (argc > 1 && **(argv + 1) == '-') { |
| 70 | char *arg = *++argv; |
| 71 | |
| 72 | --argc; |
| 73 | while (*++arg) { |
| 74 | switch (*arg) { |
| 75 | case 'f': /* force */ |
| 76 | env_flag |= H_FORCE; |
| 77 | break; |
| 78 | default: |
| 79 | return CMD_RET_USAGE; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | debug("Final value for argc=%d\n", argc); |
| 84 | name = argv[1]; |
| 85 | |
| 86 | if (strchr(name, '=')) { |
Ivan Orlov | 3c3270b | 2024-01-08 17:20:45 +0000 | [diff] [blame] | 87 | printf("## Error: illegal character '=' " |
Tom Rini | d972192 | 2023-10-26 14:31:16 -0400 | [diff] [blame] | 88 | "in variable name \"%s\"\n", name); |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | env_inc_id(); |
| 93 | |
| 94 | /* Delete only ? */ |
| 95 | if (argc < 3 || argv[2] == NULL) { |
| 96 | int rc = hdelete_r(name, &env_htab, env_flag); |
| 97 | |
| 98 | /* If the variable didn't exist, don't report an error */ |
| 99 | return rc && rc != -ENOENT ? 1 : 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Insert / replace new value |
| 104 | */ |
| 105 | for (i = 2, len = 0; i < argc; ++i) |
| 106 | len += strlen(argv[i]) + 1; |
| 107 | |
| 108 | value = malloc(len); |
| 109 | if (value == NULL) { |
| 110 | printf("## Can't malloc %d bytes\n", len); |
| 111 | return 1; |
| 112 | } |
| 113 | for (i = 2, s = value; i < argc; ++i) { |
| 114 | char *v = argv[i]; |
| 115 | |
| 116 | while ((*s++ = *v++) != '\0') |
| 117 | ; |
| 118 | *(s - 1) = ' '; |
| 119 | } |
| 120 | if (s != value) |
| 121 | *--s = '\0'; |
| 122 | |
| 123 | e.key = name; |
| 124 | e.data = value; |
| 125 | hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag); |
| 126 | free(value); |
| 127 | if (!ep) { |
| 128 | printf("## Error inserting \"%s\" variable, errno=%d\n", |
| 129 | name, errno); |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int env_set(const char *varname, const char *varvalue) |
| 137 | { |
| 138 | const char * const argv[4] = { "setenv", varname, varvalue, NULL }; |
| 139 | |
| 140 | /* before import into hashtable */ |
| 141 | if (!(gd->flags & GD_FLG_ENV_READY)) |
| 142 | return 1; |
| 143 | |
| 144 | if (varvalue == NULL || varvalue[0] == '\0') |
| 145 | return env_do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); |
| 146 | else |
| 147 | return env_do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); |
| 148 | } |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Set an environment variable to an integer value |
| 152 | * |
| 153 | * @param varname Environment variable to set |
| 154 | * @param value Value to set it to |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 155 | * Return: 0 if ok, 1 on error |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 156 | */ |
| 157 | int env_set_ulong(const char *varname, ulong value) |
| 158 | { |
| 159 | /* TODO: this should be unsigned */ |
| 160 | char *str = simple_itoa(value); |
| 161 | |
| 162 | return env_set(varname, str); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Set an environment variable to an value in hex |
| 167 | * |
| 168 | * @param varname Environment variable to set |
| 169 | * @param value Value to set it to |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 170 | * Return: 0 if ok, 1 on error |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 171 | */ |
| 172 | int env_set_hex(const char *varname, ulong value) |
| 173 | { |
| 174 | char str[17]; |
| 175 | |
| 176 | sprintf(str, "%lx", value); |
| 177 | return env_set(varname, str); |
| 178 | } |
| 179 | |
| 180 | ulong env_get_hex(const char *varname, ulong default_val) |
| 181 | { |
| 182 | const char *s; |
| 183 | ulong value; |
| 184 | char *endp; |
| 185 | |
| 186 | s = env_get(varname); |
| 187 | if (s) |
| 188 | value = hextoul(s, &endp); |
| 189 | if (!s || endp == s) |
| 190 | return default_val; |
| 191 | |
| 192 | return value; |
| 193 | } |
| 194 | |
| 195 | int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) |
| 196 | { |
| 197 | string_to_enetaddr(env_get(name), enetaddr); |
| 198 | return is_valid_ethaddr(enetaddr); |
| 199 | } |
| 200 | |
| 201 | int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) |
| 202 | { |
| 203 | char buf[ARP_HLEN_ASCII + 1]; |
| 204 | |
| 205 | if (eth_env_get_enetaddr(name, (uint8_t *)buf)) |
| 206 | return -EEXIST; |
| 207 | |
| 208 | sprintf(buf, "%pM", enetaddr); |
| 209 | |
| 210 | return env_set(name, buf); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Look up variable from environment, |
| 215 | * return address of storage for that variable, |
| 216 | * or NULL if not found |
| 217 | */ |
| 218 | char *env_get(const char *name) |
| 219 | { |
| 220 | if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ |
| 221 | struct env_entry e, *ep; |
| 222 | |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 223 | schedule(); |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 224 | |
| 225 | e.key = name; |
| 226 | e.data = NULL; |
| 227 | hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); |
| 228 | |
| 229 | return ep ? ep->data : NULL; |
| 230 | } |
| 231 | |
| 232 | /* restricted capabilities before import */ |
Marek BehĂșn | e8459c1 | 2021-11-04 00:23:22 +0100 | [diff] [blame] | 233 | 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] | 234 | return (char *)(gd->env_buf); |
| 235 | |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | /* |
| 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. |
| 243 | */ |
| 244 | char *from_env(const char *envvar) |
| 245 | { |
| 246 | char *ret; |
| 247 | |
| 248 | ret = env_get(envvar); |
| 249 | |
| 250 | if (!ret) |
| 251 | printf("missing environment variable: %s\n", envvar); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
Marek BehĂșn | 4e7c8b2 | 2021-11-04 00:23:23 +0100 | [diff] [blame] | 256 | static int env_get_from_linear(const char *env, const char *name, char *buf, |
| 257 | unsigned len) |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 258 | { |
Marek BehĂșn | 4e7c8b2 | 2021-11-04 00:23:23 +0100 | [diff] [blame] | 259 | const char *p, *end; |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 260 | size_t name_len; |
| 261 | |
| 262 | if (name == NULL || *name == '\0') |
| 263 | return -1; |
| 264 | |
| 265 | name_len = strlen(name); |
| 266 | |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 267 | for (p = env; *p != '\0'; p = end + 1) { |
| 268 | const char *value; |
| 269 | unsigned res; |
| 270 | |
| 271 | for (end = p; *end != '\0'; ++end) |
| 272 | if (end - env >= CONFIG_ENV_SIZE) |
| 273 | return -1; |
| 274 | |
| 275 | if (strncmp(name, p, name_len) || p[name_len] != '=') |
| 276 | continue; |
| 277 | value = &p[name_len + 1]; |
| 278 | |
| 279 | res = end - value; |
| 280 | memcpy(buf, value, min(len, res + 1)); |
| 281 | |
| 282 | if (len <= res) { |
| 283 | buf[len - 1] = '\0'; |
| 284 | printf("env_buf [%u bytes] too small for value of \"%s\"\n", |
| 285 | len, name); |
| 286 | } |
| 287 | |
| 288 | return res; |
| 289 | } |
| 290 | |
| 291 | return -1; |
| 292 | } |
| 293 | |
Marek BehĂșn | 4e7c8b2 | 2021-11-04 00:23:23 +0100 | [diff] [blame] | 294 | /* |
| 295 | * Look up variable from environment for restricted C runtime env. |
| 296 | */ |
| 297 | int env_get_f(const char *name, char *buf, unsigned len) |
| 298 | { |
| 299 | const char *env; |
| 300 | |
| 301 | if (gd->env_valid == ENV_INVALID) |
| 302 | env = default_environment; |
| 303 | else |
| 304 | env = (const char *)gd->env_addr; |
| 305 | |
| 306 | return env_get_from_linear(env, name, buf, len); |
| 307 | } |
| 308 | |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 309 | /** |
| 310 | * Decode the integer value of an environment variable and return it. |
| 311 | * |
| 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 |
| 315 | * found |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 316 | * Return: the decoded value, or default_val if not found |
Marek BehĂșn | f231566 | 2021-10-17 17:36:38 +0200 | [diff] [blame] | 317 | */ |
| 318 | ulong env_get_ulong(const char *name, int base, ulong default_val) |
| 319 | { |
| 320 | /* |
| 321 | * We can use env_get() here, even before relocation, since the |
| 322 | * environment variable value is an integer and thus short. |
| 323 | */ |
| 324 | const char *str = env_get(name); |
| 325 | |
| 326 | return str ? simple_strtoul(str, NULL, base) : default_val; |
| 327 | } |
| 328 | |
| 329 | /* |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 330 | * Read an environment variable as a boolean |
| 331 | * Return -1 if variable does not exist (default to true) |
| 332 | */ |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 333 | int env_get_yesno(const char *var) |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 334 | { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 335 | char *s = env_get(var); |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 336 | |
| 337 | if (s == NULL) |
| 338 | return -1; |
| 339 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? |
| 340 | 1 : 0; |
| 341 | } |
| 342 | |
Simon Glass | 7839865 | 2021-10-21 21:08:52 -0600 | [diff] [blame] | 343 | bool env_get_autostart(void) |
| 344 | { |
| 345 | return env_get_yesno("autostart") == 1; |
| 346 | } |
| 347 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 348 | /* |
| 349 | * Look up the variable from the default environment |
| 350 | */ |
Simon Glass | 723806c | 2017-08-03 12:22:15 -0600 | [diff] [blame] | 351 | char *env_get_default(const char *name) |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 352 | { |
Quentin Schulz | 87d3c47 | 2024-01-17 18:59:12 +0100 | [diff] [blame] | 353 | int ret; |
| 354 | |
| 355 | ret = env_get_default_into(name, (char *)(gd->env_buf), |
| 356 | sizeof(gd->env_buf)); |
| 357 | if (ret >= 0) |
Marek BehĂșn | 4e7c8b2 | 2021-11-04 00:23:23 +0100 | [diff] [blame] | 358 | return (char *)(gd->env_buf); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 359 | |
Marek BehĂșn | 4e7c8b2 | 2021-11-04 00:23:23 +0100 | [diff] [blame] | 360 | return NULL; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 361 | } |
| 362 | |
Quentin Schulz | 23c1ab9 | 2024-01-17 18:59:11 +0100 | [diff] [blame] | 363 | /* |
| 364 | * Look up the variable from the default environment and store its value in buf |
| 365 | */ |
| 366 | int env_get_default_into(const char *name, char *buf, unsigned int len) |
| 367 | { |
| 368 | return env_get_from_linear(default_environment, name, buf, len); |
| 369 | } |
| 370 | |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 371 | void env_set_default(const char *s, int flags) |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 372 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 373 | if (s) { |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 374 | if ((flags & H_INTERACTIVE) == 0) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 375 | printf("*** Warning - %s, " |
Yaniv Levinsky | c5d548a | 2018-06-24 19:16:57 +0300 | [diff] [blame] | 376 | "using default environment\n\n", s); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 377 | } else { |
| 378 | puts(s); |
| 379 | } |
| 380 | } else { |
Maxime Ripard | 58ae999 | 2018-01-23 21:16:57 +0100 | [diff] [blame] | 381 | debug("Using default environment\n"); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Marek Vasut | ef9bef2 | 2020-07-07 20:51:34 +0200 | [diff] [blame] | 384 | flags |= H_DEFAULT; |
Marek BehĂșn | c5cbbe3 | 2021-10-22 15:47:24 +0200 | [diff] [blame] | 385 | if (himport_r(&env_htab, default_environment, |
Alexander Holler | ecd1446 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 386 | sizeof(default_environment), '\0', flags, 0, |
Marek BehĂșn | c9db4c5 | 2021-11-04 00:23:21 +0100 | [diff] [blame] | 387 | 0, NULL) == 0) { |
Quentin Schulz | 6c90f62 | 2018-07-09 19:16:25 +0200 | [diff] [blame] | 388 | pr_err("## Error: Environment import failed: errno = %d\n", |
| 389 | errno); |
Marek BehĂșn | c9db4c5 | 2021-11-04 00:23:21 +0100 | [diff] [blame] | 390 | return; |
| 391 | } |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 392 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 393 | gd->flags |= GD_FLG_ENV_READY; |
Michal Simek | 340b0e3 | 2016-05-30 16:06:54 +0200 | [diff] [blame] | 394 | gd->flags |= GD_FLG_ENV_DEFAULT; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 397 | |
| 398 | /* [re]set individual variables to their value in the default environment */ |
Simon Glass | 0b9d8a0 | 2019-08-01 09:46:56 -0600 | [diff] [blame] | 399 | int env_set_default_vars(int nvars, char * const vars[], int flags) |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 400 | { |
| 401 | /* |
| 402 | * Special use-case: import from default environment |
| 403 | * (and use \0 as a separator) |
| 404 | */ |
Marek Vasut | ef9bef2 | 2020-07-07 20:51:34 +0200 | [diff] [blame] | 405 | flags |= H_NOCLEAR | H_DEFAULT; |
Marek BehĂșn | c5cbbe3 | 2021-10-22 15:47:24 +0200 | [diff] [blame] | 406 | return himport_r(&env_htab, default_environment, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 407 | sizeof(default_environment), '\0', |
Yaniv Levinsky | 477f811 | 2018-06-24 19:16:55 +0300 | [diff] [blame] | 408 | flags, 0, nvars, vars); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 411 | /* |
| 412 | * Check if CRC is valid and (if yes) import the environment. |
| 413 | * Note that "buf" may or may not be aligned. |
| 414 | */ |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 415 | int env_import(const char *buf, int check, int flags) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 416 | { |
| 417 | env_t *ep = (env_t *)buf; |
| 418 | |
| 419 | if (check) { |
| 420 | uint32_t crc; |
| 421 | |
| 422 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 423 | |
| 424 | if (crc32(0, ep->data, ENV_SIZE) != crc) { |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 425 | env_set_default("bad CRC", 0); |
Sam Protsenko | 49d04c5 | 2019-01-18 21:19:03 +0200 | [diff] [blame] | 426 | return -ENOMSG; /* needed for env_load() */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 430 | 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] | 431 | 0, NULL)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 432 | gd->flags |= GD_FLG_ENV_READY; |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 433 | return 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 436 | pr_err("Cannot import environment: errno = %d\n", errno); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 437 | |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 438 | env_set_default("import failed", 0); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 439 | |
Simon Goldschmidt | 42a1820 | 2018-01-31 14:47:10 +0100 | [diff] [blame] | 440 | return -EIO; |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 441 | } |
| 442 | |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 443 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
| 444 | static unsigned char env_flags; |
| 445 | |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 446 | int env_check_redund(const char *buf1, int buf1_read_fail, |
| 447 | const char *buf2, int buf2_read_fail) |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 448 | { |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 449 | int crc1_ok = 0, crc2_ok = 0; |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 450 | env_t *tmp_env1, *tmp_env2; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 451 | |
| 452 | tmp_env1 = (env_t *)buf1; |
| 453 | tmp_env2 = (env_t *)buf2; |
| 454 | |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 455 | if (buf1_read_fail && buf2_read_fail) { |
| 456 | puts("*** Error - No Valid Environment Area found\n"); |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 457 | return -EIO; |
Simon Goldschmidt | 31f044b | 2018-01-31 14:47:11 +0100 | [diff] [blame] | 458 | } else if (buf1_read_fail || buf2_read_fail) { |
| 459 | puts("*** Warning - some problems detected "); |
| 460 | puts("reading environment; recovered successfully\n"); |
| 461 | } |
| 462 | |
Brandon Maier | b9c3052 | 2020-12-17 17:19:18 -0600 | [diff] [blame] | 463 | if (!buf1_read_fail) |
| 464 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == |
| 465 | tmp_env1->crc; |
| 466 | if (!buf2_read_fail) |
| 467 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == |
| 468 | tmp_env2->crc; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 469 | |
| 470 | if (!crc1_ok && !crc2_ok) { |
Ashok Reddy Soma | 4dc5e26 | 2023-07-04 00:16:07 -0600 | [diff] [blame] | 471 | gd->env_valid = ENV_INVALID; |
Sam Protsenko | 49d04c5 | 2019-01-18 21:19:03 +0200 | [diff] [blame] | 472 | return -ENOMSG; /* needed for env_load() */ |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 473 | } else if (crc1_ok && !crc2_ok) { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 474 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 475 | } else if (!crc1_ok && crc2_ok) { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 476 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 477 | } else { |
| 478 | /* both ok - check serial */ |
| 479 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 480 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 481 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 482 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 483 | else if (tmp_env1->flags > tmp_env2->flags) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 484 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 485 | else if (tmp_env2->flags > tmp_env1->flags) |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 486 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 487 | else /* flags are equal - almost impossible */ |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 488 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 489 | } |
| 490 | |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | int env_import_redund(const char *buf1, int buf1_read_fail, |
| 495 | const char *buf2, int buf2_read_fail, |
| 496 | int flags) |
| 497 | { |
| 498 | env_t *ep; |
| 499 | int ret; |
| 500 | |
| 501 | ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail); |
| 502 | |
| 503 | if (ret == -EIO) { |
| 504 | env_set_default("bad env area", 0); |
| 505 | return -EIO; |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 506 | } else if (ret == -ENOMSG) { |
| 507 | env_set_default("bad CRC", 0); |
| 508 | return -ENOMSG; |
| 509 | } |
| 510 | |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 511 | if (gd->env_valid == ENV_VALID) |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 512 | ep = (env_t *)buf1; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 513 | else |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 514 | ep = (env_t *)buf2; |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 515 | |
| 516 | env_flags = ep->flags; |
Heiko Schocher | 1229533 | 2020-10-10 10:28:04 +0200 | [diff] [blame] | 517 | |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 518 | return env_import((char *)ep, 0, flags); |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 519 | } |
| 520 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ |
| 521 | |
Robert P. J. Day | fc0b594 | 2016-09-07 14:27:59 -0400 | [diff] [blame] | 522 | /* Export the environment and generate CRC for it. */ |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 523 | int env_export(env_t *env_out) |
| 524 | { |
| 525 | char *res; |
| 526 | ssize_t len; |
| 527 | |
| 528 | res = (char *)env_out->data; |
| 529 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
| 530 | if (len < 0) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 531 | pr_err("Cannot export environment: errno = %d\n", errno); |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 532 | return 1; |
| 533 | } |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 534 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 535 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
| 536 | |
Fiach Antaw | 76768f5 | 2017-01-25 18:53:11 +1000 | [diff] [blame] | 537 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
| 538 | env_out->flags = ++env_flags; /* increase the serial */ |
| 539 | #endif |
| 540 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 544 | void env_relocate(void) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 545 | { |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 546 | if (gd->env_valid == ENV_INVALID) { |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 547 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
| 548 | /* Environment not changable */ |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 549 | env_set_default(NULL, 0); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 550 | #else |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 551 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
Simon Glass | 0ac7d72 | 2019-08-01 09:47:00 -0600 | [diff] [blame] | 552 | env_set_default("bad CRC", 0); |
Lei Wen | d259079 | 2010-10-10 12:36:40 +0800 | [diff] [blame] | 553 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 554 | } else { |
Simon Glass | 310fb14 | 2017-08-03 12:22:07 -0600 | [diff] [blame] | 555 | env_load(); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 556 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 557 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 558 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 559 | #ifdef CONFIG_AUTO_COMPLETE |
| 560 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf, |
| 561 | bool dollar_comp) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 562 | { |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 563 | struct env_entry *match; |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 564 | int found, idx; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 565 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 566 | if (dollar_comp) { |
| 567 | /* |
| 568 | * When doing $ completion, the first character should |
| 569 | * obviously be a '$'. |
| 570 | */ |
| 571 | if (var[0] != '$') |
| 572 | return 0; |
| 573 | |
| 574 | var++; |
| 575 | |
| 576 | /* |
| 577 | * The second one, if present, should be a '{', as some |
| 578 | * configuration of the u-boot shell expand ${var} but not |
| 579 | * $var. |
| 580 | */ |
| 581 | if (var[0] == '{') |
| 582 | var++; |
| 583 | else if (var[0] != '\0') |
| 584 | return 0; |
| 585 | } |
| 586 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 587 | idx = 0; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 588 | found = 0; |
| 589 | cmdv[0] = NULL; |
| 590 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 591 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 592 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
| 593 | int vallen = strlen(match->key) + 1; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 594 | |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 595 | if (found >= maxv - 2 || |
| 596 | bufsz < vallen + (dollar_comp ? 3 : 0)) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 597 | break; |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 598 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 599 | cmdv[found++] = buf; |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 600 | |
| 601 | /* Add the '${' prefix to each var when doing $ completion. */ |
| 602 | if (dollar_comp) { |
| 603 | strcpy(buf, "${"); |
| 604 | buf += 2; |
| 605 | bufsz -= 3; |
| 606 | } |
| 607 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 608 | memcpy(buf, match->key, vallen); |
| 609 | buf += vallen; |
| 610 | bufsz -= vallen; |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 611 | |
| 612 | if (dollar_comp) { |
| 613 | /* |
| 614 | * This one is a bit odd: vallen already contains the |
| 615 | * '\0' character but we need to add the '}' suffix, |
| 616 | * hence the buf - 1 here. strcpy() will add the '\0' |
| 617 | * character just after '}'. buf is then incremented |
| 618 | * to account for the extra '}' we just added. |
| 619 | */ |
| 620 | strcpy(buf - 1, "}"); |
| 621 | buf++; |
| 622 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 625 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
| 626 | |
| 627 | if (idx) |
Boris Brezillon | 03dcf17 | 2018-12-05 09:26:50 +0100 | [diff] [blame] | 628 | cmdv[found++] = dollar_comp ? "${...}" : "..."; |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 629 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 630 | cmdv[found] = NULL; |
| 631 | return found; |
| 632 | } |
| 633 | #endif |
Rasmus Villemoes | 95fd977 | 2021-04-21 11:06:54 +0200 | [diff] [blame] | 634 | |
| 635 | #ifdef CONFIG_ENV_IMPORT_FDT |
| 636 | void env_import_fdt(void) |
| 637 | { |
| 638 | const char *path; |
| 639 | struct ofprop prop; |
| 640 | ofnode node; |
| 641 | int res; |
| 642 | |
| 643 | path = env_get("env_fdt_path"); |
| 644 | if (!path || !path[0]) |
| 645 | return; |
| 646 | |
| 647 | node = ofnode_path(path); |
| 648 | if (!ofnode_valid(node)) { |
| 649 | printf("Warning: device tree node '%s' not found\n", path); |
| 650 | return; |
| 651 | } |
| 652 | |
Simon Glass | 4b1f571 | 2022-09-06 20:27:13 -0600 | [diff] [blame] | 653 | for (res = ofnode_first_property(node, &prop); |
Rasmus Villemoes | 95fd977 | 2021-04-21 11:06:54 +0200 | [diff] [blame] | 654 | !res; |
Simon Glass | 4b1f571 | 2022-09-06 20:27:13 -0600 | [diff] [blame] | 655 | res = ofnode_next_property(&prop)) { |
Rasmus Villemoes | 95fd977 | 2021-04-21 11:06:54 +0200 | [diff] [blame] | 656 | const char *name, *val; |
| 657 | |
Simon Glass | 9243224 | 2022-09-06 20:27:14 -0600 | [diff] [blame] | 658 | val = ofprop_get_property(&prop, &name, NULL); |
Rasmus Villemoes | 95fd977 | 2021-04-21 11:06:54 +0200 | [diff] [blame] | 659 | env_set(name, val); |
| 660 | } |
| 661 | } |
| 662 | #endif |