1 /*
2  * (C) Copyright 2000-2010
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2008
6  * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
7  *
8  * (C) Copyright 2004
9  * Jian Zhang, Texas Instruments, jzhang@ti.com.
10  *
11  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12  * Andreas Heppel <aheppel@sysgo.de>
13  *
14  * SPDX-License-Identifier:	GPL-2.0+
15  */
16 
17 #include <common.h>
18 #include <command.h>
19 #include <environment.h>
20 #include <linux/stddef.h>
21 #include <malloc.h>
22 #include <nand.h>
23 #include <search.h>
24 #include <errno.h>
25 
26 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
27 #define CMD_SAVEENV
28 #elif defined(CONFIG_ENV_OFFSET_REDUND)
29 #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
30 #endif
31 
32 #if defined(CONFIG_ENV_SIZE_REDUND) &&	\
33 	(CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
34 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
35 #endif
36 
37 #ifndef CONFIG_ENV_RANGE
38 #define CONFIG_ENV_RANGE	CONFIG_ENV_SIZE
39 #endif
40 
41 char *env_name_spec = "NAND";
42 
43 #if defined(ENV_IS_EMBEDDED)
44 env_t *env_ptr = &environment;
45 #elif defined(CONFIG_NAND_ENV_DST)
46 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
47 #else /* ! ENV_IS_EMBEDDED */
48 env_t *env_ptr;
49 #endif /* ENV_IS_EMBEDDED */
50 
51 DECLARE_GLOBAL_DATA_PTR;
52 
53 /*
54  * This is called before nand_init() so we can't read NAND to
55  * validate env data.
56  *
57  * Mark it OK for now. env_relocate() in env_common.c will call our
58  * relocate function which does the real validation.
59  *
60  * When using a NAND boot image (like sequoia_nand), the environment
61  * can be embedded or attached to the U-Boot image in NAND flash.
62  * This way the SPL loads not only the U-Boot image from NAND but
63  * also the environment.
64  */
env_init(void)65 int env_init(void)
66 {
67 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
68 	int crc1_ok = 0, crc2_ok = 0;
69 	env_t *tmp_env1;
70 
71 #ifdef CONFIG_ENV_OFFSET_REDUND
72 	env_t *tmp_env2;
73 
74 	tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
75 	crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
76 #endif
77 	tmp_env1 = env_ptr;
78 	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
79 
80 	if (!crc1_ok && !crc2_ok) {
81 		gd->env_addr	= 0;
82 		gd->env_valid	= 0;
83 
84 		return 0;
85 	} else if (crc1_ok && !crc2_ok) {
86 		gd->env_valid = 1;
87 	}
88 #ifdef CONFIG_ENV_OFFSET_REDUND
89 	else if (!crc1_ok && crc2_ok) {
90 		gd->env_valid = 2;
91 	} else {
92 		/* both ok - check serial */
93 		if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
94 			gd->env_valid = 2;
95 		else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
96 			gd->env_valid = 1;
97 		else if (tmp_env1->flags > tmp_env2->flags)
98 			gd->env_valid = 1;
99 		else if (tmp_env2->flags > tmp_env1->flags)
100 			gd->env_valid = 2;
101 		else /* flags are equal - almost impossible */
102 			gd->env_valid = 1;
103 	}
104 
105 	if (gd->env_valid == 2)
106 		env_ptr = tmp_env2;
107 	else
108 #endif
109 	if (gd->env_valid == 1)
110 		env_ptr = tmp_env1;
111 
112 	gd->env_addr = (ulong)env_ptr->data;
113 
114 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
115 	gd->env_addr	= (ulong)&default_environment[0];
116 	gd->env_valid	= 1;
117 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
118 
119 	return 0;
120 }
121 
122 #ifdef CMD_SAVEENV
123 /*
124  * The legacy NAND code saved the environment in the first NAND device i.e.,
125  * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
126  */
writeenv(size_t offset,u_char * buf)127 static int writeenv(size_t offset, u_char *buf)
128 {
129 	size_t end = offset + CONFIG_ENV_RANGE;
130 	size_t amount_saved = 0;
131 	size_t blocksize, len;
132 	u_char *char_ptr;
133 
134 	blocksize = nand_info[0].erasesize;
135 	len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
136 
137 	while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
138 		if (nand_block_isbad(&nand_info[0], offset)) {
139 			offset += blocksize;
140 		} else {
141 			char_ptr = &buf[amount_saved];
142 			if (nand_write(&nand_info[0], offset, &len, char_ptr))
143 				return 1;
144 
145 			offset += blocksize;
146 			amount_saved += len;
147 		}
148 	}
149 	if (amount_saved != CONFIG_ENV_SIZE)
150 		return 1;
151 
152 	return 0;
153 }
154 
155 struct env_location {
156 	const char *name;
157 	const nand_erase_options_t erase_opts;
158 };
159 
erase_and_write_env(const struct env_location * location,u_char * env_new)160 static int erase_and_write_env(const struct env_location *location,
161 		u_char *env_new)
162 {
163 	int ret = 0;
164 
165 	printf("Erasing %s...\n", location->name);
166 	if (nand_erase_opts(&nand_info[0], &location->erase_opts))
167 		return 1;
168 
169 	printf("Writing to %s... ", location->name);
170 	ret = writeenv(location->erase_opts.offset, env_new);
171 	puts(ret ? "FAILED!\n" : "OK\n");
172 
173 	return ret;
174 }
175 
176 #ifdef CONFIG_ENV_OFFSET_REDUND
177 static unsigned char env_flags;
178 #endif
179 
saveenv(void)180 int saveenv(void)
181 {
182 	int	ret = 0;
183 	ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
184 	int	env_idx = 0;
185 	static const struct env_location location[] = {
186 		{
187 			.name = "NAND",
188 			.erase_opts = {
189 				.length = CONFIG_ENV_RANGE,
190 				.offset = CONFIG_ENV_OFFSET,
191 			},
192 		},
193 #ifdef CONFIG_ENV_OFFSET_REDUND
194 		{
195 			.name = "redundant NAND",
196 			.erase_opts = {
197 				.length = CONFIG_ENV_RANGE,
198 				.offset = CONFIG_ENV_OFFSET_REDUND,
199 			},
200 		},
201 #endif
202 	};
203 
204 
205 	if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
206 		return 1;
207 
208 	ret = env_export(env_new);
209 	if (ret)
210 		return ret;
211 
212 #ifdef CONFIG_ENV_OFFSET_REDUND
213 	env_new->flags = ++env_flags; /* increase the serial */
214 	env_idx = (gd->env_valid == 1);
215 #endif
216 
217 	ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
218 #ifdef CONFIG_ENV_OFFSET_REDUND
219 	if (!ret) {
220 		/* preset other copy for next write */
221 		gd->env_valid = gd->env_valid == 2 ? 1 : 2;
222 		return ret;
223 	}
224 
225 	env_idx = (env_idx + 1) & 1;
226 	ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
227 	if (!ret)
228 		printf("Warning: primary env write failed,"
229 				" redundancy is lost!\n");
230 #endif
231 
232 	return ret;
233 }
234 #endif /* CMD_SAVEENV */
235 
236 #if defined(CONFIG_SPL_BUILD)
readenv(size_t offset,u_char * buf)237 static int readenv(size_t offset, u_char *buf)
238 {
239 	return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
240 }
241 #else
readenv(size_t offset,u_char * buf)242 static int readenv(size_t offset, u_char *buf)
243 {
244 	size_t end = offset + CONFIG_ENV_RANGE;
245 	size_t amount_loaded = 0;
246 	size_t blocksize, len;
247 	u_char *char_ptr;
248 
249 	blocksize = nand_info[0].erasesize;
250 	if (!blocksize)
251 		return 1;
252 
253 	len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
254 
255 	while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
256 		if (nand_block_isbad(&nand_info[0], offset)) {
257 			offset += blocksize;
258 		} else {
259 			char_ptr = &buf[amount_loaded];
260 			if (nand_read_skip_bad(&nand_info[0], offset,
261 					       &len, NULL,
262 					       nand_info[0].size, char_ptr))
263 				return 1;
264 
265 			offset += blocksize;
266 			amount_loaded += len;
267 		}
268 	}
269 
270 	if (amount_loaded != CONFIG_ENV_SIZE)
271 		return 1;
272 
273 	return 0;
274 }
275 #endif /* #if defined(CONFIG_SPL_BUILD) */
276 
277 #ifdef CONFIG_ENV_OFFSET_OOB
get_nand_env_oob(nand_info_t * nand,unsigned long * result)278 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
279 {
280 	struct mtd_oob_ops ops;
281 	uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
282 	int ret;
283 
284 	ops.datbuf	= NULL;
285 	ops.mode	= MTD_OOB_AUTO;
286 	ops.ooboffs	= 0;
287 	ops.ooblen	= ENV_OFFSET_SIZE;
288 	ops.oobbuf	= (void *)oob_buf;
289 
290 	ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
291 	if (ret) {
292 		printf("error reading OOB block 0\n");
293 		return ret;
294 	}
295 
296 	if (oob_buf[0] == ENV_OOB_MARKER) {
297 		*result = oob_buf[1] * nand->erasesize;
298 	} else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
299 		*result = oob_buf[1];
300 	} else {
301 		printf("No dynamic environment marker in OOB block 0\n");
302 		return -ENOENT;
303 	}
304 
305 	return 0;
306 }
307 #endif
308 
309 #ifdef CONFIG_ENV_OFFSET_REDUND
env_relocate_spec(void)310 void env_relocate_spec(void)
311 {
312 #if !defined(ENV_IS_EMBEDDED)
313 	int read1_fail = 0, read2_fail = 0;
314 	int crc1_ok = 0, crc2_ok = 0;
315 	env_t *ep, *tmp_env1, *tmp_env2;
316 
317 	tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
318 	tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
319 	if (tmp_env1 == NULL || tmp_env2 == NULL) {
320 		puts("Can't allocate buffers for environment\n");
321 		set_default_env("!malloc() failed");
322 		goto done;
323 	}
324 
325 	read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
326 	read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
327 
328 	if (read1_fail && read2_fail)
329 		puts("*** Error - No Valid Environment Area found\n");
330 	else if (read1_fail || read2_fail)
331 		puts("*** Warning - some problems detected "
332 		     "reading environment; recovered successfully\n");
333 
334 	crc1_ok = !read1_fail &&
335 		(crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
336 	crc2_ok = !read2_fail &&
337 		(crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
338 
339 	if (!crc1_ok && !crc2_ok) {
340 		set_default_env("!bad CRC");
341 		goto done;
342 	} else if (crc1_ok && !crc2_ok) {
343 		gd->env_valid = 1;
344 	} else if (!crc1_ok && crc2_ok) {
345 		gd->env_valid = 2;
346 	} else {
347 		/* both ok - check serial */
348 		if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
349 			gd->env_valid = 2;
350 		else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
351 			gd->env_valid = 1;
352 		else if (tmp_env1->flags > tmp_env2->flags)
353 			gd->env_valid = 1;
354 		else if (tmp_env2->flags > tmp_env1->flags)
355 			gd->env_valid = 2;
356 		else /* flags are equal - almost impossible */
357 			gd->env_valid = 1;
358 	}
359 
360 	free(env_ptr);
361 
362 	if (gd->env_valid == 1)
363 		ep = tmp_env1;
364 	else
365 		ep = tmp_env2;
366 
367 	env_flags = ep->flags;
368 	env_import((char *)ep, 0);
369 
370 done:
371 	free(tmp_env1);
372 	free(tmp_env2);
373 
374 #endif /* ! ENV_IS_EMBEDDED */
375 }
376 #else /* ! CONFIG_ENV_OFFSET_REDUND */
377 /*
378  * The legacy NAND code saved the environment in the first NAND
379  * device i.e., nand_dev_desc + 0. This is also the behaviour using
380  * the new NAND code.
381  */
env_relocate_spec(void)382 void env_relocate_spec(void)
383 {
384 #if !defined(ENV_IS_EMBEDDED)
385 	int ret;
386 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
387 
388 #if defined(CONFIG_ENV_OFFSET_OOB)
389 	ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
390 	/*
391 	 * If unable to read environment offset from NAND OOB then fall through
392 	 * to the normal environment reading code below
393 	 */
394 	if (!ret) {
395 		printf("Found Environment offset in OOB..\n");
396 	} else {
397 		set_default_env("!no env offset in OOB");
398 		return;
399 	}
400 #endif
401 
402 	ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
403 	if (ret) {
404 		set_default_env("!readenv() failed");
405 		return;
406 	}
407 
408 	env_import(buf, 1);
409 #endif /* ! ENV_IS_EMBEDDED */
410 }
411 #endif /* CONFIG_ENV_OFFSET_REDUND */
412