blob: 656748c1f5b72cf9ddfec5ea3611b85fab207c41 [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
10#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.h>
wdenk05706fd2002-09-28 17:48:27 +000012#include <command.h>
Simon Glass8fe40932019-08-01 09:46:44 -060013#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060014#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass16ff5702019-11-14 12:57:19 -070016#include <sort.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060018#include <linux/printk.h>
wdenk05706fd2002-09-28 17:48:27 +000019#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020020#include <search.h>
21#include <errno.h>
wdenk05706fd2002-09-28 17:48:27 +000022#include <malloc.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070023#include <u-boot/crc.h>
Rasmus Villemoescf8e5432021-04-21 11:06:54 +020024#include <dm/ofnode.h>
Marek BehĂșn30044c52021-10-17 17:36:38 +020025#include <net.h>
26#include <watchdog.h>
wdenk05706fd2002-09-28 17:48:27 +000027
Wolfgang Denk6405a152006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
29
wdenk05706fd2002-09-28 17:48:27 +000030/************************************************************************
31 * Default settings to be used when no valid environment is found
32 */
Joe Hershberger81321142012-10-12 08:48:51 +000033#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000034
Gerlando Falautod3925952012-08-24 00:11:39 +000035struct hsearch_data env_htab = {
Joe Hershberger71497d02012-12-11 22:16:31 -060036 .change_ok = env_flags_validate,
Gerlando Falautod3925952012-08-24 00:11:39 +000037};
Mike Frysinger92d7a992010-12-08 06:26:04 -050038
Joe Hershberger864ec562012-12-11 22:16:22 -060039/*
Tom Rinib8ca93f2023-10-26 14:31:16 -040040 * 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Ășn30044c52021-10-17 17:36:38 +020044 */
Tom Rinib8ca93f2023-10-26 14:31:16 -040045static int env_id = 1;
46
47int env_get_id(void)
48{
49 return env_id;
50}
51
52void env_inc_id(void)
53{
54 env_id++;
55}
56
57int 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, '=')) {
88 printf("## Error: illegal character '='"
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
137int 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Ășn30044c52021-10-17 17:36:38 +0200150
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 Schuchardt47b4c022022-01-19 18:05:50 +0100156 * Return: 0 if ok, 1 on error
Marek BehĂșn30044c52021-10-17 17:36:38 +0200157 */
158int 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 Schuchardt47b4c022022-01-19 18:05:50 +0100171 * Return: 0 if ok, 1 on error
Marek BehĂșn30044c52021-10-17 17:36:38 +0200172 */
173int 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
181ulong 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
196int 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
202int 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 */
219char *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 Roese80877fa2022-09-02 14:10:46 +0200224 schedule();
Marek BehĂșn30044c52021-10-17 17:36:38 +0200225
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Ășne087a462021-11-04 00:23:22 +0100234 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
Marek BehĂșn30044c52021-10-17 17:36:38 +0200235 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 */
245char *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Ășnc20046f2021-11-04 00:23:23 +0100257static int env_get_from_linear(const char *env, const char *name, char *buf,
258 unsigned len)
Marek BehĂșn30044c52021-10-17 17:36:38 +0200259{
Marek BehĂșnc20046f2021-11-04 00:23:23 +0100260 const char *p, *end;
Marek BehĂșn30044c52021-10-17 17:36:38 +0200261 size_t name_len;
262
263 if (name == NULL || *name == '\0')
264 return -1;
265
266 name_len = strlen(name);
267
Marek BehĂșn30044c52021-10-17 17:36:38 +0200268 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Ășnc20046f2021-11-04 00:23:23 +0100295/*
296 * Look up variable from environment for restricted C runtime env.
297 */
298int 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Ășn30044c52021-10-17 17:36:38 +0200310/**
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 Schuchardt47b4c022022-01-19 18:05:50 +0100317 * Return: the decoded value, or default_val if not found
Marek BehĂșn30044c52021-10-17 17:36:38 +0200318 */
319ulong 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 Hershberger864ec562012-12-11 22:16:22 -0600331 * Read an environment variable as a boolean
332 * Return -1 if variable does not exist (default to true)
333 */
Simon Glass22c34c22017-08-03 12:22:13 -0600334int env_get_yesno(const char *var)
Joe Hershberger864ec562012-12-11 22:16:22 -0600335{
Simon Glass64b723f2017-08-03 12:22:12 -0600336 char *s = env_get(var);
Joe Hershberger864ec562012-12-11 22:16:22 -0600337
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 Glass46b530a2021-10-21 21:08:52 -0600344bool env_get_autostart(void)
345{
346 return env_get_yesno("autostart") == 1;
347}
348
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600349/*
350 * Look up the variable from the default environment
351 */
Simon Glassda1a1342017-08-03 12:22:15 -0600352char *env_get_default(const char *name)
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600353{
Marek BehĂșnc20046f2021-11-04 00:23:23 +0100354 if (env_get_from_linear(default_environment, name,
355 (char *)(gd->env_buf),
356 sizeof(gd->env_buf)) >= 0)
357 return (char *)(gd->env_buf);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600358
Marek BehĂșnc20046f2021-11-04 00:23:23 +0100359 return NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600360}
361
Simon Glass97385862019-08-01 09:47:00 -0600362void env_set_default(const char *s, int flags)
Harald Welte72eb50a2008-07-07 15:40:39 +0800363{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200364 if (s) {
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300365 if ((flags & H_INTERACTIVE) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200366 printf("*** Warning - %s, "
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300367 "using default environment\n\n", s);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200368 } else {
369 puts(s);
370 }
371 } else {
Maxime Ripard12e6eac2018-01-23 21:16:57 +0100372 debug("Using default environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200373 }
374
Marek Vasutab6b15a2020-07-07 20:51:34 +0200375 flags |= H_DEFAULT;
Marek BehĂșn73d25342021-10-22 15:47:24 +0200376 if (himport_r(&env_htab, default_environment,
Alexander Holler3e12be72014-07-14 17:49:55 +0200377 sizeof(default_environment), '\0', flags, 0,
Marek BehĂșn08c6b042021-11-04 00:23:21 +0100378 0, NULL) == 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200379 pr_err("## Error: Environment import failed: errno = %d\n",
380 errno);
Marek BehĂșn08c6b042021-11-04 00:23:21 +0100381 return;
382 }
Igor Grinberg3e773592011-11-07 01:14:11 +0000383
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200384 gd->flags |= GD_FLG_ENV_READY;
Michal Simek32bb7c82016-05-30 16:06:54 +0200385 gd->flags |= GD_FLG_ENV_DEFAULT;
Harald Welte72eb50a2008-07-07 15:40:39 +0800386}
387
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000388
389/* [re]set individual variables to their value in the default environment */
Simon Glasseec97962019-08-01 09:46:56 -0600390int env_set_default_vars(int nvars, char * const vars[], int flags)
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000391{
392 /*
393 * Special use-case: import from default environment
394 * (and use \0 as a separator)
395 */
Marek Vasutab6b15a2020-07-07 20:51:34 +0200396 flags |= H_NOCLEAR | H_DEFAULT;
Marek BehĂșn73d25342021-10-22 15:47:24 +0200397 return himport_r(&env_htab, default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600398 sizeof(default_environment), '\0',
Yaniv Levinsky23840d12018-06-24 19:16:55 +0300399 flags, 0, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000400}
401
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200402/*
403 * Check if CRC is valid and (if yes) import the environment.
404 * Note that "buf" may or may not be aligned.
405 */
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200406int env_import(const char *buf, int check, int flags)
wdenk05706fd2002-09-28 17:48:27 +0000407{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200408 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000409
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200410 if (check) {
411 uint32_t crc;
412
413 memcpy(&crc, &ep->crc, sizeof(crc));
414
415 if (crc32(0, ep->data, ENV_SIZE) != crc) {
Simon Glass97385862019-08-01 09:47:00 -0600416 env_set_default("bad CRC", 0);
Sam Protsenkoc3097882019-01-18 21:19:03 +0200417 return -ENOMSG; /* needed for env_load() */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200418 }
419 }
wdenk05706fd2002-09-28 17:48:27 +0000420
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200421 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600422 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200423 gd->flags |= GD_FLG_ENV_READY;
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100424 return 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200425 }
426
Masahiro Yamada81e10422017-09-16 14:10:41 +0900427 pr_err("Cannot import environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200428
Simon Glass97385862019-08-01 09:47:00 -0600429 env_set_default("import failed", 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200430
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100431 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200432}
433
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000434#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
435static unsigned char env_flags;
436
Heiko Schocher30651132020-10-10 10:28:04 +0200437int env_check_redund(const char *buf1, int buf1_read_fail,
438 const char *buf2, int buf2_read_fail)
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000439{
Brandon Maier61374b02020-12-17 17:19:18 -0600440 int crc1_ok = 0, crc2_ok = 0;
Heiko Schocher30651132020-10-10 10:28:04 +0200441 env_t *tmp_env1, *tmp_env2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000442
443 tmp_env1 = (env_t *)buf1;
444 tmp_env2 = (env_t *)buf2;
445
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100446 if (buf1_read_fail && buf2_read_fail) {
447 puts("*** Error - No Valid Environment Area found\n");
Brandon Maier61374b02020-12-17 17:19:18 -0600448 return -EIO;
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100449 } else if (buf1_read_fail || buf2_read_fail) {
450 puts("*** Warning - some problems detected ");
451 puts("reading environment; recovered successfully\n");
452 }
453
Brandon Maier61374b02020-12-17 17:19:18 -0600454 if (!buf1_read_fail)
455 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
456 tmp_env1->crc;
457 if (!buf2_read_fail)
458 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
459 tmp_env2->crc;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000460
461 if (!crc1_ok && !crc2_ok) {
Ashok Reddy Somae50e0d52023-07-04 00:16:07 -0600462 gd->env_valid = ENV_INVALID;
Sam Protsenkoc3097882019-01-18 21:19:03 +0200463 return -ENOMSG; /* needed for env_load() */
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000464 } else if (crc1_ok && !crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600465 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000466 } else if (!crc1_ok && crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600467 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000468 } else {
469 /* both ok - check serial */
470 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600471 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000472 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600473 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000474 else if (tmp_env1->flags > tmp_env2->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600475 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000476 else if (tmp_env2->flags > tmp_env1->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600477 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000478 else /* flags are equal - almost impossible */
Simon Glass1f39d0b2017-08-20 04:45:15 -0600479 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000480 }
481
Heiko Schocher30651132020-10-10 10:28:04 +0200482 return 0;
483}
484
485int env_import_redund(const char *buf1, int buf1_read_fail,
486 const char *buf2, int buf2_read_fail,
487 int flags)
488{
489 env_t *ep;
490 int ret;
491
492 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
493
494 if (ret == -EIO) {
495 env_set_default("bad env area", 0);
496 return -EIO;
Heiko Schocher30651132020-10-10 10:28:04 +0200497 } else if (ret == -ENOMSG) {
498 env_set_default("bad CRC", 0);
499 return -ENOMSG;
500 }
501
Simon Glass1f39d0b2017-08-20 04:45:15 -0600502 if (gd->env_valid == ENV_VALID)
Heiko Schocher30651132020-10-10 10:28:04 +0200503 ep = (env_t *)buf1;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000504 else
Heiko Schocher30651132020-10-10 10:28:04 +0200505 ep = (env_t *)buf2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000506
507 env_flags = ep->flags;
Heiko Schocher30651132020-10-10 10:28:04 +0200508
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200509 return env_import((char *)ep, 0, flags);
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000510}
511#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
512
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400513/* Export the environment and generate CRC for it. */
Marek Vasutd73c1292014-03-05 19:59:50 +0100514int env_export(env_t *env_out)
515{
516 char *res;
517 ssize_t len;
518
519 res = (char *)env_out->data;
520 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
521 if (len < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900522 pr_err("Cannot export environment: errno = %d\n", errno);
Marek Vasutd73c1292014-03-05 19:59:50 +0100523 return 1;
524 }
Marek Vasut486112d2014-03-05 19:59:51 +0100525
Marek Vasutd73c1292014-03-05 19:59:50 +0100526 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
527
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000528#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
529 env_out->flags = ++env_flags; /* increase the serial */
530#endif
531
Marek Vasutd73c1292014-03-05 19:59:50 +0100532 return 0;
533}
534
Igor Grinberg3e773592011-11-07 01:14:11 +0000535void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200536{
Simon Glass1f39d0b2017-08-20 04:45:15 -0600537 if (gd->env_valid == ENV_INVALID) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000538#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
539 /* Environment not changable */
Simon Glass97385862019-08-01 09:47:00 -0600540 env_set_default(NULL, 0);
wdenk05706fd2002-09-28 17:48:27 +0000541#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000542 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Simon Glass97385862019-08-01 09:47:00 -0600543 env_set_default("bad CRC", 0);
Lei Wene0909be2010-10-10 12:36:40 +0800544#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200545 } else {
Simon Glass17539572017-08-03 12:22:07 -0600546 env_load();
wdenk05706fd2002-09-28 17:48:27 +0000547 }
wdenk05706fd2002-09-28 17:48:27 +0000548}
wdenk3902d702004-04-15 18:22:41 +0000549
Boris Brezillon619fc682018-12-05 09:26:50 +0100550#ifdef CONFIG_AUTO_COMPLETE
551int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
552 bool dollar_comp)
wdenk3902d702004-04-15 18:22:41 +0000553{
Simon Glass1a236862019-08-02 09:44:18 -0600554 struct env_entry *match;
Mike Frysinger8d882322010-12-17 16:51:59 -0500555 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000556
Boris Brezillon619fc682018-12-05 09:26:50 +0100557 if (dollar_comp) {
558 /*
559 * When doing $ completion, the first character should
560 * obviously be a '$'.
561 */
562 if (var[0] != '$')
563 return 0;
564
565 var++;
566
567 /*
568 * The second one, if present, should be a '{', as some
569 * configuration of the u-boot shell expand ${var} but not
570 * $var.
571 */
572 if (var[0] == '{')
573 var++;
574 else if (var[0] != '\0')
575 return 0;
576 }
577
Mike Frysinger8d882322010-12-17 16:51:59 -0500578 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000579 found = 0;
580 cmdv[0] = NULL;
581
Boris Brezillon619fc682018-12-05 09:26:50 +0100582
Mike Frysinger8d882322010-12-17 16:51:59 -0500583 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
584 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000585
Boris Brezillon619fc682018-12-05 09:26:50 +0100586 if (found >= maxv - 2 ||
587 bufsz < vallen + (dollar_comp ? 3 : 0))
wdenk3902d702004-04-15 18:22:41 +0000588 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500589
wdenk3902d702004-04-15 18:22:41 +0000590 cmdv[found++] = buf;
Boris Brezillon619fc682018-12-05 09:26:50 +0100591
592 /* Add the '${' prefix to each var when doing $ completion. */
593 if (dollar_comp) {
594 strcpy(buf, "${");
595 buf += 2;
596 bufsz -= 3;
597 }
598
Mike Frysinger8d882322010-12-17 16:51:59 -0500599 memcpy(buf, match->key, vallen);
600 buf += vallen;
601 bufsz -= vallen;
Boris Brezillon619fc682018-12-05 09:26:50 +0100602
603 if (dollar_comp) {
604 /*
605 * This one is a bit odd: vallen already contains the
606 * '\0' character but we need to add the '}' suffix,
607 * hence the buf - 1 here. strcpy() will add the '\0'
608 * character just after '}'. buf is then incremented
609 * to account for the extra '}' we just added.
610 */
611 strcpy(buf - 1, "}");
612 buf++;
613 }
wdenk3902d702004-04-15 18:22:41 +0000614 }
615
Mike Frysinger8d882322010-12-17 16:51:59 -0500616 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
617
618 if (idx)
Boris Brezillon619fc682018-12-05 09:26:50 +0100619 cmdv[found++] = dollar_comp ? "${...}" : "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000620
wdenk3902d702004-04-15 18:22:41 +0000621 cmdv[found] = NULL;
622 return found;
623}
624#endif
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200625
626#ifdef CONFIG_ENV_IMPORT_FDT
627void env_import_fdt(void)
628{
629 const char *path;
630 struct ofprop prop;
631 ofnode node;
632 int res;
633
634 path = env_get("env_fdt_path");
635 if (!path || !path[0])
636 return;
637
638 node = ofnode_path(path);
639 if (!ofnode_valid(node)) {
640 printf("Warning: device tree node '%s' not found\n", path);
641 return;
642 }
643
Simon Glassfec058d2022-09-06 20:27:13 -0600644 for (res = ofnode_first_property(node, &prop);
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200645 !res;
Simon Glassfec058d2022-09-06 20:27:13 -0600646 res = ofnode_next_property(&prop)) {
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200647 const char *name, *val;
648
Simon Glassd0aff8b2022-09-06 20:27:14 -0600649 val = ofprop_get_property(&prop, &name, NULL);
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200650 env_set(name, val);
651 }
652}
653#endif