blob: 9c8abfa016c401ceb37804920f9d7f175aa3ca17 [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>
Simon Glass0ee482522019-12-28 10:44:40 -070016#include <flash.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000019#include <linux/stddef.h>
wdenk47cd00f2003-03-06 13:39:27 +000020#include <malloc.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020021#include <search.h>
22#include <errno.h>
Simon Glass3db71102019-11-14 12:57:16 -070023#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000024
Wolfgang Denkd87080b2006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass4415f1d2017-08-03 12:21:58 -060027#ifndef CONFIG_SPL_BUILD
28# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
29# define CMD_SAVEENV
30# elif defined(CONFIG_ENV_ADDR_REDUND)
31# error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
32# endif
wdenkc6097192002-11-03 00:24:07 +000033#endif
34
Simon Glass4415f1d2017-08-03 12:21:58 -060035/* TODO(sjg@chromium.org): Figure out all these special cases */
36#if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
37 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
38 !defined(CONFIG_TARGET_EDMINIV2)) || \
39 !defined(CONFIG_SPL_BUILD)
40#define LOADENV
41#endif
42
43#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
44#define INITENV
45#endif
46
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000047#if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
48 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
wdenkc6097192002-11-03 00:24:07 +000049#ifdef ENV_IS_EMBEDDED
Simon Glassbe54ec12019-08-01 09:47:03 -060050static env_t *env_ptr = &embedded_environment;
wdenkc6097192002-11-03 00:24:07 +000051#else /* ! ENV_IS_EMBEDDED */
52
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000053static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000054#endif /* ENV_IS_EMBEDDED */
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000055#endif
56static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000057
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
Simon Glasse5bce242017-08-03 12:22:01 -060059static ulong __maybe_unused end_addr =
60 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020061
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020062#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glasse5bce242017-08-03 12:22:01 -060063
64static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
wdenkc6097192002-11-03 00:24:07 +000065
Wolfgang Denkea882ba2010-06-20 23:33:59 +020066/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Simon Glasse5bce242017-08-03 12:22:01 -060067static ulong __maybe_unused end_addr_new =
68 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020069#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000070
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020071#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060072#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -060073static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +000074{
wdenk8ed96042005-01-09 23:16:25 +000075 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000076
77 uchar flag1 = flash_addr->flags;
78 uchar flag2 = flash_addr_new->flags;
79
wdenkc6097192002-11-03 00:24:07 +000080 ulong addr1 = (ulong)&(flash_addr->data);
81 ulong addr2 = (ulong)&(flash_addr_new->data);
82
Igor Grinberg82b54b92011-11-07 01:14:09 +000083 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
84 crc2_ok =
85 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenk8ed96042005-01-09 23:16:25 +000086
Igor Grinberg82b54b92011-11-07 01:14:09 +000087 if (crc1_ok && !crc2_ok) {
88 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -060089 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +000090 } else if (!crc1_ok && crc2_ok) {
91 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -060092 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +000093 } else if (!crc1_ok && !crc2_ok) {
Simon Glass2d7cb5b2017-08-20 04:45:15 -060094 gd->env_valid = ENV_INVALID;
Simon Glassd3716dd2019-08-01 09:47:08 -060095 } else if (flag1 == ENV_REDUND_ACTIVE &&
96 flag2 == ENV_REDUND_OBSOLETE) {
Igor Grinberg82b54b92011-11-07 01:14:09 +000097 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -060098 gd->env_valid = ENV_VALID;
Simon Glassd3716dd2019-08-01 09:47:08 -060099 } else if (flag1 == ENV_REDUND_OBSOLETE &&
100 flag2 == ENV_REDUND_ACTIVE) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000101 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -0600102 gd->env_valid = ENV_VALID;
wdenkc3f9d492004-03-14 00:59:59 +0000103 } else if (flag1 == flag2) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000104 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -0600105 gd->env_valid = ENV_REDUND;
wdenkc3f9d492004-03-14 00:59:59 +0000106 } else if (flag1 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000107 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -0600108 gd->env_valid = ENV_REDUND;
wdenkc3f9d492004-03-14 00:59:59 +0000109 } else if (flag2 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000110 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -0600111 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000112 }
113
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200114 return 0;
wdenkc6097192002-11-03 00:24:07 +0000115}
Simon Glass4415f1d2017-08-03 12:21:58 -0600116#endif
wdenkc6097192002-11-03 00:24:07 +0000117
118#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600119static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000120{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200121 env_t env_new;
Marek Vasut7ce15262014-03-05 19:59:50 +0100122 char *saved_data = NULL;
Simon Glassd3716dd2019-08-01 09:47:08 -0600123 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000124 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200125#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200126 ulong up_data = 0;
wdenk500545c2003-03-06 14:23:06 +0000127#endif
wdenkc6097192002-11-03 00:24:07 +0000128
Igor Grinberg82b54b92011-11-07 01:14:09 +0000129 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000130
Igor Grinberg82b54b92011-11-07 01:14:09 +0000131 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200132 goto done;
wdenkc6097192002-11-03 00:24:07 +0000133
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200134 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000135 (ulong)flash_addr_new, end_addr_new);
136
Igor Grinberg82b54b92011-11-07 01:14:09 +0000137 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200138 goto done;
wdenkc6097192002-11-03 00:24:07 +0000139
Marek Vasut7ce15262014-03-05 19:59:50 +0100140 rc = env_export(&env_new);
141 if (rc)
142 return rc;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000143 env_new.flags = new_flag;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200144
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200145#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg82b54b92011-11-07 01:14:09 +0000146 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200147 debug("Data to save 0x%lX\n", up_data);
wdenk47cd00f2003-03-06 13:39:27 +0000148 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000149 saved_data = malloc(up_data);
150 if (saved_data == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000151 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000152 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000154 }
wdenk8bde7f72003-06-27 21:31:46 +0000155 memcpy(saved_data,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000156 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
157 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200158 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
159 (long)flash_addr_new + CONFIG_ENV_SIZE,
160 up_data, saved_data);
wdenk47cd00f2003-03-06 13:39:27 +0000161 }
162#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200163 puts("Erasing Flash...");
Igor Grinberg82b54b92011-11-07 01:14:09 +0000164 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000165
Igor Grinberg82b54b92011-11-07 01:14:09 +0000166 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200167 goto done;
wdenkc6097192002-11-03 00:24:07 +0000168
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200169 puts("Writing to Flash... ");
170 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000171 (ulong)&(flash_addr_new->data),
Igor Grinberg82b54b92011-11-07 01:14:09 +0000172 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
173 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
174 sizeof(env_new));
175 if (rc)
176 goto perror;
177
178 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
179 sizeof(flash_addr->flags));
180 if (rc)
181 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000182
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200183#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenk47cd00f2003-03-06 13:39:27 +0000184 if (up_data) { /* restore the rest of sector */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
186 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk8bde7f72003-06-27 21:31:46 +0000187 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200188 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000189 up_data))
190 goto perror;
wdenk47cd00f2003-03-06 13:39:27 +0000191 }
192#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200193 puts("done\n");
194
wdenkc6097192002-11-03 00:24:07 +0000195 {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000196 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000197 ulong ltmp = end_addr;
198
199 flash_addr = flash_addr_new;
200 flash_addr_new = etmp;
201
202 end_addr = end_addr_new;
203 end_addr_new = ltmp;
204 }
205
206 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000207 goto done;
208perror:
209 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200210done:
Heinrich Schuchardt28ab12a2021-10-26 00:41: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
Marek Behúnee483902021-10-22 15:47:21 +0200231 gd->env_valid = ENV_INVALID;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200232 return 0;
wdenkc6097192002-11-03 00:24:07 +0000233}
Simon Glass4415f1d2017-08-03 12:21:58 -0600234#endif
wdenkc6097192002-11-03 00:24:07 +0000235
236#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600237static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000238{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200239 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200240 int rc = 1;
Tom Rini3fa19812014-04-17 16:20:39 -0400241 char *saved_data = NULL;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200242#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
243 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000244
Igor Grinberg82b54b92011-11-07 01:14:09 +0000245 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200246 debug("Data to save 0x%lx\n", up_data);
247 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000248 saved_data = malloc(up_data);
249 if (saved_data == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200250 printf("Unable to save the rest of sector (%ld)\n",
251 up_data);
252 goto done;
253 }
254 memcpy(saved_data,
255 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
256 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
257 (ulong)flash_addr + CONFIG_ENV_SIZE,
258 up_data,
259 (ulong)saved_data);
wdenkc6097192002-11-03 00:24:07 +0000260 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200261#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000262
Igor Grinberg82b54b92011-11-07 01:14:09 +0000263 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264
265 if (flash_sect_protect(0, (long)flash_addr, end_addr))
266 goto done;
267
Marek Vasut7ce15262014-03-05 19:59:50 +0100268 rc = env_export(&env_new);
269 if (rc)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200270 goto done;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200271
272 puts("Erasing Flash...");
273 if (flash_sect_erase((long)flash_addr, end_addr))
274 goto done;
275
276 puts("Writing to Flash... ");
277 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg82b54b92011-11-07 01:14:09 +0000278 if (rc != 0)
279 goto perror;
280
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200281#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
282 if (up_data) { /* restore the rest of sector */
283 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
284 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
285 if (flash_write(saved_data,
286 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000287 up_data))
288 goto perror;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200289 }
290#endif
291 puts("done\n");
292 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000293 goto done;
294perror:
295 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200296done:
Heinrich Schuchardt28ab12a2021-10-26 00:41:59 +0200297 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000298 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000299 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200300 return rc;
wdenkc6097192002-11-03 00:24:07 +0000301}
wdenkc6097192002-11-03 00:24:07 +0000302#endif /* CMD_SAVEENV */
303
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200304#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000305
Simon Glasse5bce242017-08-03 12:22:01 -0600306#ifdef LOADENV
Simon Glassc5951992017-08-03 12:22:17 -0600307static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000308{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200309#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc3f9d492004-03-14 00:59:59 +0000310 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200311 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000312 ulong ltmp = end_addr;
313
314 flash_addr = flash_addr_new;
315 flash_addr_new = etmp;
316
317 end_addr = end_addr_new;
318 end_addr_new = ltmp;
319 }
320
Simon Glassd3716dd2019-08-01 09:47:08 -0600321 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
Igor Grinberg82b54b92011-11-07 01:14:09 +0000322 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
Simon Glassd3716dd2019-08-01 09:47:08 -0600323 char flag = ENV_REDUND_OBSOLETE;
wdenkc3f9d492004-03-14 00:59:59 +0000324
Simon Glass203e94f2017-08-03 12:21:56 -0600325 gd->env_valid = ENV_REDUND;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200326 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkc3f9d492004-03-14 00:59:59 +0000327 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000328 (ulong)&(flash_addr_new->flags),
329 sizeof(flash_addr_new->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200330 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000331 }
332
Simon Glassd3716dd2019-08-01 09:47:08 -0600333 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
334 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
335 char flag = ENV_REDUND_ACTIVE;
wdenkc3f9d492004-03-14 00:59:59 +0000336
Simon Glass203e94f2017-08-03 12:21:56 -0600337 gd->env_valid = ENV_REDUND;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200338 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkc3f9d492004-03-14 00:59:59 +0000339 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000340 (ulong)&(flash_addr->flags),
341 sizeof(flash_addr->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200342 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000343 }
344
Simon Glass203e94f2017-08-03 12:21:56 -0600345 if (gd->env_valid == ENV_REDUND)
Igor Grinberg82b54b92011-11-07 01:14:09 +0000346 puts("*** Warning - some problems detected "
347 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200348#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200349
Marek Vasut890feec2020-07-07 20:51:35 +0200350 return env_import((char *)flash_addr, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +0000351}
Simon Glasse5bce242017-08-03 12:22:01 -0600352#endif /* LOADENV */
Simon Glass4415f1d2017-08-03 12:21:58 -0600353
354U_BOOT_ENV_LOCATION(flash) = {
355 .location = ENVL_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600356 ENV_NAME("Flash")
Simon Glass4415f1d2017-08-03 12:21:58 -0600357#ifdef LOADENV
Simon Glasse5bce242017-08-03 12:22:01 -0600358 .load = env_flash_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600359#endif
360#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600361 .save = env_save_ptr(env_flash_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600362#endif
363#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -0600364 .init = env_flash_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600365#endif
366};