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 /* #define DEBUG */
11 
12 #include <common.h>
13 #include <command.h>
14 #include <env.h>
15 #include <env_internal.h>
16 #include <flash.h>
17 #include <log.h>
18 #include <asm/global_data.h>
19 #include <linux/stddef.h>
20 #include <malloc.h>
21 #include <search.h>
22 #include <errno.h>
23 #include <u-boot/crc.h>
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 #ifndef CONFIG_SPL_BUILD
28 # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
29 #  define CMD_SAVEENV
30 # elif defined(CONFIG_ENV_ADDR_REDUND)
31 #  error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
32 # endif
33 #endif
34 
35 /* TODO(sjg@chromium.org): Figure out all these special cases */
36 #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
37 	!defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
38 	!defined(CONFIG_TARGET_EDMINIV2)) || \
39 	!defined(CONFIG_SPL_BUILD)
40 #define LOADENV
41 #endif
42 
43 #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
44 #define INITENV
45 #endif
46 
47 #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
48 	!defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
49 #ifdef ENV_IS_EMBEDDED
50 static env_t *env_ptr = &embedded_environment;
51 #else /* ! ENV_IS_EMBEDDED */
52 
53 static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
54 #endif /* ENV_IS_EMBEDDED */
55 #endif
56 static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
57 
58 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
59 static ulong __maybe_unused end_addr =
60 		CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
61 
62 #ifdef CONFIG_ENV_ADDR_REDUND
63 
64 static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
65 
66 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
67 static ulong __maybe_unused end_addr_new =
68 		CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
69 #endif /* CONFIG_ENV_ADDR_REDUND */
70 
71 #ifdef CONFIG_ENV_ADDR_REDUND
72 #ifdef INITENV
env_flash_init(void)73 static int env_flash_init(void)
74 {
75 	int crc1_ok = 0, crc2_ok = 0;
76 
77 	uchar flag1 = flash_addr->flags;
78 	uchar flag2 = flash_addr_new->flags;
79 
80 	ulong addr_default = (ulong)&default_environment[0];
81 	ulong addr1 = (ulong)&(flash_addr->data);
82 	ulong addr2 = (ulong)&(flash_addr_new->data);
83 
84 	crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
85 	crc2_ok =
86 		crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
87 
88 	if (crc1_ok && !crc2_ok) {
89 		gd->env_addr	= addr1;
90 		gd->env_valid	= ENV_VALID;
91 	} else if (!crc1_ok && crc2_ok) {
92 		gd->env_addr	= addr2;
93 		gd->env_valid	= ENV_VALID;
94 	} else if (!crc1_ok && !crc2_ok) {
95 		gd->env_addr	= addr_default;
96 		gd->env_valid	= ENV_INVALID;
97 	} else if (flag1 == ENV_REDUND_ACTIVE &&
98 		   flag2 == ENV_REDUND_OBSOLETE) {
99 		gd->env_addr	= addr1;
100 		gd->env_valid	= ENV_VALID;
101 	} else if (flag1 == ENV_REDUND_OBSOLETE &&
102 		   flag2 == ENV_REDUND_ACTIVE) {
103 		gd->env_addr	= addr2;
104 		gd->env_valid	= ENV_VALID;
105 	} else if (flag1 == flag2) {
106 		gd->env_addr	= addr1;
107 		gd->env_valid	= ENV_REDUND;
108 	} else if (flag1 == 0xFF) {
109 		gd->env_addr	= addr1;
110 		gd->env_valid	= ENV_REDUND;
111 	} else if (flag2 == 0xFF) {
112 		gd->env_addr	= addr2;
113 		gd->env_valid	= ENV_REDUND;
114 	}
115 
116 	return 0;
117 }
118 #endif
119 
120 #ifdef CMD_SAVEENV
env_flash_save(void)121 static int env_flash_save(void)
122 {
123 	env_t	env_new;
124 	char	*saved_data = NULL;
125 	char	flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
126 	int	rc = 1;
127 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
128 	ulong	up_data = 0;
129 #endif
130 
131 	debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
132 
133 	if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
134 		goto done;
135 
136 	debug("Protect off %08lX ... %08lX\n",
137 		(ulong)flash_addr_new, end_addr_new);
138 
139 	if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
140 		goto done;
141 
142 	rc = env_export(&env_new);
143 	if (rc)
144 		return rc;
145 	env_new.flags	= new_flag;
146 
147 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
148 	up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
149 	debug("Data to save 0x%lX\n", up_data);
150 	if (up_data) {
151 		saved_data = malloc(up_data);
152 		if (saved_data == NULL) {
153 			printf("Unable to save the rest of sector (%ld)\n",
154 				up_data);
155 			goto done;
156 		}
157 		memcpy(saved_data,
158 			(void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
159 			up_data);
160 		debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
161 			(long)flash_addr_new + CONFIG_ENV_SIZE,
162 			up_data, saved_data);
163 	}
164 #endif
165 	puts("Erasing Flash...");
166 	debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
167 
168 	if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
169 		goto done;
170 
171 	puts("Writing to Flash... ");
172 	debug(" %08lX ... %08lX ...",
173 		(ulong)&(flash_addr_new->data),
174 		sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
175 	rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
176 			 sizeof(env_new));
177 	if (rc)
178 		goto perror;
179 
180 	rc = flash_write(&flag, (ulong)&(flash_addr->flags),
181 			 sizeof(flash_addr->flags));
182 	if (rc)
183 		goto perror;
184 
185 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
186 	if (up_data) { /* restore the rest of sector */
187 		debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
188 			(long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
189 		if (flash_write(saved_data,
190 				(long)flash_addr_new + CONFIG_ENV_SIZE,
191 				up_data))
192 			goto perror;
193 	}
194 #endif
195 	puts("done\n");
196 
197 	{
198 		env_t *etmp = flash_addr;
199 		ulong ltmp = end_addr;
200 
201 		flash_addr = flash_addr_new;
202 		flash_addr_new = etmp;
203 
204 		end_addr = end_addr_new;
205 		end_addr_new = ltmp;
206 	}
207 
208 	rc = 0;
209 	goto done;
210 perror:
211 	flash_perror(rc);
212 done:
213 	if (saved_data)
214 		free(saved_data);
215 	/* try to re-protect */
216 	flash_sect_protect(1, (ulong)flash_addr, end_addr);
217 	flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
218 
219 	return rc;
220 }
221 #endif /* CMD_SAVEENV */
222 
223 #else /* ! CONFIG_ENV_ADDR_REDUND */
224 
225 #ifdef INITENV
env_flash_init(void)226 static int env_flash_init(void)
227 {
228 	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
229 		gd->env_addr	= (ulong)&(env_ptr->data);
230 		gd->env_valid	= ENV_VALID;
231 		return 0;
232 	}
233 
234 	gd->env_addr	= (ulong)&default_environment[0];
235 	gd->env_valid	= ENV_INVALID;
236 	return 0;
237 }
238 #endif
239 
240 #ifdef CMD_SAVEENV
env_flash_save(void)241 static int env_flash_save(void)
242 {
243 	env_t	env_new;
244 	int	rc = 1;
245 	char	*saved_data = NULL;
246 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
247 	ulong	up_data = 0;
248 
249 	up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
250 	debug("Data to save 0x%lx\n", up_data);
251 	if (up_data) {
252 		saved_data = malloc(up_data);
253 		if (saved_data == NULL) {
254 			printf("Unable to save the rest of sector (%ld)\n",
255 				up_data);
256 			goto done;
257 		}
258 		memcpy(saved_data,
259 			(void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
260 		debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
261 			(ulong)flash_addr + CONFIG_ENV_SIZE,
262 			up_data,
263 			(ulong)saved_data);
264 	}
265 #endif	/* CONFIG_ENV_SECT_SIZE */
266 
267 	debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
268 
269 	if (flash_sect_protect(0, (long)flash_addr, end_addr))
270 		goto done;
271 
272 	rc = env_export(&env_new);
273 	if (rc)
274 		goto done;
275 
276 	puts("Erasing Flash...");
277 	if (flash_sect_erase((long)flash_addr, end_addr))
278 		goto done;
279 
280 	puts("Writing to Flash... ");
281 	rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
282 	if (rc != 0)
283 		goto perror;
284 
285 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
286 	if (up_data) {	/* restore the rest of sector */
287 		debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
288 			(ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
289 		if (flash_write(saved_data,
290 				(long)flash_addr + CONFIG_ENV_SIZE,
291 				up_data))
292 			goto perror;
293 	}
294 #endif
295 	puts("done\n");
296 	rc = 0;
297 	goto done;
298 perror:
299 	flash_perror(rc);
300 done:
301 	if (saved_data)
302 		free(saved_data);
303 	/* try to re-protect */
304 	flash_sect_protect(1, (long)flash_addr, end_addr);
305 	return rc;
306 }
307 #endif /* CMD_SAVEENV */
308 
309 #endif /* CONFIG_ENV_ADDR_REDUND */
310 
311 #ifdef LOADENV
env_flash_load(void)312 static int env_flash_load(void)
313 {
314 #ifdef CONFIG_ENV_ADDR_REDUND
315 	if (gd->env_addr != (ulong)&(flash_addr->data)) {
316 		env_t *etmp = flash_addr;
317 		ulong ltmp = end_addr;
318 
319 		flash_addr = flash_addr_new;
320 		flash_addr_new = etmp;
321 
322 		end_addr = end_addr_new;
323 		end_addr_new = ltmp;
324 	}
325 
326 	if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
327 	    crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
328 		char flag = ENV_REDUND_OBSOLETE;
329 
330 		gd->env_valid = ENV_REDUND;
331 		flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
332 		flash_write(&flag,
333 			    (ulong)&(flash_addr_new->flags),
334 			    sizeof(flash_addr_new->flags));
335 		flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
336 	}
337 
338 	if (flash_addr->flags != ENV_REDUND_ACTIVE &&
339 	    (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
340 		char flag = ENV_REDUND_ACTIVE;
341 
342 		gd->env_valid = ENV_REDUND;
343 		flash_sect_protect(0, (ulong)flash_addr, end_addr);
344 		flash_write(&flag,
345 			    (ulong)&(flash_addr->flags),
346 			    sizeof(flash_addr->flags));
347 		flash_sect_protect(1, (ulong)flash_addr, end_addr);
348 	}
349 
350 	if (gd->env_valid == ENV_REDUND)
351 		puts("*** Warning - some problems detected "
352 		     "reading environment; recovered successfully\n\n");
353 #endif /* CONFIG_ENV_ADDR_REDUND */
354 
355 	return env_import((char *)flash_addr, 1, H_EXTERNAL);
356 }
357 #endif /* LOADENV */
358 
359 U_BOOT_ENV_LOCATION(flash) = {
360 	.location	= ENVL_FLASH,
361 	ENV_NAME("Flash")
362 #ifdef LOADENV
363 	.load		= env_flash_load,
364 #endif
365 #ifdef CMD_SAVEENV
366 	.save		= env_save_ptr(env_flash_save),
367 #endif
368 #ifdef INITENV
369 	.init		= env_flash_init,
370 #endif
371 };
372