blob: e07d336a48b9940bc0987255554f68ff27fd52cd [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denk460a9ff2010-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>
wdenkcc1e2562003-03-06 13:39:27 +000033#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020034#include <search.h>
35#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000036
Wolfgang Denk6405a152006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
Mike Frysinger78dcaf42009-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-VILLARD7e1cda62008-09-10 22:48:06 +020041#elif defined(CONFIG_ENV_ADDR_REDUND)
Igor Grinberg21821052011-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 Grinberg21821052011-11-07 01:14:09 +000045#if defined(CONFIG_ENV_SIZE_REDUND) && \
46 (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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 Grinberg21821052011-11-07 01:14:09 +000050char *env_name_spec = "Flash";
wdenkc6097192002-11-03 00:24:07 +000051
52#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000053env_t *env_ptr = &environment;
wdenkc6097192002-11-03 00:24:07 +000054
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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-VILLARD7e1cda62008-09-10 22:48:06 +020059env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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 Denk460a9ff2010-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-VILLARD7e1cda62008-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 Denk460a9ff2010-06-20 23:33:59 +020071/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020072static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020076#ifdef CONFIG_ENV_ADDR_REDUND
Igor Grinberg21821052011-11-07 01:14:09 +000077int env_init(void)
wdenkc6097192002-11-03 00:24:07 +000078{
wdenkf8062712005-01-09 23:16:25 +000079 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000080
81 uchar flag1 = flash_addr->flags;
82 uchar flag2 = flash_addr_new->flags;
83
84 ulong addr_default = (ulong)&default_environment[0];
85 ulong addr1 = (ulong)&(flash_addr->data);
86 ulong addr2 = (ulong)&(flash_addr_new->data);
87
Igor Grinberg21821052011-11-07 01:14:09 +000088 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
89 crc2_ok =
90 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenkf8062712005-01-09 23:16:25 +000091
Igor Grinberg21821052011-11-07 01:14:09 +000092 if (crc1_ok && !crc2_ok) {
93 gd->env_addr = addr1;
94 gd->env_valid = 1;
95 } else if (!crc1_ok && crc2_ok) {
96 gd->env_addr = addr2;
97 gd->env_valid = 1;
98 } else if (!crc1_ok && !crc2_ok) {
99 gd->env_addr = addr_default;
100 gd->env_valid = 0;
wdenkacd9b102004-03-14 00:59:59 +0000101 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
Igor Grinberg21821052011-11-07 01:14:09 +0000102 gd->env_addr = addr1;
103 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000104 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
Igor Grinberg21821052011-11-07 01:14:09 +0000105 gd->env_addr = addr2;
106 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000107 } else if (flag1 == flag2) {
Igor Grinberg21821052011-11-07 01:14:09 +0000108 gd->env_addr = addr1;
109 gd->env_valid = 2;
wdenkacd9b102004-03-14 00:59:59 +0000110 } else if (flag1 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000111 gd->env_addr = addr1;
112 gd->env_valid = 2;
wdenkacd9b102004-03-14 00:59:59 +0000113 } else if (flag2 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000114 gd->env_addr = addr2;
115 gd->env_valid = 2;
wdenkc6097192002-11-03 00:24:07 +0000116 }
117
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200118 return 0;
wdenkc6097192002-11-03 00:24:07 +0000119}
120
121#ifdef CMD_SAVEENV
122int saveenv(void)
123{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200124 env_t env_new;
125 ssize_t len;
Igor Grinberg21821052011-11-07 01:14:09 +0000126 char *res, *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200127 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Igor Grinberg21821052011-11-07 01:14:09 +0000128 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200129#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200130 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000131#endif
wdenkc6097192002-11-03 00:24:07 +0000132
Igor Grinberg21821052011-11-07 01:14:09 +0000133 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000134
Igor Grinberg21821052011-11-07 01:14:09 +0000135 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200136 goto done;
wdenkc6097192002-11-03 00:24:07 +0000137
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200138 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000139 (ulong)flash_addr_new, end_addr_new);
140
Igor Grinberg21821052011-11-07 01:14:09 +0000141 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200142 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200143
144 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600145 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200146 if (len < 0) {
147 error("Cannot export environment: errno = %d\n", errno);
148 goto done;
wdenkc6097192002-11-03 00:24:07 +0000149 }
Igor Grinberg21821052011-11-07 01:14:09 +0000150 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
151 env_new.flags = new_flag;
wdenkc6097192002-11-03 00:24:07 +0000152
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200153#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg21821052011-11-07 01:14:09 +0000154 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200155 debug("Data to save 0x%lX\n", up_data);
wdenkcc1e2562003-03-06 13:39:27 +0000156 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000157 saved_data = malloc(up_data);
158 if (saved_data == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000159 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000160 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200161 goto done;
wdenkcc1e2562003-03-06 13:39:27 +0000162 }
wdenk57b2d802003-06-27 21:31:46 +0000163 memcpy(saved_data,
Igor Grinberg21821052011-11-07 01:14:09 +0000164 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
165 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200166 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
167 (long)flash_addr_new + CONFIG_ENV_SIZE,
168 up_data, saved_data);
wdenkcc1e2562003-03-06 13:39:27 +0000169 }
170#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200171 puts("Erasing Flash...");
Igor Grinberg21821052011-11-07 01:14:09 +0000172 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000173
Igor Grinberg21821052011-11-07 01:14:09 +0000174 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200175 goto done;
wdenkc6097192002-11-03 00:24:07 +0000176
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200177 puts("Writing to Flash... ");
178 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000179 (ulong)&(flash_addr_new->data),
Igor Grinberg21821052011-11-07 01:14:09 +0000180 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
181 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
182 sizeof(env_new));
183 if (rc)
184 goto perror;
185
186 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
187 sizeof(flash_addr->flags));
188 if (rc)
189 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000190
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200191#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000192 if (up_data) { /* restore the rest of sector */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200193 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
194 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk57b2d802003-06-27 21:31:46 +0000195 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200196 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000197 up_data))
198 goto perror;
wdenkcc1e2562003-03-06 13:39:27 +0000199 }
200#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200201 puts("done\n");
202
wdenkc6097192002-11-03 00:24:07 +0000203 {
Igor Grinberg21821052011-11-07 01:14:09 +0000204 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000205 ulong ltmp = end_addr;
206
207 flash_addr = flash_addr_new;
208 flash_addr_new = etmp;
209
210 end_addr = end_addr_new;
211 end_addr_new = ltmp;
212 }
213
214 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000215 goto done;
216perror:
217 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200218done:
wdenkcc1e2562003-03-06 13:39:27 +0000219 if (saved_data)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200220 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000221 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000222 flash_sect_protect(1, (ulong)flash_addr, end_addr);
223 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000224
225 return rc;
226}
227#endif /* CMD_SAVEENV */
228
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200229#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000230
Igor Grinberg21821052011-11-07 01:14:09 +0000231int env_init(void)
wdenkc6097192002-11-03 00:24:07 +0000232{
wdenkc6097192002-11-03 00:24:07 +0000233 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg21821052011-11-07 01:14:09 +0000234 gd->env_addr = (ulong)&(env_ptr->data);
235 gd->env_valid = 1;
236 return 0;
wdenkc6097192002-11-03 00:24:07 +0000237 }
Dirk Behmeafa1dd52007-08-20 07:09:05 +0200238
Igor Grinberg21821052011-11-07 01:14:09 +0000239 gd->env_addr = (ulong)&default_environment[0];
240 gd->env_valid = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200241 return 0;
wdenkc6097192002-11-03 00:24:07 +0000242}
243
244#ifdef CMD_SAVEENV
wdenkc6097192002-11-03 00:24:07 +0000245int saveenv(void)
246{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200247 env_t env_new;
248 ssize_t len;
249 int rc = 1;
Igor Grinberg21821052011-11-07 01:14:09 +0000250 char *res, *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200251#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
252 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000253
Igor Grinberg21821052011-11-07 01:14:09 +0000254 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200255 debug("Data to save 0x%lx\n", up_data);
256 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000257 saved_data = malloc(up_data);
258 if (saved_data == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200259 printf("Unable to save the rest of sector (%ld)\n",
260 up_data);
261 goto done;
262 }
263 memcpy(saved_data,
264 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
265 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
266 (ulong)flash_addr + CONFIG_ENV_SIZE,
267 up_data,
268 (ulong)saved_data);
269 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200270#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000271
Igor Grinberg21821052011-11-07 01:14:09 +0000272 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000273
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200274 if (flash_sect_protect(0, (long)flash_addr, end_addr))
275 goto done;
wdenkc6097192002-11-03 00:24:07 +0000276
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200277 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600278 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200279 if (len < 0) {
280 error("Cannot export environment: errno = %d\n", errno);
281 goto done;
282 }
283 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000284
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200285 puts("Erasing Flash...");
286 if (flash_sect_erase((long)flash_addr, end_addr))
287 goto done;
wdenkc6097192002-11-03 00:24:07 +0000288
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200289 puts("Writing to Flash... ");
290 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg21821052011-11-07 01:14:09 +0000291 if (rc != 0)
292 goto perror;
293
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200294#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
295 if (up_data) { /* restore the rest of sector */
296 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
297 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
298 if (flash_write(saved_data,
299 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000300 up_data))
301 goto perror;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200302 }
303#endif
304 puts("done\n");
305 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000306 goto done;
307perror:
308 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200309done:
310 if (saved_data)
311 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000312 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000313 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200314 return rc;
wdenkc6097192002-11-03 00:24:07 +0000315}
wdenkc6097192002-11-03 00:24:07 +0000316#endif /* CMD_SAVEENV */
317
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200318#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000319
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200320void env_relocate_spec(void)
wdenkc6097192002-11-03 00:24:07 +0000321{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200322#ifdef CONFIG_ENV_ADDR_REDUND
wdenkacd9b102004-03-14 00:59:59 +0000323 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200324 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000325 ulong ltmp = end_addr;
326
327 flash_addr = flash_addr_new;
328 flash_addr_new = etmp;
329
330 end_addr = end_addr_new;
331 end_addr_new = ltmp;
332 }
333
wdenkacd9b102004-03-14 00:59:59 +0000334 if (flash_addr_new->flags != OBSOLETE_FLAG &&
Igor Grinberg21821052011-11-07 01:14:09 +0000335 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
wdenkacd9b102004-03-14 00:59:59 +0000336 char flag = OBSOLETE_FLAG;
337
wdenkc6097192002-11-03 00:24:07 +0000338 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200339 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000340 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000341 (ulong)&(flash_addr_new->flags),
342 sizeof(flash_addr_new->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200343 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000344 }
345
wdenkacd9b102004-03-14 00:59:59 +0000346 if (flash_addr->flags != ACTIVE_FLAG &&
347 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
348 char flag = ACTIVE_FLAG;
349
wdenkc6097192002-11-03 00:24:07 +0000350 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200351 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000352 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000353 (ulong)&(flash_addr->flags),
354 sizeof(flash_addr->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200355 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000356 }
357
358 if (gd->env_valid == 2)
Igor Grinberg21821052011-11-07 01:14:09 +0000359 puts("*** Warning - some problems detected "
360 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200361#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200362
363 env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000364}