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