xref: /openbmc/u-boot/env/nand.c (revision 0093b3fc)
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 <memalign.h>
23 #include <nand.h>
24 #include <search.h>
25 #include <errno.h>
26 
27 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) && \
28 		!defined(CONFIG_SPL_BUILD)
29 #define CMD_SAVEENV
30 #elif defined(CONFIG_ENV_OFFSET_REDUND)
31 #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
32 #endif
33 
34 #if defined(CONFIG_ENV_SIZE_REDUND) &&	\
35 	(CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
36 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
37 #endif
38 
39 #ifndef CONFIG_ENV_RANGE
40 #define CONFIG_ENV_RANGE	CONFIG_ENV_SIZE
41 #endif
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  */
65 static int env_nand_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	= ENV_INVALID;
83 
84 		return 0;
85 	} else if (crc1_ok && !crc2_ok) {
86 		gd->env_valid = ENV_VALID;
87 	}
88 #ifdef CONFIG_ENV_OFFSET_REDUND
89 	else if (!crc1_ok && crc2_ok) {
90 		gd->env_valid = ENV_REDUND;
91 	} else {
92 		/* both ok - check serial */
93 		if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
94 			gd->env_valid = ENV_REDUND;
95 		else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
96 			gd->env_valid = ENV_VALID;
97 		else if (tmp_env1->flags > tmp_env2->flags)
98 			gd->env_valid = ENV_VALID;
99 		else if (tmp_env2->flags > tmp_env1->flags)
100 			gd->env_valid = ENV_REDUND;
101 		else /* flags are equal - almost impossible */
102 			gd->env_valid = ENV_VALID;
103 	}
104 
105 	if (gd->env_valid == ENV_REDUND)
106 		env_ptr = tmp_env2;
107 	else
108 #endif
109 	if (gd->env_valid == ENV_VALID)
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	= ENV_VALID;
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  */
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 	struct mtd_info *mtd;
133 	u_char *char_ptr;
134 
135 	mtd = get_nand_dev_by_index(0);
136 	if (!mtd)
137 		return 1;
138 
139 	blocksize = mtd->erasesize;
140 	len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
141 
142 	while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
143 		if (nand_block_isbad(mtd, offset)) {
144 			offset += blocksize;
145 		} else {
146 			char_ptr = &buf[amount_saved];
147 			if (nand_write(mtd, offset, &len, char_ptr))
148 				return 1;
149 
150 			offset += blocksize;
151 			amount_saved += len;
152 		}
153 	}
154 	if (amount_saved != CONFIG_ENV_SIZE)
155 		return 1;
156 
157 	return 0;
158 }
159 
160 struct nand_env_location {
161 	const char *name;
162 	const nand_erase_options_t erase_opts;
163 };
164 
165 static int erase_and_write_env(const struct nand_env_location *location,
166 		u_char *env_new)
167 {
168 	struct mtd_info *mtd;
169 	int ret = 0;
170 
171 	mtd = get_nand_dev_by_index(0);
172 	if (!mtd)
173 		return 1;
174 
175 	printf("Erasing %s...\n", location->name);
176 	if (nand_erase_opts(mtd, &location->erase_opts))
177 		return 1;
178 
179 	printf("Writing to %s... ", location->name);
180 	ret = writeenv(location->erase_opts.offset, env_new);
181 	puts(ret ? "FAILED!\n" : "OK\n");
182 
183 	return ret;
184 }
185 
186 static int env_nand_save(void)
187 {
188 	int	ret = 0;
189 	ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
190 	int	env_idx = 0;
191 	static const struct nand_env_location location[] = {
192 		{
193 			.name = "NAND",
194 			.erase_opts = {
195 				.length = CONFIG_ENV_RANGE,
196 				.offset = CONFIG_ENV_OFFSET,
197 			},
198 		},
199 #ifdef CONFIG_ENV_OFFSET_REDUND
200 		{
201 			.name = "redundant NAND",
202 			.erase_opts = {
203 				.length = CONFIG_ENV_RANGE,
204 				.offset = CONFIG_ENV_OFFSET_REDUND,
205 			},
206 		},
207 #endif
208 	};
209 
210 
211 	if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
212 		return 1;
213 
214 	ret = env_export(env_new);
215 	if (ret)
216 		return ret;
217 
218 #ifdef CONFIG_ENV_OFFSET_REDUND
219 	env_idx = (gd->env_valid == ENV_VALID);
220 #endif
221 
222 	ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
223 #ifdef CONFIG_ENV_OFFSET_REDUND
224 	if (!ret) {
225 		/* preset other copy for next write */
226 		gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
227 				ENV_REDUND;
228 		return ret;
229 	}
230 
231 	env_idx = (env_idx + 1) & 1;
232 	ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
233 	if (!ret)
234 		printf("Warning: primary env write failed,"
235 				" redundancy is lost!\n");
236 #endif
237 
238 	return ret;
239 }
240 #endif /* CMD_SAVEENV */
241 
242 #if defined(CONFIG_SPL_BUILD)
243 static int readenv(size_t offset, u_char *buf)
244 {
245 	return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
246 }
247 #else
248 static int readenv(size_t offset, u_char *buf)
249 {
250 	size_t end = offset + CONFIG_ENV_RANGE;
251 	size_t amount_loaded = 0;
252 	size_t blocksize, len;
253 	struct mtd_info *mtd;
254 	u_char *char_ptr;
255 
256 	mtd = get_nand_dev_by_index(0);
257 	if (!mtd)
258 		return 1;
259 
260 	blocksize = mtd->erasesize;
261 	len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
262 
263 	while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
264 		if (nand_block_isbad(mtd, offset)) {
265 			offset += blocksize;
266 		} else {
267 			char_ptr = &buf[amount_loaded];
268 			if (nand_read_skip_bad(mtd, offset,
269 					       &len, NULL,
270 					       mtd->size, char_ptr))
271 				return 1;
272 
273 			offset += blocksize;
274 			amount_loaded += len;
275 		}
276 	}
277 
278 	if (amount_loaded != CONFIG_ENV_SIZE)
279 		return 1;
280 
281 	return 0;
282 }
283 #endif /* #if defined(CONFIG_SPL_BUILD) */
284 
285 #ifdef CONFIG_ENV_OFFSET_OOB
286 int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
287 {
288 	struct mtd_oob_ops ops;
289 	uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
290 	int ret;
291 
292 	ops.datbuf	= NULL;
293 	ops.mode	= MTD_OOB_AUTO;
294 	ops.ooboffs	= 0;
295 	ops.ooblen	= ENV_OFFSET_SIZE;
296 	ops.oobbuf	= (void *)oob_buf;
297 
298 	ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
299 	if (ret) {
300 		printf("error reading OOB block 0\n");
301 		return ret;
302 	}
303 
304 	if (oob_buf[0] == ENV_OOB_MARKER) {
305 		*result = ovoid ob_buf[1] * mtd->erasesize;
306 	} else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
307 		*result = oob_buf[1];
308 	} else {
309 		printf("No dynamic environment marker in OOB block 0\n");
310 		return -ENOENT;
311 	}
312 
313 	return 0;
314 }
315 #endif
316 
317 #ifdef CONFIG_ENV_OFFSET_REDUND
318 static int env_nand_load(void)
319 {
320 #if defined(ENV_IS_EMBEDDED)
321 	return 0;
322 #else
323 	int read1_fail, read2_fail;
324 	env_t *tmp_env1, *tmp_env2;
325 	int ret = 0;
326 
327 	tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
328 	tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
329 	if (tmp_env1 == NULL || tmp_env2 == NULL) {
330 		puts("Can't allocate buffers for environment\n");
331 		set_default_env("!malloc() failed");
332 		ret = -EIO;
333 		goto done;
334 	}
335 
336 	read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
337 	read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
338 
339 	ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
340 				read2_fail);
341 
342 done:
343 	free(tmp_env1);
344 	free(tmp_env2);
345 
346 	return ret;
347 #endif /* ! ENV_IS_EMBEDDED */
348 }
349 #else /* ! CONFIG_ENV_OFFSET_REDUND */
350 /*
351  * The legacy NAND code saved the environment in the first NAND
352  * device i.e., nand_dev_desc + 0. This is also the behaviour using
353  * the new NAND code.
354  */
355 static int env_nand_load(void)
356 {
357 #if !defined(ENV_IS_EMBEDDED)
358 	int ret;
359 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
360 
361 #if defined(CONFIG_ENV_OFFSET_OOB)
362 	struct mtd_info *mtd  = get_nand_dev_by_index(0);
363 	/*
364 	 * If unable to read environment offset from NAND OOB then fall through
365 	 * to the normal environment reading code below
366 	 */
367 	if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
368 		printf("Found Environment offset in OOB..\n");
369 	} else {
370 		set_default_env("!no env offset in OOB");
371 		return;
372 	}
373 #endif
374 
375 	ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
376 	if (ret) {
377 		set_default_env("!readenv() failed");
378 		return -EIO;
379 	}
380 
381 	return env_import(buf, 1);
382 #endif /* ! ENV_IS_EMBEDDED */
383 
384 	return 0;
385 }
386 #endif /* CONFIG_ENV_OFFSET_REDUND */
387 
388 U_BOOT_ENV_LOCATION(nand) = {
389 	.location	= ENVL_NAND,
390 	ENV_NAME("NAND")
391 	.load		= env_nand_load,
392 #if defined(CMD_SAVEENV)
393 	.save		= env_save_ptr(env_nand_save),
394 #endif
395 	.init		= env_nand_init,
396 };
397