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