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