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>
17 #include <asm/global_data.h>
18 #include <linux/stddef.h>
22 #include <u-boot/crc.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 #ifndef CONFIG_SPL_BUILD
27 # 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 addr1 = (ulong)&(flash_addr->data);
81 ulong addr2 = (ulong)&(flash_addr_new->data);
83 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
85 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
87 if (crc1_ok && !crc2_ok) {
89 gd->env_valid = ENV_VALID;
90 } else if (!crc1_ok && crc2_ok) {
92 gd->env_valid = ENV_VALID;
93 } else if (!crc1_ok && !crc2_ok) {
94 gd->env_valid = ENV_INVALID;
95 } else if (flag1 == ENV_REDUND_ACTIVE &&
96 flag2 == ENV_REDUND_OBSOLETE) {
98 gd->env_valid = ENV_VALID;
99 } else if (flag1 == ENV_REDUND_OBSOLETE &&
100 flag2 == ENV_REDUND_ACTIVE) {
101 gd->env_addr = addr2;
102 gd->env_valid = ENV_VALID;
103 } else if (flag1 == flag2) {
104 gd->env_addr = addr1;
105 gd->env_valid = ENV_REDUND;
106 } else if (flag1 == 0xFF) {
107 gd->env_addr = addr1;
108 gd->env_valid = ENV_REDUND;
109 } else if (flag2 == 0xFF) {
110 gd->env_addr = addr2;
111 gd->env_valid = ENV_REDUND;
119 static int env_flash_save(void)
122 char *saved_data = NULL;
123 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
125 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
129 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
131 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
134 debug("Protect off %08lX ... %08lX\n",
135 (ulong)flash_addr_new, end_addr_new);
137 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
140 rc = env_export(&env_new);
143 env_new.flags = new_flag;
145 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
146 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
147 debug("Data to save 0x%lX\n", up_data);
149 saved_data = malloc(up_data);
150 if (saved_data == NULL) {
151 printf("Unable to save the rest of sector (%ld)\n",
156 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
158 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
159 (long)flash_addr_new + CONFIG_ENV_SIZE,
160 up_data, saved_data);
163 puts("Erasing Flash...");
164 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
166 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
169 puts("Writing to Flash... ");
170 debug(" %08lX ... %08lX ...",
171 (ulong)&(flash_addr_new->data),
172 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
173 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
178 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
179 sizeof(flash_addr->flags));
183 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
184 if (up_data) { /* restore the rest of sector */
185 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
186 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
187 if (flash_write(saved_data,
188 (long)flash_addr_new + CONFIG_ENV_SIZE,
196 env_t *etmp = flash_addr;
197 ulong ltmp = end_addr;
199 flash_addr = flash_addr_new;
200 flash_addr_new = etmp;
202 end_addr = end_addr_new;
212 /* try to re-protect */
213 flash_sect_protect(1, (ulong)flash_addr, end_addr);
214 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
218 #endif /* CMD_SAVEENV */
220 #else /* ! CONFIG_ENV_ADDR_REDUND */
223 static int env_flash_init(void)
225 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
226 gd->env_addr = (ulong)&(env_ptr->data);
227 gd->env_valid = ENV_VALID;
231 gd->env_valid = ENV_INVALID;
237 static int env_flash_save(void)
241 char *saved_data = NULL;
242 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
245 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
246 debug("Data to save 0x%lx\n", up_data);
248 saved_data = malloc(up_data);
249 if (saved_data == NULL) {
250 printf("Unable to save the rest of sector (%ld)\n",
255 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
256 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
257 (ulong)flash_addr + CONFIG_ENV_SIZE,
261 #endif /* CONFIG_ENV_SECT_SIZE */
263 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
265 if (flash_sect_protect(0, (long)flash_addr, end_addr))
268 rc = env_export(&env_new);
272 puts("Erasing Flash...");
273 if (flash_sect_erase((long)flash_addr, end_addr))
276 puts("Writing to Flash... ");
277 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
281 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
282 if (up_data) { /* restore the rest of sector */
283 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
284 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
285 if (flash_write(saved_data,
286 (long)flash_addr + CONFIG_ENV_SIZE,
298 /* try to re-protect */
299 flash_sect_protect(1, (long)flash_addr, end_addr);
302 #endif /* CMD_SAVEENV */
304 #endif /* CONFIG_ENV_ADDR_REDUND */
307 static int env_flash_load(void)
309 #ifdef CONFIG_ENV_ADDR_REDUND
310 if (gd->env_addr != (ulong)&(flash_addr->data)) {
311 env_t *etmp = flash_addr;
312 ulong ltmp = end_addr;
314 flash_addr = flash_addr_new;
315 flash_addr_new = etmp;
317 end_addr = end_addr_new;
321 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
322 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
323 char flag = ENV_REDUND_OBSOLETE;
325 gd->env_valid = ENV_REDUND;
326 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
328 (ulong)&(flash_addr_new->flags),
329 sizeof(flash_addr_new->flags));
330 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
333 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
334 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
335 char flag = ENV_REDUND_ACTIVE;
337 gd->env_valid = ENV_REDUND;
338 flash_sect_protect(0, (ulong)flash_addr, end_addr);
340 (ulong)&(flash_addr->flags),
341 sizeof(flash_addr->flags));
342 flash_sect_protect(1, (ulong)flash_addr, end_addr);
345 if (gd->env_valid == ENV_REDUND)
346 puts("*** Warning - some problems detected "
347 "reading environment; recovered successfully\n\n");
348 #endif /* CONFIG_ENV_ADDR_REDUND */
350 return env_import((char *)flash_addr, 1, H_EXTERNAL);
354 U_BOOT_ENV_LOCATION(flash) = {
355 .location = ENVL_FLASH,
358 .load = env_flash_load,
361 .save = env_save_ptr(env_flash_save),
364 .init = env_flash_init,