Prepare v2023.10
[platform/kernel/u-boot.git] / env / common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2010
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Andreas Heppel <aheppel@sysgo.de>
8  */
9
10 #include <common.h>
11 #include <bootstage.h>
12 #include <command.h>
13 #include <env.h>
14 #include <env_internal.h>
15 #include <log.h>
16 #include <sort.h>
17 #include <asm/global_data.h>
18 #include <linux/stddef.h>
19 #include <search.h>
20 #include <errno.h>
21 #include <malloc.h>
22 #include <u-boot/crc.h>
23 #include <dm/ofnode.h>
24 #include <net.h>
25 #include <watchdog.h>
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 /************************************************************************
30  * Default settings to be used when no valid environment is found
31  */
32 #include <env_default.h>
33
34 struct hsearch_data env_htab = {
35         .change_ok = env_flags_validate,
36 };
37
38 /*
39  * 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  */
52 int 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  */
67 int 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
75 ulong 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
90 int 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
96 int 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  */
113 char *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                 schedule();
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  */
139 char *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 static int env_get_from_linear(const char *env, const char *name, char *buf,
152                                unsigned len)
153 {
154         const char *p, *end;
155         size_t name_len;
156
157         if (name == NULL || *name == '\0')
158                 return -1;
159
160         name_len = strlen(name);
161
162         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
189 /*
190  * Look up variable from environment for restricted C runtime env.
191  */
192 int 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
204 /**
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
211  * Return: the decoded value, or default_val if not found
212  */
213 ulong 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 /*
225  * Read an environment variable as a boolean
226  * Return -1 if variable does not exist (default to true)
227  */
228 int env_get_yesno(const char *var)
229 {
230         char *s = env_get(var);
231
232         if (s == NULL)
233                 return -1;
234         return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
235                 1 : 0;
236 }
237
238 bool env_get_autostart(void)
239 {
240         return env_get_yesno("autostart") == 1;
241 }
242
243 /*
244  * Look up the variable from the default environment
245  */
246 char *env_get_default(const char *name)
247 {
248         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);
252
253         return NULL;
254 }
255
256 void env_set_default(const char *s, int flags)
257 {
258         if (s) {
259                 if ((flags & H_INTERACTIVE) == 0) {
260                         printf("*** Warning - %s, "
261                                 "using default environment\n\n", s);
262                 } else {
263                         puts(s);
264                 }
265         } else {
266                 debug("Using default environment\n");
267         }
268
269         flags |= H_DEFAULT;
270         if (himport_r(&env_htab, default_environment,
271                         sizeof(default_environment), '\0', flags, 0,
272                         0, NULL) == 0) {
273                 pr_err("## Error: Environment import failed: errno = %d\n",
274                        errno);
275                 return;
276         }
277
278         gd->flags |= GD_FLG_ENV_READY;
279         gd->flags |= GD_FLG_ENV_DEFAULT;
280 }
281
282
283 /* [re]set individual variables to their value in the default environment */
284 int env_set_default_vars(int nvars, char * const vars[], int flags)
285 {
286         /*
287          * Special use-case: import from default environment
288          * (and use \0 as a separator)
289          */
290         flags |= H_NOCLEAR | H_DEFAULT;
291         return himport_r(&env_htab, default_environment,
292                                 sizeof(default_environment), '\0',
293                                 flags, 0, nvars, vars);
294 }
295
296 /*
297  * Check if CRC is valid and (if yes) import the environment.
298  * Note that "buf" may or may not be aligned.
299  */
300 int env_import(const char *buf, int check, int flags)
301 {
302         env_t *ep = (env_t *)buf;
303
304         if (check) {
305                 uint32_t crc;
306
307                 memcpy(&crc, &ep->crc, sizeof(crc));
308
309                 if (crc32(0, ep->data, ENV_SIZE) != crc) {
310                         env_set_default("bad CRC", 0);
311                         return -ENOMSG; /* needed for env_load() */
312                 }
313         }
314
315         if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0,
316                         0, NULL)) {
317                 gd->flags |= GD_FLG_ENV_READY;
318                 return 0;
319         }
320
321         pr_err("Cannot import environment: errno = %d\n", errno);
322
323         env_set_default("import failed", 0);
324
325         return -EIO;
326 }
327
328 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
329 static unsigned char env_flags;
330
331 int env_check_redund(const char *buf1, int buf1_read_fail,
332                      const char *buf2, int buf2_read_fail)
333 {
334         int crc1_ok = 0, crc2_ok = 0;
335         env_t *tmp_env1, *tmp_env2;
336
337         tmp_env1 = (env_t *)buf1;
338         tmp_env2 = (env_t *)buf2;
339
340         if (buf1_read_fail && buf2_read_fail) {
341                 puts("*** Error - No Valid Environment Area found\n");
342                 return -EIO;
343         } else if (buf1_read_fail || buf2_read_fail) {
344                 puts("*** Warning - some problems detected ");
345                 puts("reading environment; recovered successfully\n");
346         }
347
348         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;
354
355         if (!crc1_ok && !crc2_ok) {
356                 gd->env_valid = ENV_INVALID;
357                 return -ENOMSG; /* needed for env_load() */
358         } else if (crc1_ok && !crc2_ok) {
359                 gd->env_valid = ENV_VALID;
360         } else if (!crc1_ok && crc2_ok) {
361                 gd->env_valid = ENV_REDUND;
362         } else {
363                 /* both ok - check serial */
364                 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
365                         gd->env_valid = ENV_REDUND;
366                 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
367                         gd->env_valid = ENV_VALID;
368                 else if (tmp_env1->flags > tmp_env2->flags)
369                         gd->env_valid = ENV_VALID;
370                 else if (tmp_env2->flags > tmp_env1->flags)
371                         gd->env_valid = ENV_REDUND;
372                 else /* flags are equal - almost impossible */
373                         gd->env_valid = ENV_VALID;
374         }
375
376         return 0;
377 }
378
379 int 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;
391         } else if (ret == -ENOMSG) {
392                 env_set_default("bad CRC", 0);
393                 return -ENOMSG;
394         }
395
396         if (gd->env_valid == ENV_VALID)
397                 ep = (env_t *)buf1;
398         else
399                 ep = (env_t *)buf2;
400
401         env_flags = ep->flags;
402
403         return env_import((char *)ep, 0, flags);
404 }
405 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
406
407 /* Export the environment and generate CRC for it. */
408 int 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) {
416                 pr_err("Cannot export environment: errno = %d\n", errno);
417                 return 1;
418         }
419
420         env_out->crc = crc32(0, env_out->data, ENV_SIZE);
421
422 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
423         env_out->flags = ++env_flags; /* increase the serial */
424 #endif
425
426         return 0;
427 }
428
429 void env_relocate(void)
430 {
431 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
432         env_reloc();
433         env_fix_drivers();
434         env_htab.change_ok += gd->reloc_off;
435 #endif
436         if (gd->env_valid == ENV_INVALID) {
437 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
438                 /* Environment not changable */
439                 env_set_default(NULL, 0);
440 #else
441                 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
442                 env_set_default("bad CRC", 0);
443 #endif
444         } else {
445                 env_load();
446         }
447 }
448
449 #ifdef CONFIG_AUTO_COMPLETE
450 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
451                  bool dollar_comp)
452 {
453         struct env_entry *match;
454         int found, idx;
455
456         if (dollar_comp) {
457                 /*
458                  * When doing $ completion, the first character should
459                  * obviously be a '$'.
460                  */
461                 if (var[0] != '$')
462                         return 0;
463
464                 var++;
465
466                 /*
467                  * The second one, if present, should be a '{', as some
468                  * configuration of the u-boot shell expand ${var} but not
469                  * $var.
470                  */
471                 if (var[0] == '{')
472                         var++;
473                 else if (var[0] != '\0')
474                         return 0;
475         }
476
477         idx = 0;
478         found = 0;
479         cmdv[0] = NULL;
480
481
482         while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
483                 int vallen = strlen(match->key) + 1;
484
485                 if (found >= maxv - 2 ||
486                     bufsz < vallen + (dollar_comp ? 3 : 0))
487                         break;
488
489                 cmdv[found++] = buf;
490
491                 /* Add the '${' prefix to each var when doing $ completion. */
492                 if (dollar_comp) {
493                         strcpy(buf, "${");
494                         buf += 2;
495                         bufsz -= 3;
496                 }
497
498                 memcpy(buf, match->key, vallen);
499                 buf += vallen;
500                 bufsz -= vallen;
501
502                 if (dollar_comp) {
503                         /*
504                          * This one is a bit odd: vallen already contains the
505                          * '\0' character but we need to add the '}' suffix,
506                          * hence the buf - 1 here. strcpy() will add the '\0'
507                          * character just after '}'. buf is then incremented
508                          * to account for the extra '}' we just added.
509                          */
510                         strcpy(buf - 1, "}");
511                         buf++;
512                 }
513         }
514
515         qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
516
517         if (idx)
518                 cmdv[found++] = dollar_comp ? "${...}" : "...";
519
520         cmdv[found] = NULL;
521         return found;
522 }
523 #endif
524
525 #ifdef CONFIG_ENV_IMPORT_FDT
526 void env_import_fdt(void)
527 {
528         const char *path;
529         struct ofprop prop;
530         ofnode node;
531         int res;
532
533         path = env_get("env_fdt_path");
534         if (!path || !path[0])
535                 return;
536
537         node = ofnode_path(path);
538         if (!ofnode_valid(node)) {
539                 printf("Warning: device tree node '%s' not found\n", path);
540                 return;
541         }
542
543         for (res = ofnode_first_property(node, &prop);
544              !res;
545              res = ofnode_next_property(&prop)) {
546                 const char *name, *val;
547
548                 val = ofprop_get_property(&prop, &name, NULL);
549                 env_set(name, val);
550         }
551 }
552 #endif