1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
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
32 #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
33 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
34 !defined(CONFIG_TARGET_EDMINIV2)) || \
35 !defined(CONFIG_SPL_BUILD)
39 #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
43 #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
44 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
45 #ifdef ENV_IS_EMBEDDED
46 static env_t *env_ptr = &embedded_environment;
47 #else /* ! ENV_IS_EMBEDDED */
49 static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
50 #endif /* ENV_IS_EMBEDDED */
52 static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
54 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
55 static ulong __maybe_unused end_addr =
56 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
58 #ifdef CONFIG_ENV_ADDR_REDUND
60 static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
62 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
63 static ulong __maybe_unused end_addr_new =
64 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
65 #endif /* CONFIG_ENV_ADDR_REDUND */
67 #ifdef CONFIG_ENV_ADDR_REDUND
69 static int env_flash_init(void)
71 int crc1_ok = 0, crc2_ok = 0;
73 uchar flag1 = flash_addr->flags;
74 uchar flag2 = flash_addr_new->flags;
76 ulong addr_default = (ulong)&default_environment[0];
77 ulong addr1 = (ulong)&(flash_addr->data);
78 ulong addr2 = (ulong)&(flash_addr_new->data);
80 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
82 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
84 if (crc1_ok && !crc2_ok) {
86 gd->env_valid = ENV_VALID;
87 } else if (!crc1_ok && crc2_ok) {
89 gd->env_valid = ENV_VALID;
90 } else if (!crc1_ok && !crc2_ok) {
91 gd->env_addr = addr_default;
92 gd->env_valid = ENV_INVALID;
93 } else if (flag1 == ENV_REDUND_ACTIVE &&
94 flag2 == ENV_REDUND_OBSOLETE) {
96 gd->env_valid = ENV_VALID;
97 } else if (flag1 == ENV_REDUND_OBSOLETE &&
98 flag2 == ENV_REDUND_ACTIVE) {
100 gd->env_valid = ENV_VALID;
101 } else if (flag1 == flag2) {
102 gd->env_addr = addr1;
103 gd->env_valid = ENV_REDUND;
104 } else if (flag1 == 0xFF) {
105 gd->env_addr = addr1;
106 gd->env_valid = ENV_REDUND;
107 } else if (flag2 == 0xFF) {
108 gd->env_addr = addr2;
109 gd->env_valid = ENV_REDUND;
117 static int env_flash_save(void)
120 char *saved_data = NULL;
121 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
123 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
127 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
129 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
132 debug("Protect off %08lX ... %08lX\n",
133 (ulong)flash_addr_new, end_addr_new);
135 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
138 rc = env_export(&env_new);
141 env_new.flags = new_flag;
143 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
144 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
145 debug("Data to save 0x%lX\n", up_data);
147 saved_data = malloc(up_data);
148 if (saved_data == NULL) {
149 printf("Unable to save the rest of sector (%ld)\n",
154 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
156 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
157 (long)flash_addr_new + CONFIG_ENV_SIZE,
158 up_data, saved_data);
161 puts("Erasing Flash...");
162 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
164 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
167 puts("Writing to Flash... ");
168 debug(" %08lX ... %08lX ...",
169 (ulong)&(flash_addr_new->data),
170 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
171 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
176 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
177 sizeof(flash_addr->flags));
181 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
182 if (up_data) { /* restore the rest of sector */
183 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
184 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
185 if (flash_write(saved_data,
186 (long)flash_addr_new + CONFIG_ENV_SIZE,
194 env_t *etmp = flash_addr;
195 ulong ltmp = end_addr;
197 flash_addr = flash_addr_new;
198 flash_addr_new = etmp;
200 end_addr = end_addr_new;
211 /* try to re-protect */
212 flash_sect_protect(1, (ulong)flash_addr, end_addr);
213 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
217 #endif /* CMD_SAVEENV */
219 #else /* ! CONFIG_ENV_ADDR_REDUND */
222 static int env_flash_init(void)
224 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
225 gd->env_addr = (ulong)&(env_ptr->data);
226 gd->env_valid = ENV_VALID;
230 gd->env_addr = (ulong)&default_environment[0];
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,
299 /* try to re-protect */
300 flash_sect_protect(1, (long)flash_addr, end_addr);
303 #endif /* CMD_SAVEENV */
305 #endif /* CONFIG_ENV_ADDR_REDUND */
308 static int env_flash_load(void)
310 #ifdef CONFIG_ENV_ADDR_REDUND
311 if (gd->env_addr != (ulong)&(flash_addr->data)) {
312 env_t *etmp = flash_addr;
313 ulong ltmp = end_addr;
315 flash_addr = flash_addr_new;
316 flash_addr_new = etmp;
318 end_addr = end_addr_new;
322 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
323 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
324 char flag = ENV_REDUND_OBSOLETE;
326 gd->env_valid = ENV_REDUND;
327 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
329 (ulong)&(flash_addr_new->flags),
330 sizeof(flash_addr_new->flags));
331 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
334 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
335 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
336 char flag = ENV_REDUND_ACTIVE;
338 gd->env_valid = ENV_REDUND;
339 flash_sect_protect(0, (ulong)flash_addr, end_addr);
341 (ulong)&(flash_addr->flags),
342 sizeof(flash_addr->flags));
343 flash_sect_protect(1, (ulong)flash_addr, end_addr);
346 if (gd->env_valid == ENV_REDUND)
347 puts("*** Warning - some problems detected "
348 "reading environment; recovered successfully\n\n");
349 #endif /* CONFIG_ENV_ADDR_REDUND */
351 return env_import((char *)flash_addr, 1);
355 U_BOOT_ENV_LOCATION(flash) = {
356 .location = ENVL_FLASH,
359 .load = env_flash_load,
362 .save = env_save_ptr(env_flash_save),
365 .init = env_flash_init,