2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <environment.h>
32 #include <linux/stddef.h>
37 DECLARE_GLOBAL_DATA_PTR;
39 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
41 #elif defined(CONFIG_ENV_ADDR_REDUND)
42 #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
45 #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
46 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
49 char * env_name_spec = "Flash";
51 #ifdef ENV_IS_EMBEDDED
53 extern uchar environment[];
54 env_t *env_ptr = (env_t *)(&environment[0]);
56 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
58 #else /* ! ENV_IS_EMBEDDED */
60 env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
61 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
63 #endif /* ENV_IS_EMBEDDED */
65 #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
66 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
67 static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
70 #ifdef CONFIG_ENV_ADDR_REDUND
71 static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
73 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
74 static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
75 #endif /* CONFIG_ENV_ADDR_REDUND */
77 extern uchar default_environment[];
80 uchar env_get_char_spec(int index)
82 return (*((uchar *)(gd->env_addr + index)));
88 #ifdef CONFIG_ENV_ADDR_REDUND
92 int crc1_ok = 0, crc2_ok = 0;
94 uchar flag1 = flash_addr->flags;
95 uchar flag2 = flash_addr_new->flags;
97 ulong addr_default = (ulong)&default_environment[0];
98 ulong addr1 = (ulong)&(flash_addr->data);
99 ulong addr2 = (ulong)&(flash_addr_new->data);
101 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
102 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
104 if (crc1_ok && ! crc2_ok) {
105 gd->env_addr = addr1;
107 } else if (! crc1_ok && crc2_ok) {
108 gd->env_addr = addr2;
110 } else if (! crc1_ok && ! crc2_ok) {
111 gd->env_addr = addr_default;
113 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
114 gd->env_addr = addr1;
116 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
117 gd->env_addr = addr2;
119 } else if (flag1 == flag2) {
120 gd->env_addr = addr1;
122 } else if (flag1 == 0xFF) {
123 gd->env_addr = addr1;
125 } else if (flag2 == 0xFF) {
126 gd->env_addr = addr2;
138 char *saved_data = NULL;
141 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
142 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
146 debug("Protect off %08lX ... %08lX\n",
147 (ulong)flash_addr, end_addr);
149 if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
153 debug("Protect off %08lX ... %08lX\n",
154 (ulong)flash_addr_new, end_addr_new);
156 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
160 res = (char *)&env_new.data;
161 len = hexport('\0', &res, ENV_SIZE);
163 error("Cannot export environment: errno = %d\n", errno);
166 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
167 env_new.flags = new_flag;
169 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
170 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
171 debug("Data to save 0x%lX\n", up_data);
173 if ((saved_data = malloc(up_data)) == NULL) {
174 printf("Unable to save the rest of sector (%ld)\n",
179 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
180 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
181 (long)flash_addr_new + CONFIG_ENV_SIZE,
182 up_data, saved_data);
185 puts("Erasing Flash...");
186 debug(" %08lX ... %08lX ...",
187 (ulong)flash_addr_new, end_addr_new);
189 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
193 puts("Writing to Flash... ");
194 debug(" %08lX ... %08lX ...",
195 (ulong)&(flash_addr_new->data),
196 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
197 if ((rc = flash_write((char *)&env_new,
198 (ulong)flash_addr_new,
200 (rc = flash_write(&flag,
201 (ulong)&(flash_addr->flags),
202 sizeof(flash_addr->flags))) ) {
207 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
208 if (up_data) { /* restore the rest of sector */
209 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
210 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
211 if (flash_write(saved_data,
212 (long)flash_addr_new + CONFIG_ENV_SIZE,
222 env_t * etmp = flash_addr;
223 ulong ltmp = end_addr;
225 flash_addr = flash_addr_new;
226 flash_addr_new = etmp;
228 end_addr = end_addr_new;
236 /* try to re-protect */
237 (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
238 (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
242 #endif /* CMD_SAVEENV */
244 #else /* ! CONFIG_ENV_ADDR_REDUND */
248 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
249 gd->env_addr = (ulong)&(env_ptr->data);
254 gd->env_addr = (ulong)&default_environment[0];
267 char *saved_data = NULL;
268 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
271 up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
272 debug("Data to save 0x%lx\n", up_data);
274 if ((saved_data = malloc(up_data)) == NULL) {
275 printf("Unable to save the rest of sector (%ld)\n",
280 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
281 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
282 (ulong)flash_addr + CONFIG_ENV_SIZE,
286 #endif /* CONFIG_ENV_SECT_SIZE */
288 debug("Protect off %08lX ... %08lX\n",
289 (ulong)flash_addr, end_addr);
291 if (flash_sect_protect(0, (long)flash_addr, end_addr))
294 res = (char *)&env_new.data;
295 len = hexport('\0', &res, ENV_SIZE);
297 error("Cannot export environment: errno = %d\n", errno);
300 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
302 puts("Erasing Flash...");
303 if (flash_sect_erase((long)flash_addr, end_addr))
306 puts("Writing to Flash... ");
307 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
312 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
313 if (up_data) { /* restore the rest of sector */
314 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
315 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
316 if (flash_write(saved_data,
317 (long)flash_addr + CONFIG_ENV_SIZE,
329 /* try to re-protect */
330 (void) flash_sect_protect(1, (long)flash_addr, end_addr);
334 #endif /* CMD_SAVEENV */
336 #endif /* CONFIG_ENV_ADDR_REDUND */
338 void env_relocate_spec(void)
340 #ifdef CONFIG_ENV_ADDR_REDUND
341 if (gd->env_addr != (ulong)&(flash_addr->data)) {
342 env_t *etmp = flash_addr;
343 ulong ltmp = end_addr;
345 flash_addr = flash_addr_new;
346 flash_addr_new = etmp;
348 end_addr = end_addr_new;
352 if (flash_addr_new->flags != OBSOLETE_FLAG &&
353 crc32(0, flash_addr_new->data, ENV_SIZE) ==
354 flash_addr_new->crc) {
355 char flag = OBSOLETE_FLAG;
358 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
360 (ulong)&(flash_addr_new->flags),
361 sizeof(flash_addr_new->flags));
362 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
365 if (flash_addr->flags != ACTIVE_FLAG &&
366 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
367 char flag = ACTIVE_FLAG;
370 flash_sect_protect(0, (ulong)flash_addr, end_addr);
372 (ulong)&(flash_addr->flags),
373 sizeof(flash_addr->flags));
374 flash_sect_protect(1, (ulong)flash_addr, end_addr);
377 if (gd->env_valid == 2)
378 puts ("*** Warning - some problems detected "
379 "reading environment; recovered successfully\n\n");
380 #endif /* CONFIG_ENV_ADDR_REDUND */
382 env_import((char *)flash_addr, 1);