blob: ebee9069e4e3df53c5ceddbce07b91a9c082ee38 [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
12#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <command.h>
Simon Glassbf864882019-08-01 09:47:04 -060014#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060015#include <env_internal.h>
Simon Glassa606ffc2019-12-28 10:44:40 -070016#include <flash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
wdenkc6097192002-11-03 00:24:07 +000019#include <linux/stddef.h>
wdenkcc1e2562003-03-06 13:39:27 +000020#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020021#include <search.h>
22#include <errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070023#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000024
Wolfgang Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glassc10a88e2017-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 Glassc10a88e2017-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 Bhagat0e9fe162018-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 Glass204eb222019-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 Bhagat0e9fe162018-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 Bhagat0e9fe162018-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 Denk460a9ff2010-06-20 23:33:59 +020058/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
Simon Glass082af922017-08-03 12:22:01 -060059static ulong __maybe_unused end_addr =
60 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020061
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020062#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass082af922017-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 Denk460a9ff2010-06-20 23:33:59 +020066/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Simon Glass082af922017-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-VILLARD7e1cda62008-09-10 22:48:06 +020069#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000070
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020071#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glassc10a88e2017-08-03 12:21:58 -060072#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -060073static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +000074{
wdenkf8062712005-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
80 ulong addr_default = (ulong)&default_environment[0];
81 ulong addr1 = (ulong)&(flash_addr->data);
82 ulong addr2 = (ulong)&(flash_addr_new->data);
83
Igor Grinberg21821052011-11-07 01:14:09 +000084 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
85 crc2_ok =
86 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenkf8062712005-01-09 23:16:25 +000087
Igor Grinberg21821052011-11-07 01:14:09 +000088 if (crc1_ok && !crc2_ok) {
89 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -060090 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +000091 } else if (!crc1_ok && crc2_ok) {
92 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -060093 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +000094 } else if (!crc1_ok && !crc2_ok) {
95 gd->env_addr = addr_default;
Simon Glass1f39d0b2017-08-20 04:45:15 -060096 gd->env_valid = ENV_INVALID;
Simon Glass4686d8f2019-08-01 09:47:08 -060097 } else if (flag1 == ENV_REDUND_ACTIVE &&
98 flag2 == ENV_REDUND_OBSOLETE) {
Igor Grinberg21821052011-11-07 01:14:09 +000099 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600100 gd->env_valid = ENV_VALID;
Simon Glass4686d8f2019-08-01 09:47:08 -0600101 } else if (flag1 == ENV_REDUND_OBSOLETE &&
102 flag2 == ENV_REDUND_ACTIVE) {
Igor Grinberg21821052011-11-07 01:14:09 +0000103 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600104 gd->env_valid = ENV_VALID;
wdenkacd9b102004-03-14 00:59:59 +0000105 } else if (flag1 == flag2) {
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 (flag1 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000109 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600110 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000111 } else if (flag2 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000112 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600113 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000114 }
115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 return 0;
wdenkc6097192002-11-03 00:24:07 +0000117}
Simon Glassc10a88e2017-08-03 12:21:58 -0600118#endif
wdenkc6097192002-11-03 00:24:07 +0000119
120#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600121static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000122{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200123 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +0100124 char *saved_data = NULL;
Simon Glass4686d8f2019-08-01 09:47:08 -0600125 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
Igor Grinberg21821052011-11-07 01:14:09 +0000126 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200127#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200128 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000129#endif
wdenkc6097192002-11-03 00:24:07 +0000130
Igor Grinberg21821052011-11-07 01:14:09 +0000131 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000132
Igor Grinberg21821052011-11-07 01:14:09 +0000133 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200134 goto done;
wdenkc6097192002-11-03 00:24:07 +0000135
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200136 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000137 (ulong)flash_addr_new, end_addr_new);
138
Igor Grinberg21821052011-11-07 01:14:09 +0000139 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200140 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200141
Marek Vasutd73c1292014-03-05 19:59:50 +0100142 rc = env_export(&env_new);
143 if (rc)
144 return rc;
Igor Grinberg21821052011-11-07 01:14:09 +0000145 env_new.flags = new_flag;
wdenkc6097192002-11-03 00:24:07 +0000146
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200147#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg21821052011-11-07 01:14:09 +0000148 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200149 debug("Data to save 0x%lX\n", up_data);
wdenkcc1e2562003-03-06 13:39:27 +0000150 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000151 saved_data = malloc(up_data);
152 if (saved_data == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000153 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000154 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200155 goto done;
wdenkcc1e2562003-03-06 13:39:27 +0000156 }
wdenk57b2d802003-06-27 21:31:46 +0000157 memcpy(saved_data,
Igor Grinberg21821052011-11-07 01:14:09 +0000158 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
159 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200160 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
161 (long)flash_addr_new + CONFIG_ENV_SIZE,
162 up_data, saved_data);
wdenkcc1e2562003-03-06 13:39:27 +0000163 }
164#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200165 puts("Erasing Flash...");
Igor Grinberg21821052011-11-07 01:14:09 +0000166 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000167
Igor Grinberg21821052011-11-07 01:14:09 +0000168 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200169 goto done;
wdenkc6097192002-11-03 00:24:07 +0000170
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200171 puts("Writing to Flash... ");
172 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000173 (ulong)&(flash_addr_new->data),
Igor Grinberg21821052011-11-07 01:14:09 +0000174 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
175 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
176 sizeof(env_new));
177 if (rc)
178 goto perror;
179
180 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
181 sizeof(flash_addr->flags));
182 if (rc)
183 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000184
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200185#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000186 if (up_data) { /* restore the rest of sector */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200187 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
188 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk57b2d802003-06-27 21:31:46 +0000189 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200190 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000191 up_data))
192 goto perror;
wdenkcc1e2562003-03-06 13:39:27 +0000193 }
194#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200195 puts("done\n");
196
wdenkc6097192002-11-03 00:24:07 +0000197 {
Igor Grinberg21821052011-11-07 01:14:09 +0000198 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000199 ulong ltmp = end_addr;
200
201 flash_addr = flash_addr_new;
202 flash_addr_new = etmp;
203
204 end_addr = end_addr_new;
205 end_addr_new = ltmp;
206 }
207
208 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000209 goto done;
210perror:
211 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200212done:
wdenkcc1e2562003-03-06 13:39:27 +0000213 if (saved_data)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200214 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000215 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000216 flash_sect_protect(1, (ulong)flash_addr, end_addr);
217 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000218
219 return rc;
220}
221#endif /* CMD_SAVEENV */
222
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200223#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000224
Simon Glassc10a88e2017-08-03 12:21:58 -0600225#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600226static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +0000227{
wdenkc6097192002-11-03 00:24:07 +0000228 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg21821052011-11-07 01:14:09 +0000229 gd->env_addr = (ulong)&(env_ptr->data);
Simon Glass4bc2ad22017-08-03 12:21:56 -0600230 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +0000231 return 0;
wdenkc6097192002-11-03 00:24:07 +0000232 }
Dirk Behmeafa1dd52007-08-20 07:09:05 +0200233
Igor Grinberg21821052011-11-07 01:14:09 +0000234 gd->env_addr = (ulong)&default_environment[0];
Simon Glass1f39d0b2017-08-20 04:45:15 -0600235 gd->env_valid = ENV_INVALID;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200236 return 0;
wdenkc6097192002-11-03 00:24:07 +0000237}
Simon Glassc10a88e2017-08-03 12:21:58 -0600238#endif
wdenkc6097192002-11-03 00:24:07 +0000239
240#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600241static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000242{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200243 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200244 int rc = 1;
Tom Rinicfbdf562014-04-17 16:20:39 -0400245 char *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200246#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
247 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000248
Igor Grinberg21821052011-11-07 01:14:09 +0000249 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200250 debug("Data to save 0x%lx\n", up_data);
251 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000252 saved_data = malloc(up_data);
253 if (saved_data == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200254 printf("Unable to save the rest of sector (%ld)\n",
255 up_data);
256 goto done;
257 }
258 memcpy(saved_data,
259 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
260 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
261 (ulong)flash_addr + CONFIG_ENV_SIZE,
262 up_data,
263 (ulong)saved_data);
264 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200265#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000266
Igor Grinberg21821052011-11-07 01:14:09 +0000267 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000268
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 if (flash_sect_protect(0, (long)flash_addr, end_addr))
270 goto done;
wdenkc6097192002-11-03 00:24:07 +0000271
Marek Vasutd73c1292014-03-05 19:59:50 +0100272 rc = env_export(&env_new);
273 if (rc)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200274 goto done;
wdenkc6097192002-11-03 00:24:07 +0000275
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200276 puts("Erasing Flash...");
277 if (flash_sect_erase((long)flash_addr, end_addr))
278 goto done;
wdenkc6097192002-11-03 00:24:07 +0000279
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200280 puts("Writing to Flash... ");
281 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg21821052011-11-07 01:14:09 +0000282 if (rc != 0)
283 goto perror;
284
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200285#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
286 if (up_data) { /* restore the rest of sector */
287 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
288 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
289 if (flash_write(saved_data,
290 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000291 up_data))
292 goto perror;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200293 }
294#endif
295 puts("done\n");
296 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000297 goto done;
298perror:
299 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200300done:
301 if (saved_data)
302 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000303 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000304 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200305 return rc;
wdenkc6097192002-11-03 00:24:07 +0000306}
wdenkc6097192002-11-03 00:24:07 +0000307#endif /* CMD_SAVEENV */
308
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200309#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000310
Simon Glass082af922017-08-03 12:22:01 -0600311#ifdef LOADENV
Simon Glass99778492017-08-03 12:22:17 -0600312static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000313{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200314#ifdef CONFIG_ENV_ADDR_REDUND
wdenkacd9b102004-03-14 00:59:59 +0000315 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200316 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000317 ulong ltmp = end_addr;
318
319 flash_addr = flash_addr_new;
320 flash_addr_new = etmp;
321
322 end_addr = end_addr_new;
323 end_addr_new = ltmp;
324 }
325
Simon Glass4686d8f2019-08-01 09:47:08 -0600326 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
Igor Grinberg21821052011-11-07 01:14:09 +0000327 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
Simon Glass4686d8f2019-08-01 09:47:08 -0600328 char flag = ENV_REDUND_OBSOLETE;
wdenkacd9b102004-03-14 00:59:59 +0000329
Simon Glass4bc2ad22017-08-03 12:21:56 -0600330 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200331 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000332 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000333 (ulong)&(flash_addr_new->flags),
334 sizeof(flash_addr_new->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200335 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000336 }
337
Simon Glass4686d8f2019-08-01 09:47:08 -0600338 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
339 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
340 char flag = ENV_REDUND_ACTIVE;
wdenkacd9b102004-03-14 00:59:59 +0000341
Simon Glass4bc2ad22017-08-03 12:21:56 -0600342 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200343 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000344 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000345 (ulong)&(flash_addr->flags),
346 sizeof(flash_addr->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200347 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000348 }
349
Simon Glass4bc2ad22017-08-03 12:21:56 -0600350 if (gd->env_valid == ENV_REDUND)
Igor Grinberg21821052011-11-07 01:14:09 +0000351 puts("*** Warning - some problems detected "
352 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200353#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200354
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200355 return env_import((char *)flash_addr, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +0000356}
Simon Glass082af922017-08-03 12:22:01 -0600357#endif /* LOADENV */
Simon Glassc10a88e2017-08-03 12:21:58 -0600358
359U_BOOT_ENV_LOCATION(flash) = {
360 .location = ENVL_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600361 ENV_NAME("Flash")
Simon Glassc10a88e2017-08-03 12:21:58 -0600362#ifdef LOADENV
Simon Glass082af922017-08-03 12:22:01 -0600363 .load = env_flash_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600364#endif
365#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600366 .save = env_save_ptr(env_flash_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600367#endif
368#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600369 .init = env_flash_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600370#endif
371};