1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
15 #include <env_internal.h>
16 #include <linux/stddef.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #ifndef CONFIG_SPL_BUILD
24 # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
26 # elif defined(CONFIG_ENV_ADDR_REDUND)
27 # error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
31 #if defined(CONFIG_ENV_SIZE_REDUND) && \
32 (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
33 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
36 /* TODO(sjg@chromium.org): Figure out all these special cases */
37 #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
38 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
39 !defined(CONFIG_TARGET_EDMINIV2)) || \
40 !defined(CONFIG_SPL_BUILD)
44 #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
48 #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
49 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
50 #ifdef ENV_IS_EMBEDDED
51 static env_t *env_ptr = &embedded_environment;
52 #else /* ! ENV_IS_EMBEDDED */
54 static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
55 #endif /* ENV_IS_EMBEDDED */
57 static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
59 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
60 static ulong __maybe_unused end_addr =
61 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
63 #ifdef CONFIG_ENV_ADDR_REDUND
65 static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
67 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
68 static ulong __maybe_unused end_addr_new =
69 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
70 #endif /* CONFIG_ENV_ADDR_REDUND */
72 #ifdef CONFIG_ENV_ADDR_REDUND
74 static int env_flash_init(void)
76 int crc1_ok = 0, crc2_ok = 0;
78 uchar flag1 = flash_addr->flags;
79 uchar flag2 = flash_addr_new->flags;
81 ulong addr_default = (ulong)&default_environment[0];
82 ulong addr1 = (ulong)&(flash_addr->data);
83 ulong addr2 = (ulong)&(flash_addr_new->data);
85 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
87 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
89 if (crc1_ok && !crc2_ok) {
91 gd->env_valid = ENV_VALID;
92 } else if (!crc1_ok && crc2_ok) {
94 gd->env_valid = ENV_VALID;
95 } else if (!crc1_ok && !crc2_ok) {
96 gd->env_addr = addr_default;
97 gd->env_valid = ENV_INVALID;
98 } else if (flag1 == ENV_REDUND_ACTIVE &&
99 flag2 == ENV_REDUND_OBSOLETE) {
100 gd->env_addr = addr1;
101 gd->env_valid = ENV_VALID;
102 } else if (flag1 == ENV_REDUND_OBSOLETE &&
103 flag2 == ENV_REDUND_ACTIVE) {
104 gd->env_addr = addr2;
105 gd->env_valid = ENV_VALID;
106 } else if (flag1 == flag2) {
107 gd->env_addr = addr1;
108 gd->env_valid = ENV_REDUND;
109 } else if (flag1 == 0xFF) {
110 gd->env_addr = addr1;
111 gd->env_valid = ENV_REDUND;
112 } else if (flag2 == 0xFF) {
113 gd->env_addr = addr2;
114 gd->env_valid = ENV_REDUND;
122 static int env_flash_save(void)
125 char *saved_data = NULL;
126 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
128 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
132 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
134 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
137 debug("Protect off %08lX ... %08lX\n",
138 (ulong)flash_addr_new, end_addr_new);
140 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
143 rc = env_export(&env_new);
146 env_new.flags = new_flag;
148 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
149 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
150 debug("Data to save 0x%lX\n", up_data);
152 saved_data = malloc(up_data);
153 if (saved_data == NULL) {
154 printf("Unable to save the rest of sector (%ld)\n",
159 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
161 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
162 (long)flash_addr_new + CONFIG_ENV_SIZE,
163 up_data, saved_data);
166 puts("Erasing Flash...");
167 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
169 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
172 puts("Writing to Flash... ");
173 debug(" %08lX ... %08lX ...",
174 (ulong)&(flash_addr_new->data),
175 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
176 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
181 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
182 sizeof(flash_addr->flags));
186 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
187 if (up_data) { /* restore the rest of sector */
188 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
189 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
190 if (flash_write(saved_data,
191 (long)flash_addr_new + CONFIG_ENV_SIZE,
199 env_t *etmp = flash_addr;
200 ulong ltmp = end_addr;
202 flash_addr = flash_addr_new;
203 flash_addr_new = etmp;
205 end_addr = end_addr_new;
216 /* try to re-protect */
217 flash_sect_protect(1, (ulong)flash_addr, end_addr);
218 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
222 #endif /* CMD_SAVEENV */
224 #else /* ! CONFIG_ENV_ADDR_REDUND */
227 static int env_flash_init(void)
229 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
230 gd->env_addr = (ulong)&(env_ptr->data);
231 gd->env_valid = ENV_VALID;
235 gd->env_addr = (ulong)&default_environment[0];
236 gd->env_valid = ENV_INVALID;
242 static int env_flash_save(void)
246 char *saved_data = NULL;
247 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
250 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
251 debug("Data to save 0x%lx\n", up_data);
253 saved_data = malloc(up_data);
254 if (saved_data == NULL) {
255 printf("Unable to save the rest of sector (%ld)\n",
260 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
261 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
262 (ulong)flash_addr + CONFIG_ENV_SIZE,
266 #endif /* CONFIG_ENV_SECT_SIZE */
268 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
270 if (flash_sect_protect(0, (long)flash_addr, end_addr))
273 rc = env_export(&env_new);
277 puts("Erasing Flash...");
278 if (flash_sect_erase((long)flash_addr, end_addr))
281 puts("Writing to Flash... ");
282 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
286 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
287 if (up_data) { /* restore the rest of sector */
288 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
289 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
290 if (flash_write(saved_data,
291 (long)flash_addr + CONFIG_ENV_SIZE,
304 /* try to re-protect */
305 flash_sect_protect(1, (long)flash_addr, end_addr);
308 #endif /* CMD_SAVEENV */
310 #endif /* CONFIG_ENV_ADDR_REDUND */
313 static int env_flash_load(void)
315 #ifdef CONFIG_ENV_ADDR_REDUND
316 if (gd->env_addr != (ulong)&(flash_addr->data)) {
317 env_t *etmp = flash_addr;
318 ulong ltmp = end_addr;
320 flash_addr = flash_addr_new;
321 flash_addr_new = etmp;
323 end_addr = end_addr_new;
327 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
328 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
329 char flag = ENV_REDUND_OBSOLETE;
331 gd->env_valid = ENV_REDUND;
332 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
334 (ulong)&(flash_addr_new->flags),
335 sizeof(flash_addr_new->flags));
336 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
339 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
340 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
341 char flag = ENV_REDUND_ACTIVE;
343 gd->env_valid = ENV_REDUND;
344 flash_sect_protect(0, (ulong)flash_addr, end_addr);
346 (ulong)&(flash_addr->flags),
347 sizeof(flash_addr->flags));
348 flash_sect_protect(1, (ulong)flash_addr, end_addr);
351 if (gd->env_valid == ENV_REDUND)
352 puts("*** Warning - some problems detected "
353 "reading environment; recovered successfully\n\n");
354 #endif /* CONFIG_ENV_ADDR_REDUND */
356 return env_import((char *)flash_addr, 1);
360 U_BOOT_ENV_LOCATION(flash) = {
361 .location = ENVL_FLASH,
364 .load = env_flash_load,
367 .save = env_save_ptr(env_flash_save),
370 .init = env_flash_init,