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;
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);
221 #endif /* CMD_SAVEENV */
223 #else /* ! CONFIG_ENV_ADDR_REDUND */
226 static int env_flash_init(void)
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;
234 gd->env_addr = (ulong)&default_environment[0];
235 gd->env_valid = ENV_INVALID;
241 static int env_flash_save(void)
245 char *saved_data = NULL;
246 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
249 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
250 debug("Data to save 0x%lx\n", up_data);
252 saved_data = malloc(up_data);
253 if (saved_data == NULL) {
254 printf("Unable to save the rest of sector (%ld)\n",
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,
265 #endif /* CONFIG_ENV_SECT_SIZE */
267 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
269 if (flash_sect_protect(0, (long)flash_addr, end_addr))
272 rc = env_export(&env_new);
276 puts("Erasing Flash...");
277 if (flash_sect_erase((long)flash_addr, end_addr))
280 puts("Writing to Flash... ");
281 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
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,
303 /* try to re-protect */
304 flash_sect_protect(1, (long)flash_addr, end_addr);
307 #endif /* CMD_SAVEENV */
309 #endif /* CONFIG_ENV_ADDR_REDUND */
312 static int env_flash_load(void)
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;
319 flash_addr = flash_addr_new;
320 flash_addr_new = etmp;
322 end_addr = end_addr_new;
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;
330 gd->env_valid = ENV_REDUND;
331 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
333 (ulong)&(flash_addr_new->flags),
334 sizeof(flash_addr_new->flags));
335 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
338 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
339 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
340 char flag = ENV_REDUND_ACTIVE;
342 gd->env_valid = ENV_REDUND;
343 flash_sect_protect(0, (ulong)flash_addr, end_addr);
345 (ulong)&(flash_addr->flags),
346 sizeof(flash_addr->flags));
347 flash_sect_protect(1, (ulong)flash_addr, end_addr);
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 */
355 return env_import((char *)flash_addr, 1, H_EXTERNAL);
359 U_BOOT_ENV_LOCATION(flash) = {
360 .location = ENVL_FLASH,
363 .load = env_flash_load,
366 .save = env_save_ptr(env_flash_save),
369 .init = env_flash_init,