blob: 8d47d726059e11acb103d6e3a083d35c73329829 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk05706fd2002-09-28 17:48:27 +00002/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
wdenk05706fd2002-09-28 17:48:27 +00004 * 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>
wdenk05706fd2002-09-28 17:48:27 +00008 */
9
Simon Glass1ea97892020-05-10 11:40:00 -060010#include <bootstage.h>
wdenk05706fd2002-09-28 17:48:27 +000011#include <command.h>
Simon Glass8fe40932019-08-01 09:46:44 -060012#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060013#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass16ff5702019-11-14 12:57:19 -070015#include <sort.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
wdenk05706fd2002-09-28 17:48:27 +000018#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020019#include <search.h>
20#include <errno.h>
wdenk05706fd2002-09-28 17:48:27 +000021#include <malloc.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070022#include <u-boot/crc.h>
Rasmus Villemoescf8e5432021-04-21 11:06:54 +020023#include <dm/ofnode.h>
Marek BehĂșn30044c52021-10-17 17:36:38 +020024#include <net.h>
25#include <watchdog.h>
wdenk05706fd2002-09-28 17:48:27 +000026
Wolfgang Denk6405a152006-03-31 18:32:53 +020027DECLARE_GLOBAL_DATA_PTR;
28
wdenk05706fd2002-09-28 17:48:27 +000029/************************************************************************
30 * Default settings to be used when no valid environment is found
31 */
Joe Hershberger81321142012-10-12 08:48:51 +000032#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000033
Gerlando Falautod3925952012-08-24 00:11:39 +000034struct hsearch_data env_htab = {
Joe Hershberger71497d02012-12-11 22:16:31 -060035 .change_ok = env_flags_validate,
Gerlando Falautod3925952012-08-24 00:11:39 +000036};
Mike Frysinger92d7a992010-12-08 06:26:04 -050037
Joe Hershberger864ec562012-12-11 22:16:22 -060038/*
Tom Rinib8ca93f2023-10-26 14:31:16 -040039 * 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Ășn30044c52021-10-17 17:36:38 +020043 */
Tom Rinib8ca93f2023-10-26 14:31:16 -040044static int env_id = 1;
45
46int env_get_id(void)
47{
48 return env_id;
49}
50
51void env_inc_id(void)
52{
53 env_id++;
54}
55
56int 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 Orlov9e40e072024-01-08 17:20:45 +000087 printf("## Error: illegal character '=' "
Tom Rinib8ca93f2023-10-26 14:31:16 -040088 "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
136int 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Ășn30044c52021-10-17 17:36:38 +0200149
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 Schuchardt47b4c022022-01-19 18:05:50 +0100155 * Return: 0 if ok, 1 on error
Marek BehĂșn30044c52021-10-17 17:36:38 +0200156 */
157int 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 Schuchardt47b4c022022-01-19 18:05:50 +0100170 * Return: 0 if ok, 1 on error
Marek BehĂșn30044c52021-10-17 17:36:38 +0200171 */
172int 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
180ulong 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
195int 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
201int 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 */
218char *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 Roese80877fa2022-09-02 14:10:46 +0200223 schedule();
Marek BehĂșn30044c52021-10-17 17:36:38 +0200224
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Ășne087a462021-11-04 00:23:22 +0100233 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
Marek BehĂșn30044c52021-10-17 17:36:38 +0200234 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 */
244char *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Ășnc20046f2021-11-04 00:23:23 +0100256static int env_get_from_linear(const char *env, const char *name, char *buf,
257 unsigned len)
Marek BehĂșn30044c52021-10-17 17:36:38 +0200258{
Marek BehĂșnc20046f2021-11-04 00:23:23 +0100259 const char *p, *end;
Marek BehĂșn30044c52021-10-17 17:36:38 +0200260 size_t name_len;
261
262 if (name == NULL || *name == '\0')
263 return -1;
264
265 name_len = strlen(name);
266
Marek BehĂșn30044c52021-10-17 17:36:38 +0200267 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Ășnc20046f2021-11-04 00:23:23 +0100294/*
295 * Look up variable from environment for restricted C runtime env.
296 */
297int 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Ășn30044c52021-10-17 17:36:38 +0200309/**
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 Schuchardt47b4c022022-01-19 18:05:50 +0100316 * Return: the decoded value, or default_val if not found
Marek BehĂșn30044c52021-10-17 17:36:38 +0200317 */
318ulong 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 Hershberger864ec562012-12-11 22:16:22 -0600330 * Read an environment variable as a boolean
331 * Return -1 if variable does not exist (default to true)
332 */
Simon Glass22c34c22017-08-03 12:22:13 -0600333int env_get_yesno(const char *var)
Joe Hershberger864ec562012-12-11 22:16:22 -0600334{
Simon Glass64b723f2017-08-03 12:22:12 -0600335 char *s = env_get(var);
Joe Hershberger864ec562012-12-11 22:16:22 -0600336
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 Glass46b530a2021-10-21 21:08:52 -0600343bool env_get_autostart(void)
344{
345 return env_get_yesno("autostart") == 1;
346}
347
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600348/*
349 * Look up the variable from the default environment
350 */
Simon Glassda1a1342017-08-03 12:22:15 -0600351char *env_get_default(const char *name)
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600352{
Quentin Schulz1e33bff2024-01-17 18:59:12 +0100353 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Ășnc20046f2021-11-04 00:23:23 +0100358 return (char *)(gd->env_buf);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600359
Marek BehĂșnc20046f2021-11-04 00:23:23 +0100360 return NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600361}
362
Quentin Schulz7665d332024-01-17 18:59:11 +0100363/*
364 * Look up the variable from the default environment and store its value in buf
365 */
366int 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 Glass97385862019-08-01 09:47:00 -0600371void env_set_default(const char *s, int flags)
Harald Welte72eb50a2008-07-07 15:40:39 +0800372{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200373 if (s) {
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300374 if ((flags & H_INTERACTIVE) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200375 printf("*** Warning - %s, "
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300376 "using default environment\n\n", s);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200377 } else {
378 puts(s);
379 }
380 } else {
Maxime Ripard12e6eac2018-01-23 21:16:57 +0100381 debug("Using default environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200382 }
383
Marek Vasutab6b15a2020-07-07 20:51:34 +0200384 flags |= H_DEFAULT;
Marek BehĂșn73d25342021-10-22 15:47:24 +0200385 if (himport_r(&env_htab, default_environment,
Alexander Holler3e12be72014-07-14 17:49:55 +0200386 sizeof(default_environment), '\0', flags, 0,
Marek BehĂșn08c6b042021-11-04 00:23:21 +0100387 0, NULL) == 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200388 pr_err("## Error: Environment import failed: errno = %d\n",
389 errno);
Marek BehĂșn08c6b042021-11-04 00:23:21 +0100390 return;
391 }
Igor Grinberg3e773592011-11-07 01:14:11 +0000392
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200393 gd->flags |= GD_FLG_ENV_READY;
Michal Simek32bb7c82016-05-30 16:06:54 +0200394 gd->flags |= GD_FLG_ENV_DEFAULT;
Harald Welte72eb50a2008-07-07 15:40:39 +0800395}
396
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000397/* [re]set individual variables to their value in the default environment */
Simon Glasseec97962019-08-01 09:46:56 -0600398int env_set_default_vars(int nvars, char * const vars[], int flags)
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000399{
400 /*
401 * Special use-case: import from default environment
402 * (and use \0 as a separator)
403 */
Marek Vasutab6b15a2020-07-07 20:51:34 +0200404 flags |= H_NOCLEAR | H_DEFAULT;
Marek BehĂșn73d25342021-10-22 15:47:24 +0200405 return himport_r(&env_htab, default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600406 sizeof(default_environment), '\0',
Yaniv Levinsky23840d12018-06-24 19:16:55 +0300407 flags, 0, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000408}
409
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200410/*
411 * Check if CRC is valid and (if yes) import the environment.
412 * Note that "buf" may or may not be aligned.
413 */
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200414int env_import(const char *buf, int check, int flags)
wdenk05706fd2002-09-28 17:48:27 +0000415{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200416 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000417
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200418 if (check) {
419 uint32_t crc;
420
421 memcpy(&crc, &ep->crc, sizeof(crc));
422
423 if (crc32(0, ep->data, ENV_SIZE) != crc) {
Simon Glass97385862019-08-01 09:47:00 -0600424 env_set_default("bad CRC", 0);
Sam Protsenkoc3097882019-01-18 21:19:03 +0200425 return -ENOMSG; /* needed for env_load() */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200426 }
427 }
wdenk05706fd2002-09-28 17:48:27 +0000428
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200429 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600430 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200431 gd->flags |= GD_FLG_ENV_READY;
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100432 return 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200433 }
434
Masahiro Yamada81e10422017-09-16 14:10:41 +0900435 pr_err("Cannot import environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200436
Simon Glass97385862019-08-01 09:47:00 -0600437 env_set_default("import failed", 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200438
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100439 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200440}
441
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000442#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
443static unsigned char env_flags;
444
Heiko Schocher30651132020-10-10 10:28:04 +0200445int env_check_redund(const char *buf1, int buf1_read_fail,
446 const char *buf2, int buf2_read_fail)
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000447{
Brandon Maier61374b02020-12-17 17:19:18 -0600448 int crc1_ok = 0, crc2_ok = 0;
Heiko Schocher30651132020-10-10 10:28:04 +0200449 env_t *tmp_env1, *tmp_env2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000450
451 tmp_env1 = (env_t *)buf1;
452 tmp_env2 = (env_t *)buf2;
453
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100454 if (buf1_read_fail && buf2_read_fail) {
455 puts("*** Error - No Valid Environment Area found\n");
Brandon Maier61374b02020-12-17 17:19:18 -0600456 return -EIO;
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100457 } else if (buf1_read_fail || buf2_read_fail) {
458 puts("*** Warning - some problems detected ");
459 puts("reading environment; recovered successfully\n");
460 }
461
Brandon Maier61374b02020-12-17 17:19:18 -0600462 if (!buf1_read_fail)
463 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
464 tmp_env1->crc;
465 if (!buf2_read_fail)
466 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
467 tmp_env2->crc;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000468
469 if (!crc1_ok && !crc2_ok) {
Ashok Reddy Somae50e0d52023-07-04 00:16:07 -0600470 gd->env_valid = ENV_INVALID;
Sam Protsenkoc3097882019-01-18 21:19:03 +0200471 return -ENOMSG; /* needed for env_load() */
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000472 } else if (crc1_ok && !crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600473 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000474 } else if (!crc1_ok && crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600475 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000476 } else {
477 /* both ok - check serial */
478 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600479 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000480 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600481 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000482 else if (tmp_env1->flags > tmp_env2->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600483 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000484 else if (tmp_env2->flags > tmp_env1->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600485 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000486 else /* flags are equal - almost impossible */
Simon Glass1f39d0b2017-08-20 04:45:15 -0600487 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000488 }
489
Heiko Schocher30651132020-10-10 10:28:04 +0200490 return 0;
491}
492
493int env_import_redund(const char *buf1, int buf1_read_fail,
494 const char *buf2, int buf2_read_fail,
495 int flags)
496{
497 env_t *ep;
498 int ret;
499
500 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
501
502 if (ret == -EIO) {
503 env_set_default("bad env area", 0);
504 return -EIO;
Heiko Schocher30651132020-10-10 10:28:04 +0200505 } else if (ret == -ENOMSG) {
506 env_set_default("bad CRC", 0);
507 return -ENOMSG;
508 }
509
Simon Glass1f39d0b2017-08-20 04:45:15 -0600510 if (gd->env_valid == ENV_VALID)
Heiko Schocher30651132020-10-10 10:28:04 +0200511 ep = (env_t *)buf1;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000512 else
Heiko Schocher30651132020-10-10 10:28:04 +0200513 ep = (env_t *)buf2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000514
515 env_flags = ep->flags;
Heiko Schocher30651132020-10-10 10:28:04 +0200516
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200517 return env_import((char *)ep, 0, flags);
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000518}
519#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
520
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400521/* Export the environment and generate CRC for it. */
Marek Vasutd73c1292014-03-05 19:59:50 +0100522int env_export(env_t *env_out)
523{
524 char *res;
525 ssize_t len;
526
527 res = (char *)env_out->data;
528 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
529 if (len < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900530 pr_err("Cannot export environment: errno = %d\n", errno);
Marek Vasutd73c1292014-03-05 19:59:50 +0100531 return 1;
532 }
Marek Vasut486112d2014-03-05 19:59:51 +0100533
Marek Vasutd73c1292014-03-05 19:59:50 +0100534 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
535
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000536#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
537 env_out->flags = ++env_flags; /* increase the serial */
538#endif
539
Marek Vasutd73c1292014-03-05 19:59:50 +0100540 return 0;
541}
542
Igor Grinberg3e773592011-11-07 01:14:11 +0000543void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200544{
Simon Glass1f39d0b2017-08-20 04:45:15 -0600545 if (gd->env_valid == ENV_INVALID) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000546#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
547 /* Environment not changable */
Simon Glass97385862019-08-01 09:47:00 -0600548 env_set_default(NULL, 0);
wdenk05706fd2002-09-28 17:48:27 +0000549#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000550 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Simon Glass97385862019-08-01 09:47:00 -0600551 env_set_default("bad CRC", 0);
Lei Wene0909be2010-10-10 12:36:40 +0800552#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200553 } else {
Simon Glass17539572017-08-03 12:22:07 -0600554 env_load();
wdenk05706fd2002-09-28 17:48:27 +0000555 }
wdenk05706fd2002-09-28 17:48:27 +0000556}
wdenk3902d702004-04-15 18:22:41 +0000557
Boris Brezillon619fc682018-12-05 09:26:50 +0100558#ifdef CONFIG_AUTO_COMPLETE
559int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
560 bool dollar_comp)
wdenk3902d702004-04-15 18:22:41 +0000561{
Simon Glass1a236862019-08-02 09:44:18 -0600562 struct env_entry *match;
Mike Frysinger8d882322010-12-17 16:51:59 -0500563 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000564
Boris Brezillon619fc682018-12-05 09:26:50 +0100565 if (dollar_comp) {
566 /*
567 * When doing $ completion, the first character should
568 * obviously be a '$'.
569 */
570 if (var[0] != '$')
571 return 0;
572
573 var++;
574
575 /*
576 * The second one, if present, should be a '{', as some
577 * configuration of the u-boot shell expand ${var} but not
578 * $var.
579 */
580 if (var[0] == '{')
581 var++;
582 else if (var[0] != '\0')
583 return 0;
584 }
585
Mike Frysinger8d882322010-12-17 16:51:59 -0500586 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000587 found = 0;
588 cmdv[0] = NULL;
589
Mike Frysinger8d882322010-12-17 16:51:59 -0500590 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
591 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000592
Boris Brezillon619fc682018-12-05 09:26:50 +0100593 if (found >= maxv - 2 ||
594 bufsz < vallen + (dollar_comp ? 3 : 0))
wdenk3902d702004-04-15 18:22:41 +0000595 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500596
wdenk3902d702004-04-15 18:22:41 +0000597 cmdv[found++] = buf;
Boris Brezillon619fc682018-12-05 09:26:50 +0100598
599 /* Add the '${' prefix to each var when doing $ completion. */
600 if (dollar_comp) {
601 strcpy(buf, "${");
602 buf += 2;
603 bufsz -= 3;
604 }
605
Mike Frysinger8d882322010-12-17 16:51:59 -0500606 memcpy(buf, match->key, vallen);
607 buf += vallen;
608 bufsz -= vallen;
Boris Brezillon619fc682018-12-05 09:26:50 +0100609
610 if (dollar_comp) {
611 /*
612 * This one is a bit odd: vallen already contains the
613 * '\0' character but we need to add the '}' suffix,
614 * hence the buf - 1 here. strcpy() will add the '\0'
615 * character just after '}'. buf is then incremented
616 * to account for the extra '}' we just added.
617 */
618 strcpy(buf - 1, "}");
619 buf++;
620 }
wdenk3902d702004-04-15 18:22:41 +0000621 }
622
Mike Frysinger8d882322010-12-17 16:51:59 -0500623 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
624
625 if (idx)
Boris Brezillon619fc682018-12-05 09:26:50 +0100626 cmdv[found++] = dollar_comp ? "${...}" : "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000627
wdenk3902d702004-04-15 18:22:41 +0000628 cmdv[found] = NULL;
629 return found;
630}
631#endif
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200632
633#ifdef CONFIG_ENV_IMPORT_FDT
634void env_import_fdt(void)
635{
636 const char *path;
637 struct ofprop prop;
638 ofnode node;
639 int res;
640
641 path = env_get("env_fdt_path");
642 if (!path || !path[0])
643 return;
644
645 node = ofnode_path(path);
646 if (!ofnode_valid(node)) {
647 printf("Warning: device tree node '%s' not found\n", path);
648 return;
649 }
650
Simon Glassfec058d2022-09-06 20:27:13 -0600651 for (res = ofnode_first_property(node, &prop);
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200652 !res;
Simon Glassfec058d2022-09-06 20:27:13 -0600653 res = ofnode_next_property(&prop)) {
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200654 const char *name, *val;
655
Simon Glassd0aff8b2022-09-06 20:27:14 -0600656 val = ofprop_get_property(&prop, &name, NULL);
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200657 env_set(name, val);
658 }
659}
660#endif