blob: 21045a7eb33306f5852fe594b5cacc5871ab59b5 [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>
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/*
Marek Behún30044c52021-10-17 17:36:38 +020039 * This env_set() function is defined in cmd/nvedit.c, since it calls
40 * _do_env_set(), whis is a static function in that file.
41 *
42 * int env_set(const char *varname, const char *varvalue);
43 */
44
45/**
46 * Set an environment variable to an integer value
47 *
48 * @param varname Environment variable to set
49 * @param value Value to set it to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010050 * Return: 0 if ok, 1 on error
Marek Behún30044c52021-10-17 17:36:38 +020051 */
52int env_set_ulong(const char *varname, ulong value)
53{
54 /* TODO: this should be unsigned */
55 char *str = simple_itoa(value);
56
57 return env_set(varname, str);
58}
59
60/**
61 * Set an environment variable to an value in hex
62 *
63 * @param varname Environment variable to set
64 * @param value Value to set it to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010065 * Return: 0 if ok, 1 on error
Marek Behún30044c52021-10-17 17:36:38 +020066 */
67int env_set_hex(const char *varname, ulong value)
68{
69 char str[17];
70
71 sprintf(str, "%lx", value);
72 return env_set(varname, str);
73}
74
75ulong env_get_hex(const char *varname, ulong default_val)
76{
77 const char *s;
78 ulong value;
79 char *endp;
80
81 s = env_get(varname);
82 if (s)
83 value = hextoul(s, &endp);
84 if (!s || endp == s)
85 return default_val;
86
87 return value;
88}
89
90int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
91{
92 string_to_enetaddr(env_get(name), enetaddr);
93 return is_valid_ethaddr(enetaddr);
94}
95
96int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr)
97{
98 char buf[ARP_HLEN_ASCII + 1];
99
100 if (eth_env_get_enetaddr(name, (uint8_t *)buf))
101 return -EEXIST;
102
103 sprintf(buf, "%pM", enetaddr);
104
105 return env_set(name, buf);
106}
107
108/*
109 * Look up variable from environment,
110 * return address of storage for that variable,
111 * or NULL if not found
112 */
113char *env_get(const char *name)
114{
115 if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
116 struct env_entry e, *ep;
117
Stefan Roese80877fa2022-09-02 14:10:46 +0200118 schedule();
Marek Behún30044c52021-10-17 17:36:38 +0200119
120 e.key = name;
121 e.data = NULL;
122 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
123
124 return ep ? ep->data : NULL;
125 }
126
127 /* restricted capabilities before import */
Marek Behúne087a462021-11-04 00:23:22 +0100128 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) >= 0)
Marek Behún30044c52021-10-17 17:36:38 +0200129 return (char *)(gd->env_buf);
130
131 return NULL;
132}
133
134/*
135 * Like env_get, but prints an error if envvar isn't defined in the
136 * environment. It always returns what env_get does, so it can be used in
137 * place of env_get without changing error handling otherwise.
138 */
139char *from_env(const char *envvar)
140{
141 char *ret;
142
143 ret = env_get(envvar);
144
145 if (!ret)
146 printf("missing environment variable: %s\n", envvar);
147
148 return ret;
149}
150
Marek Behúnc20046f2021-11-04 00:23:23 +0100151static int env_get_from_linear(const char *env, const char *name, char *buf,
152 unsigned len)
Marek Behún30044c52021-10-17 17:36:38 +0200153{
Marek Behúnc20046f2021-11-04 00:23:23 +0100154 const char *p, *end;
Marek Behún30044c52021-10-17 17:36:38 +0200155 size_t name_len;
156
157 if (name == NULL || *name == '\0')
158 return -1;
159
160 name_len = strlen(name);
161
Marek Behún30044c52021-10-17 17:36:38 +0200162 for (p = env; *p != '\0'; p = end + 1) {
163 const char *value;
164 unsigned res;
165
166 for (end = p; *end != '\0'; ++end)
167 if (end - env >= CONFIG_ENV_SIZE)
168 return -1;
169
170 if (strncmp(name, p, name_len) || p[name_len] != '=')
171 continue;
172 value = &p[name_len + 1];
173
174 res = end - value;
175 memcpy(buf, value, min(len, res + 1));
176
177 if (len <= res) {
178 buf[len - 1] = '\0';
179 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
180 len, name);
181 }
182
183 return res;
184 }
185
186 return -1;
187}
188
Marek Behúnc20046f2021-11-04 00:23:23 +0100189/*
190 * Look up variable from environment for restricted C runtime env.
191 */
192int env_get_f(const char *name, char *buf, unsigned len)
193{
194 const char *env;
195
196 if (gd->env_valid == ENV_INVALID)
197 env = default_environment;
198 else
199 env = (const char *)gd->env_addr;
200
201 return env_get_from_linear(env, name, buf, len);
202}
203
Marek Behún30044c52021-10-17 17:36:38 +0200204/**
205 * Decode the integer value of an environment variable and return it.
206 *
207 * @param name Name of environment variable
208 * @param base Number base to use (normally 10, or 16 for hex)
209 * @param default_val Default value to return if the variable is not
210 * found
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100211 * Return: the decoded value, or default_val if not found
Marek Behún30044c52021-10-17 17:36:38 +0200212 */
213ulong env_get_ulong(const char *name, int base, ulong default_val)
214{
215 /*
216 * We can use env_get() here, even before relocation, since the
217 * environment variable value is an integer and thus short.
218 */
219 const char *str = env_get(name);
220
221 return str ? simple_strtoul(str, NULL, base) : default_val;
222}
223
224/*
Joe Hershberger864ec562012-12-11 22:16:22 -0600225 * Read an environment variable as a boolean
226 * Return -1 if variable does not exist (default to true)
227 */
Simon Glass22c34c22017-08-03 12:22:13 -0600228int env_get_yesno(const char *var)
Joe Hershberger864ec562012-12-11 22:16:22 -0600229{
Simon Glass64b723f2017-08-03 12:22:12 -0600230 char *s = env_get(var);
Joe Hershberger864ec562012-12-11 22:16:22 -0600231
232 if (s == NULL)
233 return -1;
234 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
235 1 : 0;
236}
237
Simon Glass46b530a2021-10-21 21:08:52 -0600238bool env_get_autostart(void)
239{
240 return env_get_yesno("autostart") == 1;
241}
242
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600243/*
244 * Look up the variable from the default environment
245 */
Simon Glassda1a1342017-08-03 12:22:15 -0600246char *env_get_default(const char *name)
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600247{
Marek Behúnc20046f2021-11-04 00:23:23 +0100248 if (env_get_from_linear(default_environment, name,
249 (char *)(gd->env_buf),
250 sizeof(gd->env_buf)) >= 0)
251 return (char *)(gd->env_buf);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600252
Marek Behúnc20046f2021-11-04 00:23:23 +0100253 return NULL;
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600254}
255
Simon Glass97385862019-08-01 09:47:00 -0600256void env_set_default(const char *s, int flags)
Harald Welte72eb50a2008-07-07 15:40:39 +0800257{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200258 if (s) {
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300259 if ((flags & H_INTERACTIVE) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200260 printf("*** Warning - %s, "
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300261 "using default environment\n\n", s);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200262 } else {
263 puts(s);
264 }
265 } else {
Maxime Ripard12e6eac2018-01-23 21:16:57 +0100266 debug("Using default environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200267 }
268
Marek Vasutab6b15a2020-07-07 20:51:34 +0200269 flags |= H_DEFAULT;
Marek Behún73d25342021-10-22 15:47:24 +0200270 if (himport_r(&env_htab, default_environment,
Alexander Holler3e12be72014-07-14 17:49:55 +0200271 sizeof(default_environment), '\0', flags, 0,
Marek Behún08c6b042021-11-04 00:23:21 +0100272 0, NULL) == 0) {
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200273 pr_err("## Error: Environment import failed: errno = %d\n",
274 errno);
Marek Behún08c6b042021-11-04 00:23:21 +0100275 return;
276 }
Igor Grinberg3e773592011-11-07 01:14:11 +0000277
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200278 gd->flags |= GD_FLG_ENV_READY;
Michal Simek32bb7c82016-05-30 16:06:54 +0200279 gd->flags |= GD_FLG_ENV_DEFAULT;
Harald Welte72eb50a2008-07-07 15:40:39 +0800280}
281
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000282
283/* [re]set individual variables to their value in the default environment */
Simon Glasseec97962019-08-01 09:46:56 -0600284int env_set_default_vars(int nvars, char * const vars[], int flags)
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000285{
286 /*
287 * Special use-case: import from default environment
288 * (and use \0 as a separator)
289 */
Marek Vasutab6b15a2020-07-07 20:51:34 +0200290 flags |= H_NOCLEAR | H_DEFAULT;
Marek Behún73d25342021-10-22 15:47:24 +0200291 return himport_r(&env_htab, default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600292 sizeof(default_environment), '\0',
Yaniv Levinsky23840d12018-06-24 19:16:55 +0300293 flags, 0, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000294}
295
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200296/*
297 * Check if CRC is valid and (if yes) import the environment.
298 * Note that "buf" may or may not be aligned.
299 */
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200300int env_import(const char *buf, int check, int flags)
wdenk05706fd2002-09-28 17:48:27 +0000301{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200302 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000303
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200304 if (check) {
305 uint32_t crc;
306
307 memcpy(&crc, &ep->crc, sizeof(crc));
308
309 if (crc32(0, ep->data, ENV_SIZE) != crc) {
Simon Glass97385862019-08-01 09:47:00 -0600310 env_set_default("bad CRC", 0);
Sam Protsenkoc3097882019-01-18 21:19:03 +0200311 return -ENOMSG; /* needed for env_load() */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200312 }
313 }
wdenk05706fd2002-09-28 17:48:27 +0000314
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200315 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600316 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200317 gd->flags |= GD_FLG_ENV_READY;
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100318 return 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200319 }
320
Masahiro Yamada81e10422017-09-16 14:10:41 +0900321 pr_err("Cannot import environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200322
Simon Glass97385862019-08-01 09:47:00 -0600323 env_set_default("import failed", 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200324
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100325 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200326}
327
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000328#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
329static unsigned char env_flags;
330
Heiko Schocher30651132020-10-10 10:28:04 +0200331int env_check_redund(const char *buf1, int buf1_read_fail,
332 const char *buf2, int buf2_read_fail)
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000333{
Brandon Maier61374b02020-12-17 17:19:18 -0600334 int crc1_ok = 0, crc2_ok = 0;
Heiko Schocher30651132020-10-10 10:28:04 +0200335 env_t *tmp_env1, *tmp_env2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000336
337 tmp_env1 = (env_t *)buf1;
338 tmp_env2 = (env_t *)buf2;
339
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100340 if (buf1_read_fail && buf2_read_fail) {
341 puts("*** Error - No Valid Environment Area found\n");
Brandon Maier61374b02020-12-17 17:19:18 -0600342 return -EIO;
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100343 } else if (buf1_read_fail || buf2_read_fail) {
344 puts("*** Warning - some problems detected ");
345 puts("reading environment; recovered successfully\n");
346 }
347
Brandon Maier61374b02020-12-17 17:19:18 -0600348 if (!buf1_read_fail)
349 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
350 tmp_env1->crc;
351 if (!buf2_read_fail)
352 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
353 tmp_env2->crc;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000354
355 if (!crc1_ok && !crc2_ok) {
Ashok Reddy Somae50e0d52023-07-04 00:16:07 -0600356 gd->env_valid = ENV_INVALID;
Sam Protsenkoc3097882019-01-18 21:19:03 +0200357 return -ENOMSG; /* needed for env_load() */
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000358 } else if (crc1_ok && !crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600359 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000360 } else if (!crc1_ok && crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600361 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000362 } else {
363 /* both ok - check serial */
364 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600365 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000366 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600367 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000368 else if (tmp_env1->flags > tmp_env2->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600369 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000370 else if (tmp_env2->flags > tmp_env1->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600371 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000372 else /* flags are equal - almost impossible */
Simon Glass1f39d0b2017-08-20 04:45:15 -0600373 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000374 }
375
Heiko Schocher30651132020-10-10 10:28:04 +0200376 return 0;
377}
378
379int env_import_redund(const char *buf1, int buf1_read_fail,
380 const char *buf2, int buf2_read_fail,
381 int flags)
382{
383 env_t *ep;
384 int ret;
385
386 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
387
388 if (ret == -EIO) {
389 env_set_default("bad env area", 0);
390 return -EIO;
Heiko Schocher30651132020-10-10 10:28:04 +0200391 } else if (ret == -ENOMSG) {
392 env_set_default("bad CRC", 0);
393 return -ENOMSG;
394 }
395
Simon Glass1f39d0b2017-08-20 04:45:15 -0600396 if (gd->env_valid == ENV_VALID)
Heiko Schocher30651132020-10-10 10:28:04 +0200397 ep = (env_t *)buf1;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000398 else
Heiko Schocher30651132020-10-10 10:28:04 +0200399 ep = (env_t *)buf2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000400
401 env_flags = ep->flags;
Heiko Schocher30651132020-10-10 10:28:04 +0200402
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200403 return env_import((char *)ep, 0, flags);
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000404}
405#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
406
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400407/* Export the environment and generate CRC for it. */
Marek Vasutd73c1292014-03-05 19:59:50 +0100408int env_export(env_t *env_out)
409{
410 char *res;
411 ssize_t len;
412
413 res = (char *)env_out->data;
414 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
415 if (len < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900416 pr_err("Cannot export environment: errno = %d\n", errno);
Marek Vasutd73c1292014-03-05 19:59:50 +0100417 return 1;
418 }
Marek Vasut486112d2014-03-05 19:59:51 +0100419
Marek Vasutd73c1292014-03-05 19:59:50 +0100420 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
421
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000422#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
423 env_out->flags = ++env_flags; /* increase the serial */
424#endif
425
Marek Vasutd73c1292014-03-05 19:59:50 +0100426 return 0;
427}
428
Igor Grinberg3e773592011-11-07 01:14:11 +0000429void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200430{
Simon Glass1f39d0b2017-08-20 04:45:15 -0600431 if (gd->env_valid == ENV_INVALID) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000432#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
433 /* Environment not changable */
Simon Glass97385862019-08-01 09:47:00 -0600434 env_set_default(NULL, 0);
wdenk05706fd2002-09-28 17:48:27 +0000435#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000436 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Simon Glass97385862019-08-01 09:47:00 -0600437 env_set_default("bad CRC", 0);
Lei Wene0909be2010-10-10 12:36:40 +0800438#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200439 } else {
Simon Glass17539572017-08-03 12:22:07 -0600440 env_load();
wdenk05706fd2002-09-28 17:48:27 +0000441 }
wdenk05706fd2002-09-28 17:48:27 +0000442}
wdenk3902d702004-04-15 18:22:41 +0000443
Boris Brezillon619fc682018-12-05 09:26:50 +0100444#ifdef CONFIG_AUTO_COMPLETE
445int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
446 bool dollar_comp)
wdenk3902d702004-04-15 18:22:41 +0000447{
Simon Glass1a236862019-08-02 09:44:18 -0600448 struct env_entry *match;
Mike Frysinger8d882322010-12-17 16:51:59 -0500449 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000450
Boris Brezillon619fc682018-12-05 09:26:50 +0100451 if (dollar_comp) {
452 /*
453 * When doing $ completion, the first character should
454 * obviously be a '$'.
455 */
456 if (var[0] != '$')
457 return 0;
458
459 var++;
460
461 /*
462 * The second one, if present, should be a '{', as some
463 * configuration of the u-boot shell expand ${var} but not
464 * $var.
465 */
466 if (var[0] == '{')
467 var++;
468 else if (var[0] != '\0')
469 return 0;
470 }
471
Mike Frysinger8d882322010-12-17 16:51:59 -0500472 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000473 found = 0;
474 cmdv[0] = NULL;
475
Boris Brezillon619fc682018-12-05 09:26:50 +0100476
Mike Frysinger8d882322010-12-17 16:51:59 -0500477 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
478 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000479
Boris Brezillon619fc682018-12-05 09:26:50 +0100480 if (found >= maxv - 2 ||
481 bufsz < vallen + (dollar_comp ? 3 : 0))
wdenk3902d702004-04-15 18:22:41 +0000482 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500483
wdenk3902d702004-04-15 18:22:41 +0000484 cmdv[found++] = buf;
Boris Brezillon619fc682018-12-05 09:26:50 +0100485
486 /* Add the '${' prefix to each var when doing $ completion. */
487 if (dollar_comp) {
488 strcpy(buf, "${");
489 buf += 2;
490 bufsz -= 3;
491 }
492
Mike Frysinger8d882322010-12-17 16:51:59 -0500493 memcpy(buf, match->key, vallen);
494 buf += vallen;
495 bufsz -= vallen;
Boris Brezillon619fc682018-12-05 09:26:50 +0100496
497 if (dollar_comp) {
498 /*
499 * This one is a bit odd: vallen already contains the
500 * '\0' character but we need to add the '}' suffix,
501 * hence the buf - 1 here. strcpy() will add the '\0'
502 * character just after '}'. buf is then incremented
503 * to account for the extra '}' we just added.
504 */
505 strcpy(buf - 1, "}");
506 buf++;
507 }
wdenk3902d702004-04-15 18:22:41 +0000508 }
509
Mike Frysinger8d882322010-12-17 16:51:59 -0500510 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
511
512 if (idx)
Boris Brezillon619fc682018-12-05 09:26:50 +0100513 cmdv[found++] = dollar_comp ? "${...}" : "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000514
wdenk3902d702004-04-15 18:22:41 +0000515 cmdv[found] = NULL;
516 return found;
517}
518#endif
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200519
520#ifdef CONFIG_ENV_IMPORT_FDT
521void env_import_fdt(void)
522{
523 const char *path;
524 struct ofprop prop;
525 ofnode node;
526 int res;
527
528 path = env_get("env_fdt_path");
529 if (!path || !path[0])
530 return;
531
532 node = ofnode_path(path);
533 if (!ofnode_valid(node)) {
534 printf("Warning: device tree node '%s' not found\n", path);
535 return;
536 }
537
Simon Glassfec058d2022-09-06 20:27:13 -0600538 for (res = ofnode_first_property(node, &prop);
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200539 !res;
Simon Glassfec058d2022-09-06 20:27:13 -0600540 res = ofnode_next_property(&prop)) {
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200541 const char *name, *val;
542
Simon Glassd0aff8b2022-09-06 20:27:14 -0600543 val = ofprop_get_property(&prop, &name, NULL);
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200544 env_set(name, val);
545 }
546}
547#endif