blob: a99f850e91f57d26e3c58bf166792701f7e5463c [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27/* #define DEBUG */
28
29#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000030#include <command.h>
31#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000032#include <linux/stddef.h>
wdenk47cd00f2003-03-06 13:39:27 +000033#include <malloc.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020034#include <search.h>
35#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000036
Wolfgang Denkd87080b2006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
Mike Frysingerbdab39d2009-01-28 19:08:14 -050039#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
wdenkc6097192002-11-03 00:24:07 +000040#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020041#elif defined(CONFIG_ENV_ADDR_REDUND)
Igor Grinberg82b54b92011-11-07 01:14:09 +000042#error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
wdenkc6097192002-11-03 00:24:07 +000043#endif
44
Igor Grinberg82b54b92011-11-07 01:14:09 +000045#if defined(CONFIG_ENV_SIZE_REDUND) && \
46 (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020047#error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
wdenkc6097192002-11-03 00:24:07 +000048#endif
49
Igor Grinberg82b54b92011-11-07 01:14:09 +000050char *env_name_spec = "Flash";
wdenkc6097192002-11-03 00:24:07 +000051
52#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000053env_t *env_ptr = &environment;
wdenkc6097192002-11-03 00:24:07 +000054
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020055static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000056
57#else /* ! ENV_IS_EMBEDDED */
58
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020059env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020060static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000061#endif /* ENV_IS_EMBEDDED */
62
Wolfgang Denkea882ba2010-06-20 23:33:59 +020063#if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
64/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
65static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
66#endif
67
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020068#ifdef CONFIG_ENV_ADDR_REDUND
69static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
wdenkc6097192002-11-03 00:24:07 +000070
Wolfgang Denkea882ba2010-06-20 23:33:59 +020071/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020072static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020073#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000074
wdenkc6097192002-11-03 00:24:07 +000075
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076uchar env_get_char_spec(int index)
wdenkc6097192002-11-03 00:24:07 +000077{
Igor Grinberg82b54b92011-11-07 01:14:09 +000078 return *((uchar *)(gd->env_addr + index));
wdenkc6097192002-11-03 00:24:07 +000079}
80
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020081#ifdef CONFIG_ENV_ADDR_REDUND
Igor Grinberg82b54b92011-11-07 01:14:09 +000082int env_init(void)
wdenkc6097192002-11-03 00:24:07 +000083{
wdenk8ed96042005-01-09 23:16:25 +000084 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000085
86 uchar flag1 = flash_addr->flags;
87 uchar flag2 = flash_addr_new->flags;
88
89 ulong addr_default = (ulong)&default_environment[0];
90 ulong addr1 = (ulong)&(flash_addr->data);
91 ulong addr2 = (ulong)&(flash_addr_new->data);
92
Igor Grinberg82b54b92011-11-07 01:14:09 +000093 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
94 crc2_ok =
95 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenk8ed96042005-01-09 23:16:25 +000096
Igor Grinberg82b54b92011-11-07 01:14:09 +000097 if (crc1_ok && !crc2_ok) {
98 gd->env_addr = addr1;
99 gd->env_valid = 1;
100 } else if (!crc1_ok && crc2_ok) {
101 gd->env_addr = addr2;
102 gd->env_valid = 1;
103 } else if (!crc1_ok && !crc2_ok) {
104 gd->env_addr = addr_default;
105 gd->env_valid = 0;
wdenkc3f9d492004-03-14 00:59:59 +0000106 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000107 gd->env_addr = addr1;
108 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000109 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000110 gd->env_addr = addr2;
111 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000112 } else if (flag1 == flag2) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000113 gd->env_addr = addr1;
114 gd->env_valid = 2;
wdenkc3f9d492004-03-14 00:59:59 +0000115 } else if (flag1 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000116 gd->env_addr = addr1;
117 gd->env_valid = 2;
wdenkc3f9d492004-03-14 00:59:59 +0000118 } else if (flag2 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000119 gd->env_addr = addr2;
120 gd->env_valid = 2;
wdenkc6097192002-11-03 00:24:07 +0000121 }
122
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200123 return 0;
wdenkc6097192002-11-03 00:24:07 +0000124}
125
126#ifdef CMD_SAVEENV
127int saveenv(void)
128{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200129 env_t env_new;
130 ssize_t len;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000131 char *res, *saved_data = NULL;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200132 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000133 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200134#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200135 ulong up_data = 0;
wdenk500545c2003-03-06 14:23:06 +0000136#endif
wdenkc6097192002-11-03 00:24:07 +0000137
Igor Grinberg82b54b92011-11-07 01:14:09 +0000138 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000139
Igor Grinberg82b54b92011-11-07 01:14:09 +0000140 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200141 goto done;
wdenkc6097192002-11-03 00:24:07 +0000142
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200143 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000144 (ulong)flash_addr_new, end_addr_new);
145
Igor Grinberg82b54b92011-11-07 01:14:09 +0000146 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200147 goto done;
wdenkc6097192002-11-03 00:24:07 +0000148
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200149 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100150 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200151 if (len < 0) {
152 error("Cannot export environment: errno = %d\n", errno);
153 goto done;
154 }
Igor Grinberg82b54b92011-11-07 01:14:09 +0000155 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
156 env_new.flags = new_flag;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200157
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200158#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg82b54b92011-11-07 01:14:09 +0000159 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200160 debug("Data to save 0x%lX\n", up_data);
wdenk47cd00f2003-03-06 13:39:27 +0000161 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000162 saved_data = malloc(up_data);
163 if (saved_data == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000164 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000165 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200166 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000167 }
wdenk8bde7f72003-06-27 21:31:46 +0000168 memcpy(saved_data,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000169 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
170 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200171 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
172 (long)flash_addr_new + CONFIG_ENV_SIZE,
173 up_data, saved_data);
wdenk47cd00f2003-03-06 13:39:27 +0000174 }
175#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200176 puts("Erasing Flash...");
Igor Grinberg82b54b92011-11-07 01:14:09 +0000177 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000178
Igor Grinberg82b54b92011-11-07 01:14:09 +0000179 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200180 goto done;
wdenkc6097192002-11-03 00:24:07 +0000181
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200182 puts("Writing to Flash... ");
183 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000184 (ulong)&(flash_addr_new->data),
Igor Grinberg82b54b92011-11-07 01:14:09 +0000185 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
186 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
187 sizeof(env_new));
188 if (rc)
189 goto perror;
190
191 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
192 sizeof(flash_addr->flags));
193 if (rc)
194 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000195
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200196#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenk47cd00f2003-03-06 13:39:27 +0000197 if (up_data) { /* restore the rest of sector */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200198 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
199 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk8bde7f72003-06-27 21:31:46 +0000200 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200201 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000202 up_data))
203 goto perror;
wdenk47cd00f2003-03-06 13:39:27 +0000204 }
205#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200206 puts("done\n");
207
wdenkc6097192002-11-03 00:24:07 +0000208 {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000209 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000210 ulong ltmp = end_addr;
211
212 flash_addr = flash_addr_new;
213 flash_addr_new = etmp;
214
215 end_addr = end_addr_new;
216 end_addr_new = ltmp;
217 }
218
219 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000220 goto done;
221perror:
222 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200223done:
wdenk47cd00f2003-03-06 13:39:27 +0000224 if (saved_data)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200225 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000226 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000227 flash_sect_protect(1, (ulong)flash_addr, end_addr);
228 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000229
230 return rc;
231}
232#endif /* CMD_SAVEENV */
233
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200234#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000235
Igor Grinberg82b54b92011-11-07 01:14:09 +0000236int env_init(void)
wdenkc6097192002-11-03 00:24:07 +0000237{
wdenkc6097192002-11-03 00:24:07 +0000238 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000239 gd->env_addr = (ulong)&(env_ptr->data);
240 gd->env_valid = 1;
241 return 0;
wdenkc6097192002-11-03 00:24:07 +0000242 }
Dirk Behme9bb8b202007-08-20 07:09:05 +0200243
Igor Grinberg82b54b92011-11-07 01:14:09 +0000244 gd->env_addr = (ulong)&default_environment[0];
245 gd->env_valid = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200246 return 0;
wdenkc6097192002-11-03 00:24:07 +0000247}
248
249#ifdef CMD_SAVEENV
wdenkc6097192002-11-03 00:24:07 +0000250int saveenv(void)
251{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252 env_t env_new;
253 ssize_t len;
254 int rc = 1;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000255 char *res, *saved_data = NULL;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
257 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000258
Igor Grinberg82b54b92011-11-07 01:14:09 +0000259 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200260 debug("Data to save 0x%lx\n", up_data);
261 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000262 saved_data = malloc(up_data);
263 if (saved_data == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 printf("Unable to save the rest of sector (%ld)\n",
265 up_data);
266 goto done;
267 }
268 memcpy(saved_data,
269 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
270 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
271 (ulong)flash_addr + CONFIG_ENV_SIZE,
272 up_data,
273 (ulong)saved_data);
wdenkc6097192002-11-03 00:24:07 +0000274 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200275#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000276
Igor Grinberg82b54b92011-11-07 01:14:09 +0000277 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200278
279 if (flash_sect_protect(0, (long)flash_addr, end_addr))
280 goto done;
281
282 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100283 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200284 if (len < 0) {
285 error("Cannot export environment: errno = %d\n", errno);
286 goto done;
287 }
288 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
289
290 puts("Erasing Flash...");
291 if (flash_sect_erase((long)flash_addr, end_addr))
292 goto done;
293
294 puts("Writing to Flash... ");
295 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg82b54b92011-11-07 01:14:09 +0000296 if (rc != 0)
297 goto perror;
298
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200299#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
300 if (up_data) { /* restore the rest of sector */
301 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
302 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
303 if (flash_write(saved_data,
304 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000305 up_data))
306 goto perror;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200307 }
308#endif
309 puts("done\n");
310 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000311 goto done;
312perror:
313 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200314done:
315 if (saved_data)
316 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000317 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000318 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200319 return rc;
wdenkc6097192002-11-03 00:24:07 +0000320}
wdenkc6097192002-11-03 00:24:07 +0000321#endif /* CMD_SAVEENV */
322
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200323#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000324
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200325void env_relocate_spec(void)
wdenkc6097192002-11-03 00:24:07 +0000326{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200327#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc3f9d492004-03-14 00:59:59 +0000328 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200329 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000330 ulong ltmp = end_addr;
331
332 flash_addr = flash_addr_new;
333 flash_addr_new = etmp;
334
335 end_addr = end_addr_new;
336 end_addr_new = ltmp;
337 }
338
wdenkc3f9d492004-03-14 00:59:59 +0000339 if (flash_addr_new->flags != OBSOLETE_FLAG &&
Igor Grinberg82b54b92011-11-07 01:14:09 +0000340 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
wdenkc3f9d492004-03-14 00:59:59 +0000341 char flag = OBSOLETE_FLAG;
342
wdenkc6097192002-11-03 00:24:07 +0000343 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200344 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkc3f9d492004-03-14 00:59:59 +0000345 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000346 (ulong)&(flash_addr_new->flags),
347 sizeof(flash_addr_new->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200348 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000349 }
350
wdenkc3f9d492004-03-14 00:59:59 +0000351 if (flash_addr->flags != ACTIVE_FLAG &&
352 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
353 char flag = ACTIVE_FLAG;
354
wdenkc6097192002-11-03 00:24:07 +0000355 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200356 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkc3f9d492004-03-14 00:59:59 +0000357 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000358 (ulong)&(flash_addr->flags),
359 sizeof(flash_addr->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200360 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000361 }
362
363 if (gd->env_valid == 2)
Igor Grinberg82b54b92011-11-07 01:14:09 +0000364 puts("*** Warning - some problems detected "
365 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200366#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200367
368 env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000369}