blob: 99729ca002c2d8696a22b30a060d938629b1b440 [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
50 * @return 0 if ok, 1 on error
51 */
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
65 * @return 0 if ok, 1 on error
66 */
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
118 WATCHDOG_RESET();
119
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 */
128 if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
129 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
151/*
152 * Look up variable from environment for restricted C runtime env.
153 */
154int env_get_f(const char *name, char *buf, unsigned len)
155{
156 const char *env, *p, *end;
157 size_t name_len;
158
159 if (name == NULL || *name == '\0')
160 return -1;
161
162 name_len = strlen(name);
163
164 if (gd->env_valid == ENV_INVALID)
Marek BehĂșn73d25342021-10-22 15:47:24 +0200165 env = default_environment;
Marek BehĂșn30044c52021-10-17 17:36:38 +0200166 else
167 env = (const char *)gd->env_addr;
168
169 for (p = env; *p != '\0'; p = end + 1) {
170 const char *value;
171 unsigned res;
172
173 for (end = p; *end != '\0'; ++end)
174 if (end - env >= CONFIG_ENV_SIZE)
175 return -1;
176
177 if (strncmp(name, p, name_len) || p[name_len] != '=')
178 continue;
179 value = &p[name_len + 1];
180
181 res = end - value;
182 memcpy(buf, value, min(len, res + 1));
183
184 if (len <= res) {
185 buf[len - 1] = '\0';
186 printf("env_buf [%u bytes] too small for value of \"%s\"\n",
187 len, name);
188 }
189
190 return res;
191 }
192
193 return -1;
194}
195
196/**
197 * Decode the integer value of an environment variable and return it.
198 *
199 * @param name Name of environment variable
200 * @param base Number base to use (normally 10, or 16 for hex)
201 * @param default_val Default value to return if the variable is not
202 * found
203 * @return the decoded value, or default_val if not found
204 */
205ulong env_get_ulong(const char *name, int base, ulong default_val)
206{
207 /*
208 * We can use env_get() here, even before relocation, since the
209 * environment variable value is an integer and thus short.
210 */
211 const char *str = env_get(name);
212
213 return str ? simple_strtoul(str, NULL, base) : default_val;
214}
215
216/*
Joe Hershberger864ec562012-12-11 22:16:22 -0600217 * Read an environment variable as a boolean
218 * Return -1 if variable does not exist (default to true)
219 */
Simon Glass22c34c22017-08-03 12:22:13 -0600220int env_get_yesno(const char *var)
Joe Hershberger864ec562012-12-11 22:16:22 -0600221{
Simon Glass64b723f2017-08-03 12:22:12 -0600222 char *s = env_get(var);
Joe Hershberger864ec562012-12-11 22:16:22 -0600223
224 if (s == NULL)
225 return -1;
226 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
227 1 : 0;
228}
229
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600230/*
231 * Look up the variable from the default environment
232 */
Simon Glassda1a1342017-08-03 12:22:15 -0600233char *env_get_default(const char *name)
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600234{
235 char *ret_val;
236 unsigned long really_valid = gd->env_valid;
237 unsigned long real_gd_flags = gd->flags;
238
239 /* Pretend that the image is bad. */
240 gd->flags &= ~GD_FLG_ENV_READY;
Simon Glass1f39d0b2017-08-20 04:45:15 -0600241 gd->env_valid = ENV_INVALID;
Simon Glass64b723f2017-08-03 12:22:12 -0600242 ret_val = env_get(name);
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600243 gd->env_valid = really_valid;
244 gd->flags = real_gd_flags;
245 return ret_val;
246}
247
Simon Glass97385862019-08-01 09:47:00 -0600248void env_set_default(const char *s, int flags)
Harald Welte72eb50a2008-07-07 15:40:39 +0800249{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200250 if (s) {
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300251 if ((flags & H_INTERACTIVE) == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200252 printf("*** Warning - %s, "
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300253 "using default environment\n\n", s);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200254 } else {
255 puts(s);
256 }
257 } else {
Maxime Ripard12e6eac2018-01-23 21:16:57 +0100258 debug("Using default environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200259 }
260
Marek Vasutab6b15a2020-07-07 20:51:34 +0200261 flags |= H_DEFAULT;
Marek BehĂșn73d25342021-10-22 15:47:24 +0200262 if (himport_r(&env_htab, default_environment,
Alexander Holler3e12be72014-07-14 17:49:55 +0200263 sizeof(default_environment), '\0', flags, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600264 0, NULL) == 0)
Quentin Schulz4e9d34cf2018-07-09 19:16:25 +0200265 pr_err("## Error: Environment import failed: errno = %d\n",
266 errno);
Igor Grinberg3e773592011-11-07 01:14:11 +0000267
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200268 gd->flags |= GD_FLG_ENV_READY;
Michal Simek32bb7c82016-05-30 16:06:54 +0200269 gd->flags |= GD_FLG_ENV_DEFAULT;
Harald Welte72eb50a2008-07-07 15:40:39 +0800270}
271
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000272
273/* [re]set individual variables to their value in the default environment */
Simon Glasseec97962019-08-01 09:46:56 -0600274int env_set_default_vars(int nvars, char * const vars[], int flags)
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000275{
276 /*
277 * Special use-case: import from default environment
278 * (and use \0 as a separator)
279 */
Marek Vasutab6b15a2020-07-07 20:51:34 +0200280 flags |= H_NOCLEAR | H_DEFAULT;
Marek BehĂșn73d25342021-10-22 15:47:24 +0200281 return himport_r(&env_htab, default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600282 sizeof(default_environment), '\0',
Yaniv Levinsky23840d12018-06-24 19:16:55 +0300283 flags, 0, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000284}
285
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200286/*
287 * Check if CRC is valid and (if yes) import the environment.
288 * Note that "buf" may or may not be aligned.
289 */
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200290int env_import(const char *buf, int check, int flags)
wdenk05706fd2002-09-28 17:48:27 +0000291{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200292 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000293
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200294 if (check) {
295 uint32_t crc;
296
297 memcpy(&crc, &ep->crc, sizeof(crc));
298
299 if (crc32(0, ep->data, ENV_SIZE) != crc) {
Simon Glass97385862019-08-01 09:47:00 -0600300 env_set_default("bad CRC", 0);
Sam Protsenkoc3097882019-01-18 21:19:03 +0200301 return -ENOMSG; /* needed for env_load() */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200302 }
303 }
wdenk05706fd2002-09-28 17:48:27 +0000304
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200305 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600306 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200307 gd->flags |= GD_FLG_ENV_READY;
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100308 return 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200309 }
310
Masahiro Yamada81e10422017-09-16 14:10:41 +0900311 pr_err("Cannot import environment: errno = %d\n", errno);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200312
Simon Glass97385862019-08-01 09:47:00 -0600313 env_set_default("import failed", 0);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200314
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100315 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200316}
317
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000318#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
319static unsigned char env_flags;
320
Heiko Schocher30651132020-10-10 10:28:04 +0200321int env_check_redund(const char *buf1, int buf1_read_fail,
322 const char *buf2, int buf2_read_fail)
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000323{
Brandon Maier61374b02020-12-17 17:19:18 -0600324 int crc1_ok = 0, crc2_ok = 0;
Heiko Schocher30651132020-10-10 10:28:04 +0200325 env_t *tmp_env1, *tmp_env2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000326
327 tmp_env1 = (env_t *)buf1;
328 tmp_env2 = (env_t *)buf2;
329
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100330 if (buf1_read_fail && buf2_read_fail) {
331 puts("*** Error - No Valid Environment Area found\n");
Brandon Maier61374b02020-12-17 17:19:18 -0600332 return -EIO;
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100333 } else if (buf1_read_fail || buf2_read_fail) {
334 puts("*** Warning - some problems detected ");
335 puts("reading environment; recovered successfully\n");
336 }
337
Brandon Maier61374b02020-12-17 17:19:18 -0600338 if (!buf1_read_fail)
339 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
340 tmp_env1->crc;
341 if (!buf2_read_fail)
342 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
343 tmp_env2->crc;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000344
345 if (!crc1_ok && !crc2_ok) {
Sam Protsenkoc3097882019-01-18 21:19:03 +0200346 return -ENOMSG; /* needed for env_load() */
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000347 } else if (crc1_ok && !crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600348 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000349 } else if (!crc1_ok && crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600350 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000351 } else {
352 /* both ok - check serial */
353 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600354 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000355 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600356 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000357 else if (tmp_env1->flags > tmp_env2->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600358 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000359 else if (tmp_env2->flags > tmp_env1->flags)
Simon Glass1f39d0b2017-08-20 04:45:15 -0600360 gd->env_valid = ENV_REDUND;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000361 else /* flags are equal - almost impossible */
Simon Glass1f39d0b2017-08-20 04:45:15 -0600362 gd->env_valid = ENV_VALID;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000363 }
364
Heiko Schocher30651132020-10-10 10:28:04 +0200365 return 0;
366}
367
368int env_import_redund(const char *buf1, int buf1_read_fail,
369 const char *buf2, int buf2_read_fail,
370 int flags)
371{
372 env_t *ep;
373 int ret;
374
375 ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
376
377 if (ret == -EIO) {
378 env_set_default("bad env area", 0);
379 return -EIO;
Heiko Schocher30651132020-10-10 10:28:04 +0200380 } else if (ret == -ENOMSG) {
381 env_set_default("bad CRC", 0);
382 return -ENOMSG;
383 }
384
Simon Glass1f39d0b2017-08-20 04:45:15 -0600385 if (gd->env_valid == ENV_VALID)
Heiko Schocher30651132020-10-10 10:28:04 +0200386 ep = (env_t *)buf1;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000387 else
Heiko Schocher30651132020-10-10 10:28:04 +0200388 ep = (env_t *)buf2;
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000389
390 env_flags = ep->flags;
Heiko Schocher30651132020-10-10 10:28:04 +0200391
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200392 return env_import((char *)ep, 0, flags);
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000393}
394#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
395
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400396/* Export the environment and generate CRC for it. */
Marek Vasutd73c1292014-03-05 19:59:50 +0100397int env_export(env_t *env_out)
398{
399 char *res;
400 ssize_t len;
401
402 res = (char *)env_out->data;
403 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
404 if (len < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900405 pr_err("Cannot export environment: errno = %d\n", errno);
Marek Vasutd73c1292014-03-05 19:59:50 +0100406 return 1;
407 }
Marek Vasut486112d2014-03-05 19:59:51 +0100408
Marek Vasutd73c1292014-03-05 19:59:50 +0100409 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
410
Fiach Antaw57ea7f72017-01-25 18:53:11 +1000411#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
412 env_out->flags = ++env_flags; /* increase the serial */
413#endif
414
Marek Vasutd73c1292014-03-05 19:59:50 +0100415 return 0;
416}
417
Igor Grinberg3e773592011-11-07 01:14:11 +0000418void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200419{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200420#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200421 env_reloc();
Siva Durga Prasad Paladugu75c65792018-04-13 07:57:21 +0200422 env_fix_drivers();
Tom Rini9ad1d3b2019-10-04 12:21:33 -0400423 env_htab.change_ok += gd->reloc_off;
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200424#endif
Simon Glass1f39d0b2017-08-20 04:45:15 -0600425 if (gd->env_valid == ENV_INVALID) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000426#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
427 /* Environment not changable */
Simon Glass97385862019-08-01 09:47:00 -0600428 env_set_default(NULL, 0);
wdenk05706fd2002-09-28 17:48:27 +0000429#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000430 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Simon Glass97385862019-08-01 09:47:00 -0600431 env_set_default("bad CRC", 0);
Lei Wene0909be2010-10-10 12:36:40 +0800432#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200433 } else {
Simon Glass17539572017-08-03 12:22:07 -0600434 env_load();
wdenk05706fd2002-09-28 17:48:27 +0000435 }
wdenk05706fd2002-09-28 17:48:27 +0000436}
wdenk3902d702004-04-15 18:22:41 +0000437
Boris Brezillon619fc682018-12-05 09:26:50 +0100438#ifdef CONFIG_AUTO_COMPLETE
439int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
440 bool dollar_comp)
wdenk3902d702004-04-15 18:22:41 +0000441{
Simon Glass1a236862019-08-02 09:44:18 -0600442 struct env_entry *match;
Mike Frysinger8d882322010-12-17 16:51:59 -0500443 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000444
Boris Brezillon619fc682018-12-05 09:26:50 +0100445 if (dollar_comp) {
446 /*
447 * When doing $ completion, the first character should
448 * obviously be a '$'.
449 */
450 if (var[0] != '$')
451 return 0;
452
453 var++;
454
455 /*
456 * The second one, if present, should be a '{', as some
457 * configuration of the u-boot shell expand ${var} but not
458 * $var.
459 */
460 if (var[0] == '{')
461 var++;
462 else if (var[0] != '\0')
463 return 0;
464 }
465
Mike Frysinger8d882322010-12-17 16:51:59 -0500466 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000467 found = 0;
468 cmdv[0] = NULL;
469
Boris Brezillon619fc682018-12-05 09:26:50 +0100470
Mike Frysinger8d882322010-12-17 16:51:59 -0500471 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
472 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000473
Boris Brezillon619fc682018-12-05 09:26:50 +0100474 if (found >= maxv - 2 ||
475 bufsz < vallen + (dollar_comp ? 3 : 0))
wdenk3902d702004-04-15 18:22:41 +0000476 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500477
wdenk3902d702004-04-15 18:22:41 +0000478 cmdv[found++] = buf;
Boris Brezillon619fc682018-12-05 09:26:50 +0100479
480 /* Add the '${' prefix to each var when doing $ completion. */
481 if (dollar_comp) {
482 strcpy(buf, "${");
483 buf += 2;
484 bufsz -= 3;
485 }
486
Mike Frysinger8d882322010-12-17 16:51:59 -0500487 memcpy(buf, match->key, vallen);
488 buf += vallen;
489 bufsz -= vallen;
Boris Brezillon619fc682018-12-05 09:26:50 +0100490
491 if (dollar_comp) {
492 /*
493 * This one is a bit odd: vallen already contains the
494 * '\0' character but we need to add the '}' suffix,
495 * hence the buf - 1 here. strcpy() will add the '\0'
496 * character just after '}'. buf is then incremented
497 * to account for the extra '}' we just added.
498 */
499 strcpy(buf - 1, "}");
500 buf++;
501 }
wdenk3902d702004-04-15 18:22:41 +0000502 }
503
Mike Frysinger8d882322010-12-17 16:51:59 -0500504 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
505
506 if (idx)
Boris Brezillon619fc682018-12-05 09:26:50 +0100507 cmdv[found++] = dollar_comp ? "${...}" : "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000508
wdenk3902d702004-04-15 18:22:41 +0000509 cmdv[found] = NULL;
510 return found;
511}
512#endif
Rasmus Villemoescf8e5432021-04-21 11:06:54 +0200513
514#ifdef CONFIG_ENV_IMPORT_FDT
515void env_import_fdt(void)
516{
517 const char *path;
518 struct ofprop prop;
519 ofnode node;
520 int res;
521
522 path = env_get("env_fdt_path");
523 if (!path || !path[0])
524 return;
525
526 node = ofnode_path(path);
527 if (!ofnode_valid(node)) {
528 printf("Warning: device tree node '%s' not found\n", path);
529 return;
530 }
531
532 for (res = ofnode_get_first_property(node, &prop);
533 !res;
534 res = ofnode_get_next_property(&prop)) {
535 const char *name, *val;
536
537 val = ofnode_get_property_by_prop(&prop, &name, NULL);
538 env_set(name, val);
539 }
540}
541#endif