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