blob: b487e6701ef82632372cbff55f307362112ceb09 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
wdenkc6097192002-11-03 00:24:07 +00008 */
9
10/* #define DEBUG */
11
12#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <command.h>
Simon Glassdb197012019-08-01 09:47:04 -060014#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060015#include <env_internal.h>
wdenkc6097192002-11-03 00:24:07 +000016#include <linux/stddef.h>
wdenk47cd00f2003-03-06 13:39:27 +000017#include <malloc.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Simon Glass3db71102019-11-14 12:57:16 -070020#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000021
Wolfgang Denkd87080b2006-03-31 18:32:53 +020022DECLARE_GLOBAL_DATA_PTR;
23
Simon Glass4415f1d2017-08-03 12:21:58 -060024#ifndef CONFIG_SPL_BUILD
25# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
26# define CMD_SAVEENV
27# elif defined(CONFIG_ENV_ADDR_REDUND)
28# error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
29# endif
wdenkc6097192002-11-03 00:24:07 +000030#endif
31
Simon Glass4415f1d2017-08-03 12:21:58 -060032/* TODO(sjg@chromium.org): Figure out all these special cases */
33#if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
34 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
35 !defined(CONFIG_TARGET_EDMINIV2)) || \
36 !defined(CONFIG_SPL_BUILD)
37#define LOADENV
38#endif
39
40#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
41#define INITENV
42#endif
43
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000044#if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
45 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
wdenkc6097192002-11-03 00:24:07 +000046#ifdef ENV_IS_EMBEDDED
Simon Glassbe54ec12019-08-01 09:47:03 -060047static env_t *env_ptr = &embedded_environment;
wdenkc6097192002-11-03 00:24:07 +000048#else /* ! ENV_IS_EMBEDDED */
49
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000050static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000051#endif /* ENV_IS_EMBEDDED */
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000052#endif
53static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000054
Wolfgang Denkea882ba2010-06-20 23:33:59 +020055/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
Simon Glasse5bce242017-08-03 12:22:01 -060056static ulong __maybe_unused end_addr =
57 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020059#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glasse5bce242017-08-03 12:22:01 -060060
61static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
wdenkc6097192002-11-03 00:24:07 +000062
Wolfgang Denkea882ba2010-06-20 23:33:59 +020063/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Simon Glasse5bce242017-08-03 12:22:01 -060064static ulong __maybe_unused end_addr_new =
65 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020066#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000067
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020068#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060069#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -060070static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +000071{
wdenk8ed96042005-01-09 23:16:25 +000072 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000073
74 uchar flag1 = flash_addr->flags;
75 uchar flag2 = flash_addr_new->flags;
76
77 ulong addr_default = (ulong)&default_environment[0];
78 ulong addr1 = (ulong)&(flash_addr->data);
79 ulong addr2 = (ulong)&(flash_addr_new->data);
80
Igor Grinberg82b54b92011-11-07 01:14:09 +000081 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
82 crc2_ok =
83 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenk8ed96042005-01-09 23:16:25 +000084
Igor Grinberg82b54b92011-11-07 01:14:09 +000085 if (crc1_ok && !crc2_ok) {
86 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -060087 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +000088 } else if (!crc1_ok && crc2_ok) {
89 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -060090 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +000091 } else if (!crc1_ok && !crc2_ok) {
92 gd->env_addr = addr_default;
Simon Glass2d7cb5b2017-08-20 04:45:15 -060093 gd->env_valid = ENV_INVALID;
Simon Glassd3716dd2019-08-01 09:47:08 -060094 } else if (flag1 == ENV_REDUND_ACTIVE &&
95 flag2 == ENV_REDUND_OBSOLETE) {
Igor Grinberg82b54b92011-11-07 01:14:09 +000096 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -060097 gd->env_valid = ENV_VALID;
Simon Glassd3716dd2019-08-01 09:47:08 -060098 } else if (flag1 == ENV_REDUND_OBSOLETE &&
99 flag2 == ENV_REDUND_ACTIVE) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000100 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -0600101 gd->env_valid = ENV_VALID;
wdenkc3f9d492004-03-14 00:59:59 +0000102 } else if (flag1 == flag2) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000103 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -0600104 gd->env_valid = ENV_REDUND;
wdenkc3f9d492004-03-14 00:59:59 +0000105 } else if (flag1 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000106 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -0600107 gd->env_valid = ENV_REDUND;
wdenkc3f9d492004-03-14 00:59:59 +0000108 } else if (flag2 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000109 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -0600110 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000111 }
112
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200113 return 0;
wdenkc6097192002-11-03 00:24:07 +0000114}
Simon Glass4415f1d2017-08-03 12:21:58 -0600115#endif
wdenkc6097192002-11-03 00:24:07 +0000116
117#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600118static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000119{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200120 env_t env_new;
Marek Vasut7ce15262014-03-05 19:59:50 +0100121 char *saved_data = NULL;
Simon Glassd3716dd2019-08-01 09:47:08 -0600122 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000123 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200124#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200125 ulong up_data = 0;
wdenk500545c2003-03-06 14:23:06 +0000126#endif
wdenkc6097192002-11-03 00:24:07 +0000127
Igor Grinberg82b54b92011-11-07 01:14:09 +0000128 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000129
Igor Grinberg82b54b92011-11-07 01:14:09 +0000130 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200131 goto done;
wdenkc6097192002-11-03 00:24:07 +0000132
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200133 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000134 (ulong)flash_addr_new, end_addr_new);
135
Igor Grinberg82b54b92011-11-07 01:14:09 +0000136 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200137 goto done;
wdenkc6097192002-11-03 00:24:07 +0000138
Marek Vasut7ce15262014-03-05 19:59:50 +0100139 rc = env_export(&env_new);
140 if (rc)
141 return rc;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000142 env_new.flags = new_flag;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200143
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200144#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg82b54b92011-11-07 01:14:09 +0000145 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200146 debug("Data to save 0x%lX\n", up_data);
wdenk47cd00f2003-03-06 13:39:27 +0000147 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000148 saved_data = malloc(up_data);
149 if (saved_data == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000150 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000151 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200152 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000153 }
wdenk8bde7f72003-06-27 21:31:46 +0000154 memcpy(saved_data,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000155 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
156 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200157 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
158 (long)flash_addr_new + CONFIG_ENV_SIZE,
159 up_data, saved_data);
wdenk47cd00f2003-03-06 13:39:27 +0000160 }
161#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200162 puts("Erasing Flash...");
Igor Grinberg82b54b92011-11-07 01:14:09 +0000163 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000164
Igor Grinberg82b54b92011-11-07 01:14:09 +0000165 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200166 goto done;
wdenkc6097192002-11-03 00:24:07 +0000167
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200168 puts("Writing to Flash... ");
169 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000170 (ulong)&(flash_addr_new->data),
Igor Grinberg82b54b92011-11-07 01:14:09 +0000171 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
172 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
173 sizeof(env_new));
174 if (rc)
175 goto perror;
176
177 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
178 sizeof(flash_addr->flags));
179 if (rc)
180 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000181
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200182#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenk47cd00f2003-03-06 13:39:27 +0000183 if (up_data) { /* restore the rest of sector */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200184 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
185 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk8bde7f72003-06-27 21:31:46 +0000186 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200187 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000188 up_data))
189 goto perror;
wdenk47cd00f2003-03-06 13:39:27 +0000190 }
191#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200192 puts("done\n");
193
wdenkc6097192002-11-03 00:24:07 +0000194 {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000195 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000196 ulong ltmp = end_addr;
197
198 flash_addr = flash_addr_new;
199 flash_addr_new = etmp;
200
201 end_addr = end_addr_new;
202 end_addr_new = ltmp;
203 }
204
205 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000206 goto done;
207perror:
208 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200209done:
wdenk47cd00f2003-03-06 13:39:27 +0000210 if (saved_data)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200211 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000212 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000213 flash_sect_protect(1, (ulong)flash_addr, end_addr);
214 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000215
216 return rc;
217}
218#endif /* CMD_SAVEENV */
219
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200220#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000221
Simon Glass4415f1d2017-08-03 12:21:58 -0600222#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -0600223static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +0000224{
wdenkc6097192002-11-03 00:24:07 +0000225 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000226 gd->env_addr = (ulong)&(env_ptr->data);
Simon Glass203e94f2017-08-03 12:21:56 -0600227 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000228 return 0;
wdenkc6097192002-11-03 00:24:07 +0000229 }
Dirk Behme9bb8b202007-08-20 07:09:05 +0200230
Igor Grinberg82b54b92011-11-07 01:14:09 +0000231 gd->env_addr = (ulong)&default_environment[0];
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600232 gd->env_valid = ENV_INVALID;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200233 return 0;
wdenkc6097192002-11-03 00:24:07 +0000234}
Simon Glass4415f1d2017-08-03 12:21:58 -0600235#endif
wdenkc6097192002-11-03 00:24:07 +0000236
237#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600238static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000239{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200240 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200241 int rc = 1;
Tom Rini3fa19812014-04-17 16:20:39 -0400242 char *saved_data = NULL;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200243#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
244 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000245
Igor Grinberg82b54b92011-11-07 01:14:09 +0000246 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247 debug("Data to save 0x%lx\n", up_data);
248 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000249 saved_data = malloc(up_data);
250 if (saved_data == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200251 printf("Unable to save the rest of sector (%ld)\n",
252 up_data);
253 goto done;
254 }
255 memcpy(saved_data,
256 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
257 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
258 (ulong)flash_addr + CONFIG_ENV_SIZE,
259 up_data,
260 (ulong)saved_data);
wdenkc6097192002-11-03 00:24:07 +0000261 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200262#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000263
Igor Grinberg82b54b92011-11-07 01:14:09 +0000264 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200265
266 if (flash_sect_protect(0, (long)flash_addr, end_addr))
267 goto done;
268
Marek Vasut7ce15262014-03-05 19:59:50 +0100269 rc = env_export(&env_new);
270 if (rc)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200271 goto done;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200272
273 puts("Erasing Flash...");
274 if (flash_sect_erase((long)flash_addr, end_addr))
275 goto done;
276
277 puts("Writing to Flash... ");
278 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg82b54b92011-11-07 01:14:09 +0000279 if (rc != 0)
280 goto perror;
281
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200282#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
283 if (up_data) { /* restore the rest of sector */
284 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
285 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
286 if (flash_write(saved_data,
287 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000288 up_data))
289 goto perror;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200290 }
291#endif
292 puts("done\n");
293 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000294 goto done;
295perror:
296 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200297done:
298 if (saved_data)
299 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000300 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000301 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200302 return rc;
wdenkc6097192002-11-03 00:24:07 +0000303}
wdenkc6097192002-11-03 00:24:07 +0000304#endif /* CMD_SAVEENV */
305
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200306#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000307
Simon Glasse5bce242017-08-03 12:22:01 -0600308#ifdef LOADENV
Simon Glassc5951992017-08-03 12:22:17 -0600309static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000310{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200311#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc3f9d492004-03-14 00:59:59 +0000312 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200313 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000314 ulong ltmp = end_addr;
315
316 flash_addr = flash_addr_new;
317 flash_addr_new = etmp;
318
319 end_addr = end_addr_new;
320 end_addr_new = ltmp;
321 }
322
Simon Glassd3716dd2019-08-01 09:47:08 -0600323 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
Igor Grinberg82b54b92011-11-07 01:14:09 +0000324 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
Simon Glassd3716dd2019-08-01 09:47:08 -0600325 char flag = ENV_REDUND_OBSOLETE;
wdenkc3f9d492004-03-14 00:59:59 +0000326
Simon Glass203e94f2017-08-03 12:21:56 -0600327 gd->env_valid = ENV_REDUND;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200328 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkc3f9d492004-03-14 00:59:59 +0000329 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000330 (ulong)&(flash_addr_new->flags),
331 sizeof(flash_addr_new->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200332 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000333 }
334
Simon Glassd3716dd2019-08-01 09:47:08 -0600335 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
336 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
337 char flag = ENV_REDUND_ACTIVE;
wdenkc3f9d492004-03-14 00:59:59 +0000338
Simon Glass203e94f2017-08-03 12:21:56 -0600339 gd->env_valid = ENV_REDUND;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200340 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkc3f9d492004-03-14 00:59:59 +0000341 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000342 (ulong)&(flash_addr->flags),
343 sizeof(flash_addr->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200344 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000345 }
346
Simon Glass203e94f2017-08-03 12:21:56 -0600347 if (gd->env_valid == ENV_REDUND)
Igor Grinberg82b54b92011-11-07 01:14:09 +0000348 puts("*** Warning - some problems detected "
349 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200350#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200351
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100352 return env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000353}
Simon Glasse5bce242017-08-03 12:22:01 -0600354#endif /* LOADENV */
Simon Glass4415f1d2017-08-03 12:21:58 -0600355
356U_BOOT_ENV_LOCATION(flash) = {
357 .location = ENVL_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600358 ENV_NAME("Flash")
Simon Glass4415f1d2017-08-03 12:21:58 -0600359#ifdef LOADENV
Simon Glasse5bce242017-08-03 12:22:01 -0600360 .load = env_flash_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600361#endif
362#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600363 .save = env_save_ptr(env_flash_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600364#endif
365#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -0600366 .init = env_flash_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600367#endif
368};