Revert "env: solve compilation error in SPL"
[oweals/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 <command.h>
12 #include <env.h>
13 #include <env_internal.h>
14 #include <linux/stddef.h>
15 #include <search.h>
16 #include <errno.h>
17 #include <malloc.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /************************************************************************
22  * Default settings to be used when no valid environment is found
23  */
24 #include <env_default.h>
25
26 struct hsearch_data env_htab = {
27         .change_ok = env_flags_validate,
28 };
29
30 /*
31  * Read an environment variable as a boolean
32  * Return -1 if variable does not exist (default to true)
33  */
34 int env_get_yesno(const char *var)
35 {
36         char *s = env_get(var);
37
38         if (s == NULL)
39                 return -1;
40         return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
41                 1 : 0;
42 }
43
44 /*
45  * Look up the variable from the default environment
46  */
47 char *env_get_default(const char *name)
48 {
49         char *ret_val;
50         unsigned long really_valid = gd->env_valid;
51         unsigned long real_gd_flags = gd->flags;
52
53         /* Pretend that the image is bad. */
54         gd->flags &= ~GD_FLG_ENV_READY;
55         gd->env_valid = ENV_INVALID;
56         ret_val = env_get(name);
57         gd->env_valid = really_valid;
58         gd->flags = real_gd_flags;
59         return ret_val;
60 }
61
62 void env_set_default(const char *s, int flags)
63 {
64         if (sizeof(default_environment) > ENV_SIZE) {
65                 puts("*** Error - default environment is too large\n\n");
66                 return;
67         }
68
69         if (s) {
70                 if ((flags & H_INTERACTIVE) == 0) {
71                         printf("*** Warning - %s, "
72                                 "using default environment\n\n", s);
73                 } else {
74                         puts(s);
75                 }
76         } else {
77                 debug("Using default environment\n");
78         }
79
80         if (himport_r(&env_htab, (char *)default_environment,
81                         sizeof(default_environment), '\0', flags, 0,
82                         0, NULL) == 0)
83                 pr_err("## Error: Environment import failed: errno = %d\n",
84                        errno);
85
86         gd->flags |= GD_FLG_ENV_READY;
87         gd->flags |= GD_FLG_ENV_DEFAULT;
88 }
89
90
91 /* [re]set individual variables to their value in the default environment */
92 int env_set_default_vars(int nvars, char * const vars[], int flags)
93 {
94         /*
95          * Special use-case: import from default environment
96          * (and use \0 as a separator)
97          */
98         flags |= H_NOCLEAR;
99         return himport_r(&env_htab, (const char *)default_environment,
100                                 sizeof(default_environment), '\0',
101                                 flags, 0, nvars, vars);
102 }
103
104 /*
105  * Check if CRC is valid and (if yes) import the environment.
106  * Note that "buf" may or may not be aligned.
107  */
108 int env_import(const char *buf, int check)
109 {
110         env_t *ep = (env_t *)buf;
111
112         if (check) {
113                 uint32_t crc;
114
115                 memcpy(&crc, &ep->crc, sizeof(crc));
116
117                 if (crc32(0, ep->data, ENV_SIZE) != crc) {
118                         env_set_default("bad CRC", 0);
119                         return -ENOMSG; /* needed for env_load() */
120                 }
121         }
122
123         if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
124                         0, NULL)) {
125                 gd->flags |= GD_FLG_ENV_READY;
126                 return 0;
127         }
128
129         pr_err("Cannot import environment: errno = %d\n", errno);
130
131         env_set_default("import failed", 0);
132
133         return -EIO;
134 }
135
136 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
137 static unsigned char env_flags;
138
139 int env_import_redund(const char *buf1, int buf1_read_fail,
140                       const char *buf2, int buf2_read_fail)
141 {
142         int crc1_ok, crc2_ok;
143         env_t *ep, *tmp_env1, *tmp_env2;
144
145         tmp_env1 = (env_t *)buf1;
146         tmp_env2 = (env_t *)buf2;
147
148         if (buf1_read_fail && buf2_read_fail) {
149                 puts("*** Error - No Valid Environment Area found\n");
150         } else if (buf1_read_fail || buf2_read_fail) {
151                 puts("*** Warning - some problems detected ");
152                 puts("reading environment; recovered successfully\n");
153         }
154
155         if (buf1_read_fail && buf2_read_fail) {
156                 env_set_default("bad env area", 0);
157                 return -EIO;
158         } else if (!buf1_read_fail && buf2_read_fail) {
159                 gd->env_valid = ENV_VALID;
160                 return env_import((char *)tmp_env1, 1);
161         } else if (buf1_read_fail && !buf2_read_fail) {
162                 gd->env_valid = ENV_REDUND;
163                 return env_import((char *)tmp_env2, 1);
164         }
165
166         crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
167                         tmp_env1->crc;
168         crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
169                         tmp_env2->crc;
170
171         if (!crc1_ok && !crc2_ok) {
172                 env_set_default("bad CRC", 0);
173                 return -ENOMSG; /* needed for env_load() */
174         } else if (crc1_ok && !crc2_ok) {
175                 gd->env_valid = ENV_VALID;
176         } else if (!crc1_ok && crc2_ok) {
177                 gd->env_valid = ENV_REDUND;
178         } else {
179                 /* both ok - check serial */
180                 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
181                         gd->env_valid = ENV_REDUND;
182                 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
183                         gd->env_valid = ENV_VALID;
184                 else if (tmp_env1->flags > tmp_env2->flags)
185                         gd->env_valid = ENV_VALID;
186                 else if (tmp_env2->flags > tmp_env1->flags)
187                         gd->env_valid = ENV_REDUND;
188                 else /* flags are equal - almost impossible */
189                         gd->env_valid = ENV_VALID;
190         }
191
192         if (gd->env_valid == ENV_VALID)
193                 ep = tmp_env1;
194         else
195                 ep = tmp_env2;
196
197         env_flags = ep->flags;
198         return env_import((char *)ep, 0);
199 }
200 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
201
202 /* Export the environment and generate CRC for it. */
203 int env_export(env_t *env_out)
204 {
205         char *res;
206         ssize_t len;
207
208         res = (char *)env_out->data;
209         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
210         if (len < 0) {
211                 pr_err("Cannot export environment: errno = %d\n", errno);
212                 return 1;
213         }
214
215         env_out->crc = crc32(0, env_out->data, ENV_SIZE);
216
217 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
218         env_out->flags = ++env_flags; /* increase the serial */
219 #endif
220
221         return 0;
222 }
223
224 void env_relocate(void)
225 {
226 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
227         env_reloc();
228         env_fix_drivers();
229         env_htab.change_ok += gd->reloc_off;
230 #endif
231         if (gd->env_valid == ENV_INVALID) {
232 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
233                 /* Environment not changable */
234                 env_set_default(NULL, 0);
235 #else
236                 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
237                 env_set_default("bad CRC", 0);
238 #endif
239         } else {
240                 env_load();
241         }
242 }
243
244 #ifdef CONFIG_AUTO_COMPLETE
245 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
246                  bool dollar_comp)
247 {
248         struct env_entry *match;
249         int found, idx;
250
251         if (dollar_comp) {
252                 /*
253                  * When doing $ completion, the first character should
254                  * obviously be a '$'.
255                  */
256                 if (var[0] != '$')
257                         return 0;
258
259                 var++;
260
261                 /*
262                  * The second one, if present, should be a '{', as some
263                  * configuration of the u-boot shell expand ${var} but not
264                  * $var.
265                  */
266                 if (var[0] == '{')
267                         var++;
268                 else if (var[0] != '\0')
269                         return 0;
270         }
271
272         idx = 0;
273         found = 0;
274         cmdv[0] = NULL;
275
276
277         while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
278                 int vallen = strlen(match->key) + 1;
279
280                 if (found >= maxv - 2 ||
281                     bufsz < vallen + (dollar_comp ? 3 : 0))
282                         break;
283
284                 cmdv[found++] = buf;
285
286                 /* Add the '${' prefix to each var when doing $ completion. */
287                 if (dollar_comp) {
288                         strcpy(buf, "${");
289                         buf += 2;
290                         bufsz -= 3;
291                 }
292
293                 memcpy(buf, match->key, vallen);
294                 buf += vallen;
295                 bufsz -= vallen;
296
297                 if (dollar_comp) {
298                         /*
299                          * This one is a bit odd: vallen already contains the
300                          * '\0' character but we need to add the '}' suffix,
301                          * hence the buf - 1 here. strcpy() will add the '\0'
302                          * character just after '}'. buf is then incremented
303                          * to account for the extra '}' we just added.
304                          */
305                         strcpy(buf - 1, "}");
306                         buf++;
307                 }
308         }
309
310         qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
311
312         if (idx)
313                 cmdv[found++] = dollar_comp ? "${...}" : "...";
314
315         cmdv[found] = NULL;
316         return found;
317 }
318 #endif