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)));
85 #ifdef CONFIG_ENV_ADDR_REDUND
89 int crc1_ok = 0, crc2_ok = 0;
91 uchar flag1 = flash_addr->flags;
92 uchar flag2 = flash_addr_new->flags;
94 ulong addr_default = (ulong)&default_environment[0];
95 ulong addr1 = (ulong)&(flash_addr->data);
96 ulong addr2 = (ulong)&(flash_addr_new->data);
98 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
99 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
101 if (crc1_ok && ! crc2_ok) {
102 gd->env_addr = addr1;
104 } else if (! crc1_ok && crc2_ok) {
105 gd->env_addr = addr2;
107 } else if (! crc1_ok && ! crc2_ok) {
108 gd->env_addr = addr_default;
110 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
111 gd->env_addr = addr1;
113 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
114 gd->env_addr = addr2;
116 } else if (flag1 == flag2) {
117 gd->env_addr = addr1;
119 } else if (flag1 == 0xFF) {
120 gd->env_addr = addr1;
122 } else if (flag2 == 0xFF) {
123 gd->env_addr = addr2;
135 char *saved_data = NULL;
138 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
139 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
143 debug("Protect off %08lX ... %08lX\n",
144 (ulong)flash_addr, end_addr);
146 if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
150 debug("Protect off %08lX ... %08lX\n",
151 (ulong)flash_addr_new, end_addr_new);
153 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
157 res = (char *)&env_new.data;
158 len = hexport('\0', &res, ENV_SIZE);
160 error("Cannot export environment: errno = %d\n", errno);
163 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
164 env_new.flags = new_flag;
166 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
167 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
168 debug("Data to save 0x%lX\n", up_data);
170 if ((saved_data = malloc(up_data)) == NULL) {
171 printf("Unable to save the rest of sector (%ld)\n",
176 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
177 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
178 (long)flash_addr_new + CONFIG_ENV_SIZE,
179 up_data, saved_data);
182 puts("Erasing Flash...");
183 debug(" %08lX ... %08lX ...",
184 (ulong)flash_addr_new, end_addr_new);
186 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
190 puts("Writing to Flash... ");
191 debug(" %08lX ... %08lX ...",
192 (ulong)&(flash_addr_new->data),
193 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
194 if ((rc = flash_write((char *)&env_new,
195 (ulong)flash_addr_new,
197 (rc = flash_write(&flag,
198 (ulong)&(flash_addr->flags),
199 sizeof(flash_addr->flags))) ) {
204 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
205 if (up_data) { /* restore the rest of sector */
206 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
207 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
208 if (flash_write(saved_data,
209 (long)flash_addr_new + CONFIG_ENV_SIZE,
219 env_t * etmp = flash_addr;
220 ulong ltmp = end_addr;
222 flash_addr = flash_addr_new;
223 flash_addr_new = etmp;
225 end_addr = end_addr_new;
233 /* try to re-protect */
234 (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
235 (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
239 #endif /* CMD_SAVEENV */
241 #else /* ! CONFIG_ENV_ADDR_REDUND */
245 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
246 gd->env_addr = (ulong)&(env_ptr->data);
251 gd->env_addr = (ulong)&default_environment[0];
264 char *saved_data = NULL;
265 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
268 up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
269 debug("Data to save 0x%lx\n", up_data);
271 if ((saved_data = malloc(up_data)) == NULL) {
272 printf("Unable to save the rest of sector (%ld)\n",
277 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
278 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
279 (ulong)flash_addr + CONFIG_ENV_SIZE,
283 #endif /* CONFIG_ENV_SECT_SIZE */
285 debug("Protect off %08lX ... %08lX\n",
286 (ulong)flash_addr, end_addr);
288 if (flash_sect_protect(0, (long)flash_addr, end_addr))
291 res = (char *)&env_new.data;
292 len = hexport('\0', &res, ENV_SIZE);
294 error("Cannot export environment: errno = %d\n", errno);
297 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
299 puts("Erasing Flash...");
300 if (flash_sect_erase((long)flash_addr, end_addr))
303 puts("Writing to Flash... ");
304 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
309 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
310 if (up_data) { /* restore the rest of sector */
311 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
312 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
313 if (flash_write(saved_data,
314 (long)flash_addr + CONFIG_ENV_SIZE,
326 /* try to re-protect */
327 (void) flash_sect_protect(1, (long)flash_addr, end_addr);
331 #endif /* CMD_SAVEENV */
333 #endif /* CONFIG_ENV_ADDR_REDUND */
335 void env_relocate_spec(void)
337 #ifdef CONFIG_ENV_ADDR_REDUND
338 if (gd->env_addr != (ulong)&(flash_addr->data)) {
339 env_t *etmp = flash_addr;
340 ulong ltmp = end_addr;
342 flash_addr = flash_addr_new;
343 flash_addr_new = etmp;
345 end_addr = end_addr_new;
349 if (flash_addr_new->flags != OBSOLETE_FLAG &&
350 crc32(0, flash_addr_new->data, ENV_SIZE) ==
351 flash_addr_new->crc) {
352 char flag = OBSOLETE_FLAG;
355 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
357 (ulong)&(flash_addr_new->flags),
358 sizeof(flash_addr_new->flags));
359 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
362 if (flash_addr->flags != ACTIVE_FLAG &&
363 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
364 char flag = ACTIVE_FLAG;
367 flash_sect_protect(0, (ulong)flash_addr, end_addr);
369 (ulong)&(flash_addr->flags),
370 sizeof(flash_addr->flags));
371 flash_sect_protect(1, (ulong)flash_addr, end_addr);
374 if (gd->env_valid == 2)
375 puts ("*** Warning - some problems detected "
376 "reading environment; recovered successfully\n\n");
377 #endif /* CONFIG_ENV_ADDR_REDUND */
379 env_import((char *)flash_addr, 1);