blob: 0f7393d830c1761726bdb7896f655cead9176d06 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Wolfgang Denk460a9ff2010-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
wdenkc6097192002-11-03 00:24:07 +000012#include <command.h>
Simon Glassbf864882019-08-01 09:47:04 -060013#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060014#include <env_internal.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000017#include <linux/stddef.h>
wdenkcc1e2562003-03-06 13:39:27 +000018#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020019#include <search.h>
20#include <errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070021#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000022
Wolfgang Denk6405a152006-03-31 18:32:53 +020023DECLARE_GLOBAL_DATA_PTR;
24
Simon Glass0e84d962024-09-29 19:49:50 -060025#ifndef CONFIG_XPL_BUILD
Simon Glassc10a88e2017-08-03 12:21:58 -060026# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
Tom Rini063c9382022-07-23 13:05:03 -040027# include <flash.h>
Simon Glassc10a88e2017-08-03 12:21:58 -060028# define CMD_SAVEENV
29# elif defined(CONFIG_ENV_ADDR_REDUND)
30# error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
31# endif
wdenkc6097192002-11-03 00:24:07 +000032#endif
33
Simon Glassc10a88e2017-08-03 12:21:58 -060034/* TODO(sjg@chromium.org): Figure out all these special cases */
35#if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
36 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
37 !defined(CONFIG_TARGET_EDMINIV2)) || \
Simon Glass0e84d962024-09-29 19:49:50 -060038 !defined(CONFIG_XPL_BUILD)
Simon Glassc10a88e2017-08-03 12:21:58 -060039#define LOADENV
40#endif
41
Simon Glass0e84d962024-09-29 19:49:50 -060042#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_XPL_BUILD)
Simon Glassc10a88e2017-08-03 12:21:58 -060043#define INITENV
44#endif
45
Rajesh Bhagat0e9fe162018-11-05 18:01:10 +000046#if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
47 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
wdenkc6097192002-11-03 00:24:07 +000048#ifdef ENV_IS_EMBEDDED
Simon Glass204eb222019-08-01 09:47:03 -060049static env_t *env_ptr = &embedded_environment;
wdenkc6097192002-11-03 00:24:07 +000050#else /* ! ENV_IS_EMBEDDED */
51
Rajesh Bhagat0e9fe162018-11-05 18:01:10 +000052static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000053#endif /* ENV_IS_EMBEDDED */
Rajesh Bhagat0e9fe162018-11-05 18:01:10 +000054#endif
55static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000056
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020057/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
Simon Glass082af922017-08-03 12:22:01 -060058static ulong __maybe_unused end_addr =
59 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020060
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020061#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass082af922017-08-03 12:22:01 -060062
63static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
wdenkc6097192002-11-03 00:24:07 +000064
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020065/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Simon Glass082af922017-08-03 12:22:01 -060066static ulong __maybe_unused end_addr_new =
67 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020068#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000069
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020070#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glassc10a88e2017-08-03 12:21:58 -060071#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -060072static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +000073{
wdenkf8062712005-01-09 23:16:25 +000074 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000075
76 uchar flag1 = flash_addr->flags;
77 uchar flag2 = flash_addr_new->flags;
78
wdenkc6097192002-11-03 00:24:07 +000079 ulong addr1 = (ulong)&(flash_addr->data);
80 ulong addr2 = (ulong)&(flash_addr_new->data);
81
Igor Grinberg21821052011-11-07 01:14:09 +000082 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
83 crc2_ok =
84 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenkf8062712005-01-09 23:16:25 +000085
Igor Grinberg21821052011-11-07 01:14:09 +000086 if (crc1_ok && !crc2_ok) {
87 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -060088 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +000089 } else if (!crc1_ok && crc2_ok) {
90 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -060091 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +000092 } else if (!crc1_ok && !crc2_ok) {
Simon Glass1f39d0b2017-08-20 04:45:15 -060093 gd->env_valid = ENV_INVALID;
Simon Glass4686d8f2019-08-01 09:47:08 -060094 } else if (flag1 == ENV_REDUND_ACTIVE &&
95 flag2 == ENV_REDUND_OBSOLETE) {
Igor Grinberg21821052011-11-07 01:14:09 +000096 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -060097 gd->env_valid = ENV_VALID;
Simon Glass4686d8f2019-08-01 09:47:08 -060098 } else if (flag1 == ENV_REDUND_OBSOLETE &&
99 flag2 == ENV_REDUND_ACTIVE) {
Igor Grinberg21821052011-11-07 01:14:09 +0000100 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600101 gd->env_valid = ENV_VALID;
wdenkacd9b102004-03-14 00:59:59 +0000102 } else if (flag1 == flag2) {
Igor Grinberg21821052011-11-07 01:14:09 +0000103 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600104 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000105 } else if (flag1 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000106 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600107 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000108 } else if (flag2 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000109 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600110 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000111 }
112
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200113 return 0;
wdenkc6097192002-11-03 00:24:07 +0000114}
Simon Glassc10a88e2017-08-03 12:21:58 -0600115#endif
wdenkc6097192002-11-03 00:24:07 +0000116
117#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600118static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000119{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200120 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +0100121 char *saved_data = NULL;
Simon Glass4686d8f2019-08-01 09:47:08 -0600122 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
Igor Grinberg21821052011-11-07 01:14:09 +0000123 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200124#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200125 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000126#endif
wdenkc6097192002-11-03 00:24:07 +0000127
Igor Grinberg21821052011-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 Grinberg21821052011-11-07 01:14:09 +0000130 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200131 goto done;
wdenkc6097192002-11-03 00:24:07 +0000132
Wolfgang Denk460a9ff2010-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 Grinberg21821052011-11-07 01:14:09 +0000136 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200137 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200138
Marek Vasutd73c1292014-03-05 19:59:50 +0100139 rc = env_export(&env_new);
140 if (rc)
141 return rc;
Igor Grinberg21821052011-11-07 01:14:09 +0000142 env_new.flags = new_flag;
wdenkc6097192002-11-03 00:24:07 +0000143
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200144#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg21821052011-11-07 01:14:09 +0000145 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200146 debug("Data to save 0x%lX\n", up_data);
wdenkcc1e2562003-03-06 13:39:27 +0000147 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000148 saved_data = malloc(up_data);
149 if (saved_data == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000150 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000151 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200152 goto done;
wdenkcc1e2562003-03-06 13:39:27 +0000153 }
wdenk57b2d802003-06-27 21:31:46 +0000154 memcpy(saved_data,
Igor Grinberg21821052011-11-07 01:14:09 +0000155 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
156 up_data);
Wolfgang Denk460a9ff2010-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);
wdenkcc1e2562003-03-06 13:39:27 +0000160 }
161#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200162 puts("Erasing Flash...");
Igor Grinberg21821052011-11-07 01:14:09 +0000163 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000164
Igor Grinberg21821052011-11-07 01:14:09 +0000165 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200166 goto done;
wdenkc6097192002-11-03 00:24:07 +0000167
Wolfgang Denk460a9ff2010-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 Grinberg21821052011-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-VILLARD7e1cda62008-09-10 22:48:06 +0200182#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000183 if (up_data) { /* restore the rest of sector */
Wolfgang Denk460a9ff2010-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);
wdenk57b2d802003-06-27 21:31:46 +0000186 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200187 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000188 up_data))
189 goto perror;
wdenkcc1e2562003-03-06 13:39:27 +0000190 }
191#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200192 puts("done\n");
193
wdenkc6097192002-11-03 00:24:07 +0000194 {
Igor Grinberg21821052011-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 Grinberg21821052011-11-07 01:14:09 +0000206 goto done;
207perror:
208 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200209done:
Heinrich Schuchardta5a378b2021-10-26 00:41:59 +0200210 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000211 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000212 flash_sect_protect(1, (ulong)flash_addr, end_addr);
213 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000214
215 return rc;
216}
217#endif /* CMD_SAVEENV */
218
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200219#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000220
Simon Glassc10a88e2017-08-03 12:21:58 -0600221#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600222static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +0000223{
wdenkc6097192002-11-03 00:24:07 +0000224 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg21821052011-11-07 01:14:09 +0000225 gd->env_addr = (ulong)&(env_ptr->data);
Simon Glass4bc2ad22017-08-03 12:21:56 -0600226 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +0000227 return 0;
wdenkc6097192002-11-03 00:24:07 +0000228 }
Dirk Behmeafa1dd52007-08-20 07:09:05 +0200229
Marek Behúnc27f9a12021-10-22 15:47:21 +0200230 gd->env_valid = ENV_INVALID;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200231 return 0;
wdenkc6097192002-11-03 00:24:07 +0000232}
Simon Glassc10a88e2017-08-03 12:21:58 -0600233#endif
wdenkc6097192002-11-03 00:24:07 +0000234
235#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600236static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000237{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200238 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200239 int rc = 1;
Tom Rinicfbdf562014-04-17 16:20:39 -0400240 char *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200241#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
242 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000243
Igor Grinberg21821052011-11-07 01:14:09 +0000244 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200245 debug("Data to save 0x%lx\n", up_data);
246 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000247 saved_data = malloc(up_data);
248 if (saved_data == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200249 printf("Unable to save the rest of sector (%ld)\n",
250 up_data);
251 goto done;
252 }
253 memcpy(saved_data,
254 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
255 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
256 (ulong)flash_addr + CONFIG_ENV_SIZE,
257 up_data,
258 (ulong)saved_data);
259 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200260#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000261
Igor Grinberg21821052011-11-07 01:14:09 +0000262 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000263
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200264 if (flash_sect_protect(0, (long)flash_addr, end_addr))
265 goto done;
wdenkc6097192002-11-03 00:24:07 +0000266
Marek Vasutd73c1292014-03-05 19:59:50 +0100267 rc = env_export(&env_new);
268 if (rc)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 goto done;
wdenkc6097192002-11-03 00:24:07 +0000270
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200271 puts("Erasing Flash...");
272 if (flash_sect_erase((long)flash_addr, end_addr))
273 goto done;
wdenkc6097192002-11-03 00:24:07 +0000274
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200275 puts("Writing to Flash... ");
276 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg21821052011-11-07 01:14:09 +0000277 if (rc != 0)
278 goto perror;
279
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200280#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
281 if (up_data) { /* restore the rest of sector */
282 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
283 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
284 if (flash_write(saved_data,
285 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000286 up_data))
287 goto perror;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200288 }
289#endif
290 puts("done\n");
291 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000292 goto done;
293perror:
294 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200295done:
Heinrich Schuchardta5a378b2021-10-26 00:41:59 +0200296 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000297 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000298 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200299 return rc;
wdenkc6097192002-11-03 00:24:07 +0000300}
wdenkc6097192002-11-03 00:24:07 +0000301#endif /* CMD_SAVEENV */
302
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200303#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000304
Simon Glass082af922017-08-03 12:22:01 -0600305#ifdef LOADENV
Simon Glass99778492017-08-03 12:22:17 -0600306static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000307{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200308#ifdef CONFIG_ENV_ADDR_REDUND
wdenkacd9b102004-03-14 00:59:59 +0000309 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200310 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000311 ulong ltmp = end_addr;
312
313 flash_addr = flash_addr_new;
314 flash_addr_new = etmp;
315
316 end_addr = end_addr_new;
317 end_addr_new = ltmp;
318 }
319
Simon Glass4686d8f2019-08-01 09:47:08 -0600320 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
Igor Grinberg21821052011-11-07 01:14:09 +0000321 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
Simon Glass4686d8f2019-08-01 09:47:08 -0600322 char flag = ENV_REDUND_OBSOLETE;
wdenkacd9b102004-03-14 00:59:59 +0000323
Simon Glass4bc2ad22017-08-03 12:21:56 -0600324 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200325 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000326 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000327 (ulong)&(flash_addr_new->flags),
328 sizeof(flash_addr_new->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200329 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000330 }
331
Simon Glass4686d8f2019-08-01 09:47:08 -0600332 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
333 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
334 char flag = ENV_REDUND_ACTIVE;
wdenkacd9b102004-03-14 00:59:59 +0000335
Simon Glass4bc2ad22017-08-03 12:21:56 -0600336 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200337 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000338 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000339 (ulong)&(flash_addr->flags),
340 sizeof(flash_addr->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200341 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000342 }
343
Simon Glass4bc2ad22017-08-03 12:21:56 -0600344 if (gd->env_valid == ENV_REDUND)
Igor Grinberg21821052011-11-07 01:14:09 +0000345 puts("*** Warning - some problems detected "
346 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200347#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200348
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200349 return env_import((char *)flash_addr, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +0000350}
Simon Glass082af922017-08-03 12:22:01 -0600351#endif /* LOADENV */
Simon Glassc10a88e2017-08-03 12:21:58 -0600352
353U_BOOT_ENV_LOCATION(flash) = {
354 .location = ENVL_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600355 ENV_NAME("Flash")
Simon Glassc10a88e2017-08-03 12:21:58 -0600356#ifdef LOADENV
Simon Glass082af922017-08-03 12:22:01 -0600357 .load = env_flash_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600358#endif
359#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600360 .save = env_save_ptr(env_flash_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600361#endif
362#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600363 .init = env_flash_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600364#endif
365};