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>
18 #include <asm/global_data.h>
19 #include <linux/stddef.h>
23 #include <u-boot/crc.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifndef CONFIG_SPL_BUILD
28 # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
30 # elif defined(CONFIG_ENV_ADDR_REDUND)
31 # error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
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)
43 #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
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 */
53 static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
54 #endif /* ENV_IS_EMBEDDED */
56 static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
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;
62 #ifdef CONFIG_ENV_ADDR_REDUND
64 static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
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 */
71 #ifdef CONFIG_ENV_ADDR_REDUND
73 static int env_flash_init(void)
75 int crc1_ok = 0, crc2_ok = 0;
77 uchar flag1 = flash_addr->flags;
78 uchar flag2 = flash_addr_new->flags;
80 ulong addr_default = (ulong)&default_environment[0];
81 ulong addr1 = (ulong)&(flash_addr->data);
82 ulong addr2 = (ulong)&(flash_addr_new->data);
84 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
86 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
88 if (crc1_ok && !crc2_ok) {
90 gd->env_valid = ENV_VALID;
91 } else if (!crc1_ok && crc2_ok) {
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) {
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;
121 static int env_flash_save(void)
124 char *saved_data = NULL;
125 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
127 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
131 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
133 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
136 debug("Protect off %08lX ... %08lX\n",
137 (ulong)flash_addr_new, end_addr_new);
139 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
142 rc = env_export(&env_new);
145 env_new.flags = new_flag;
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);
151 saved_data = malloc(up_data);
152 if (saved_data == NULL) {
153 printf("Unable to save the rest of sector (%ld)\n",
158 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
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);
165 puts("Erasing Flash...");
166 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
168 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
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,
180 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
181 sizeof(flash_addr->flags));
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,
198 env_t *etmp = flash_addr;
199 ulong ltmp = end_addr;
201 flash_addr = flash_addr_new;
202 flash_addr_new = etmp;
204 end_addr = end_addr_new;
214 /* try to re-protect */
215 flash_sect_protect(1, (ulong)flash_addr, end_addr);
216 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
220 #endif /* CMD_SAVEENV */
222 #else /* ! CONFIG_ENV_ADDR_REDUND */
225 static int env_flash_init(void)
227 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
228 gd->env_addr = (ulong)&(env_ptr->data);
229 gd->env_valid = ENV_VALID;
233 gd->env_addr = (ulong)&default_environment[0];
234 gd->env_valid = ENV_INVALID;
240 static int env_flash_save(void)
244 char *saved_data = NULL;
245 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
248 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
249 debug("Data to save 0x%lx\n", up_data);
251 saved_data = malloc(up_data);
252 if (saved_data == NULL) {
253 printf("Unable to save the rest of sector (%ld)\n",
258 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
259 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
260 (ulong)flash_addr + CONFIG_ENV_SIZE,
264 #endif /* CONFIG_ENV_SECT_SIZE */
266 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
268 if (flash_sect_protect(0, (long)flash_addr, end_addr))
271 rc = env_export(&env_new);
275 puts("Erasing Flash...");
276 if (flash_sect_erase((long)flash_addr, end_addr))
279 puts("Writing to Flash... ");
280 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
284 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
285 if (up_data) { /* restore the rest of sector */
286 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
287 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
288 if (flash_write(saved_data,
289 (long)flash_addr + CONFIG_ENV_SIZE,
301 /* try to re-protect */
302 flash_sect_protect(1, (long)flash_addr, end_addr);
305 #endif /* CMD_SAVEENV */
307 #endif /* CONFIG_ENV_ADDR_REDUND */
310 static int env_flash_load(void)
312 #ifdef CONFIG_ENV_ADDR_REDUND
313 if (gd->env_addr != (ulong)&(flash_addr->data)) {
314 env_t *etmp = flash_addr;
315 ulong ltmp = end_addr;
317 flash_addr = flash_addr_new;
318 flash_addr_new = etmp;
320 end_addr = end_addr_new;
324 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
325 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
326 char flag = ENV_REDUND_OBSOLETE;
328 gd->env_valid = ENV_REDUND;
329 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
331 (ulong)&(flash_addr_new->flags),
332 sizeof(flash_addr_new->flags));
333 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
336 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
337 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
338 char flag = ENV_REDUND_ACTIVE;
340 gd->env_valid = ENV_REDUND;
341 flash_sect_protect(0, (ulong)flash_addr, end_addr);
343 (ulong)&(flash_addr->flags),
344 sizeof(flash_addr->flags));
345 flash_sect_protect(1, (ulong)flash_addr, end_addr);
348 if (gd->env_valid == ENV_REDUND)
349 puts("*** Warning - some problems detected "
350 "reading environment; recovered successfully\n\n");
351 #endif /* CONFIG_ENV_ADDR_REDUND */
353 return env_import((char *)flash_addr, 1, H_EXTERNAL);
357 U_BOOT_ENV_LOCATION(flash) = {
358 .location = ENVL_FLASH,
361 .load = env_flash_load,
364 .save = env_save_ptr(env_flash_save),
367 .init = env_flash_init,