blob: 722d5adf8b696637b11af03cd6a4ad6e788754b2 [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>
wdenkc6097192002-11-03 00:24:07 +000018#include <linux/stddef.h>
wdenkcc1e2562003-03-06 13:39:27 +000019#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020020#include <search.h>
21#include <errno.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070022#include <u-boot/crc.h>
wdenkc6097192002-11-03 00:24:07 +000023
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Simon Glassc10a88e2017-08-03 12:21:58 -060026#ifndef CONFIG_SPL_BUILD
27# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
28# 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)) || \
38 !defined(CONFIG_SPL_BUILD)
39#define LOADENV
40#endif
41
42#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
43#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
79 ulong addr_default = (ulong)&default_environment[0];
80 ulong addr1 = (ulong)&(flash_addr->data);
81 ulong addr2 = (ulong)&(flash_addr_new->data);
82
Igor Grinberg21821052011-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;
wdenkf8062712005-01-09 23:16:25 +000086
Igor Grinberg21821052011-11-07 01:14:09 +000087 if (crc1_ok && !crc2_ok) {
88 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -060089 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +000090 } else if (!crc1_ok && crc2_ok) {
91 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -060092 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +000093 } else if (!crc1_ok && !crc2_ok) {
94 gd->env_addr = addr_default;
Simon Glass1f39d0b2017-08-20 04:45:15 -060095 gd->env_valid = ENV_INVALID;
Simon Glass4686d8f2019-08-01 09:47:08 -060096 } else if (flag1 == ENV_REDUND_ACTIVE &&
97 flag2 == ENV_REDUND_OBSOLETE) {
Igor Grinberg21821052011-11-07 01:14:09 +000098 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -060099 gd->env_valid = ENV_VALID;
Simon Glass4686d8f2019-08-01 09:47:08 -0600100 } else if (flag1 == ENV_REDUND_OBSOLETE &&
101 flag2 == ENV_REDUND_ACTIVE) {
Igor Grinberg21821052011-11-07 01:14:09 +0000102 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600103 gd->env_valid = ENV_VALID;
wdenkacd9b102004-03-14 00:59:59 +0000104 } else if (flag1 == flag2) {
Igor Grinberg21821052011-11-07 01:14:09 +0000105 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600106 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000107 } else if (flag1 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000108 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600109 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000110 } else if (flag2 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000111 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600112 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000113 }
114
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200115 return 0;
wdenkc6097192002-11-03 00:24:07 +0000116}
Simon Glassc10a88e2017-08-03 12:21:58 -0600117#endif
wdenkc6097192002-11-03 00:24:07 +0000118
119#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600120static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000121{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200122 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +0100123 char *saved_data = NULL;
Simon Glass4686d8f2019-08-01 09:47:08 -0600124 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
Igor Grinberg21821052011-11-07 01:14:09 +0000125 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200126#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200127 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000128#endif
wdenkc6097192002-11-03 00:24:07 +0000129
Igor Grinberg21821052011-11-07 01:14:09 +0000130 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000131
Igor Grinberg21821052011-11-07 01:14:09 +0000132 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200133 goto done;
wdenkc6097192002-11-03 00:24:07 +0000134
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200135 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000136 (ulong)flash_addr_new, end_addr_new);
137
Igor Grinberg21821052011-11-07 01:14:09 +0000138 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200139 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200140
Marek Vasutd73c1292014-03-05 19:59:50 +0100141 rc = env_export(&env_new);
142 if (rc)
143 return rc;
Igor Grinberg21821052011-11-07 01:14:09 +0000144 env_new.flags = new_flag;
wdenkc6097192002-11-03 00:24:07 +0000145
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200146#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg21821052011-11-07 01:14:09 +0000147 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200148 debug("Data to save 0x%lX\n", up_data);
wdenkcc1e2562003-03-06 13:39:27 +0000149 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000150 saved_data = malloc(up_data);
151 if (saved_data == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000152 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000153 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200154 goto done;
wdenkcc1e2562003-03-06 13:39:27 +0000155 }
wdenk57b2d802003-06-27 21:31:46 +0000156 memcpy(saved_data,
Igor Grinberg21821052011-11-07 01:14:09 +0000157 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
158 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200159 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
160 (long)flash_addr_new + CONFIG_ENV_SIZE,
161 up_data, saved_data);
wdenkcc1e2562003-03-06 13:39:27 +0000162 }
163#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200164 puts("Erasing Flash...");
Igor Grinberg21821052011-11-07 01:14:09 +0000165 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000166
Igor Grinberg21821052011-11-07 01:14:09 +0000167 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200168 goto done;
wdenkc6097192002-11-03 00:24:07 +0000169
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200170 puts("Writing to Flash... ");
171 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000172 (ulong)&(flash_addr_new->data),
Igor Grinberg21821052011-11-07 01:14:09 +0000173 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
174 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
175 sizeof(env_new));
176 if (rc)
177 goto perror;
178
179 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
180 sizeof(flash_addr->flags));
181 if (rc)
182 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000183
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200184#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000185 if (up_data) { /* restore the rest of sector */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200186 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
187 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk57b2d802003-06-27 21:31:46 +0000188 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200189 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000190 up_data))
191 goto perror;
wdenkcc1e2562003-03-06 13:39:27 +0000192 }
193#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200194 puts("done\n");
195
wdenkc6097192002-11-03 00:24:07 +0000196 {
Igor Grinberg21821052011-11-07 01:14:09 +0000197 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000198 ulong ltmp = end_addr;
199
200 flash_addr = flash_addr_new;
201 flash_addr_new = etmp;
202
203 end_addr = end_addr_new;
204 end_addr_new = ltmp;
205 }
206
207 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000208 goto done;
209perror:
210 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200211done:
wdenkcc1e2562003-03-06 13:39:27 +0000212 if (saved_data)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200213 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000214 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000215 flash_sect_protect(1, (ulong)flash_addr, end_addr);
216 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000217
218 return rc;
219}
220#endif /* CMD_SAVEENV */
221
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200222#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000223
Simon Glassc10a88e2017-08-03 12:21:58 -0600224#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600225static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +0000226{
wdenkc6097192002-11-03 00:24:07 +0000227 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg21821052011-11-07 01:14:09 +0000228 gd->env_addr = (ulong)&(env_ptr->data);
Simon Glass4bc2ad22017-08-03 12:21:56 -0600229 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +0000230 return 0;
wdenkc6097192002-11-03 00:24:07 +0000231 }
Dirk Behmeafa1dd52007-08-20 07:09:05 +0200232
Igor Grinberg21821052011-11-07 01:14:09 +0000233 gd->env_addr = (ulong)&default_environment[0];
Simon Glass1f39d0b2017-08-20 04:45:15 -0600234 gd->env_valid = ENV_INVALID;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200235 return 0;
wdenkc6097192002-11-03 00:24:07 +0000236}
Simon Glassc10a88e2017-08-03 12:21:58 -0600237#endif
wdenkc6097192002-11-03 00:24:07 +0000238
239#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600240static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000241{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200243 int rc = 1;
Tom Rinicfbdf562014-04-17 16:20:39 -0400244 char *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200245#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
246 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000247
Igor Grinberg21821052011-11-07 01:14:09 +0000248 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200249 debug("Data to save 0x%lx\n", up_data);
250 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000251 saved_data = malloc(up_data);
252 if (saved_data == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200253 printf("Unable to save the rest of sector (%ld)\n",
254 up_data);
255 goto done;
256 }
257 memcpy(saved_data,
258 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
259 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
260 (ulong)flash_addr + CONFIG_ENV_SIZE,
261 up_data,
262 (ulong)saved_data);
263 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200264#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000265
Igor Grinberg21821052011-11-07 01:14:09 +0000266 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000267
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200268 if (flash_sect_protect(0, (long)flash_addr, end_addr))
269 goto done;
wdenkc6097192002-11-03 00:24:07 +0000270
Marek Vasutd73c1292014-03-05 19:59:50 +0100271 rc = env_export(&env_new);
272 if (rc)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200273 goto done;
wdenkc6097192002-11-03 00:24:07 +0000274
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200275 puts("Erasing Flash...");
276 if (flash_sect_erase((long)flash_addr, end_addr))
277 goto done;
wdenkc6097192002-11-03 00:24:07 +0000278
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200279 puts("Writing to Flash... ");
280 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg21821052011-11-07 01:14:09 +0000281 if (rc != 0)
282 goto perror;
283
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200284#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
285 if (up_data) { /* restore the rest of sector */
286 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
287 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
288 if (flash_write(saved_data,
289 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000290 up_data))
291 goto perror;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200292 }
293#endif
294 puts("done\n");
295 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000296 goto done;
297perror:
298 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200299done:
300 if (saved_data)
301 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000302 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000303 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200304 return rc;
wdenkc6097192002-11-03 00:24:07 +0000305}
wdenkc6097192002-11-03 00:24:07 +0000306#endif /* CMD_SAVEENV */
307
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200308#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000309
Simon Glass082af922017-08-03 12:22:01 -0600310#ifdef LOADENV
Simon Glass99778492017-08-03 12:22:17 -0600311static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000312{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200313#ifdef CONFIG_ENV_ADDR_REDUND
wdenkacd9b102004-03-14 00:59:59 +0000314 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200315 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000316 ulong ltmp = end_addr;
317
318 flash_addr = flash_addr_new;
319 flash_addr_new = etmp;
320
321 end_addr = end_addr_new;
322 end_addr_new = ltmp;
323 }
324
Simon Glass4686d8f2019-08-01 09:47:08 -0600325 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
Igor Grinberg21821052011-11-07 01:14:09 +0000326 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
Simon Glass4686d8f2019-08-01 09:47:08 -0600327 char flag = ENV_REDUND_OBSOLETE;
wdenkacd9b102004-03-14 00:59:59 +0000328
Simon Glass4bc2ad22017-08-03 12:21:56 -0600329 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200330 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000331 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000332 (ulong)&(flash_addr_new->flags),
333 sizeof(flash_addr_new->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200334 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000335 }
336
Simon Glass4686d8f2019-08-01 09:47:08 -0600337 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
338 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
339 char flag = ENV_REDUND_ACTIVE;
wdenkacd9b102004-03-14 00:59:59 +0000340
Simon Glass4bc2ad22017-08-03 12:21:56 -0600341 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200342 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000343 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000344 (ulong)&(flash_addr->flags),
345 sizeof(flash_addr->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200346 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000347 }
348
Simon Glass4bc2ad22017-08-03 12:21:56 -0600349 if (gd->env_valid == ENV_REDUND)
Igor Grinberg21821052011-11-07 01:14:09 +0000350 puts("*** Warning - some problems detected "
351 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200352#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200353
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200354 return env_import((char *)flash_addr, 1, H_EXTERNAL);
wdenkc6097192002-11-03 00:24:07 +0000355}
Simon Glass082af922017-08-03 12:22:01 -0600356#endif /* LOADENV */
Simon Glassc10a88e2017-08-03 12:21:58 -0600357
358U_BOOT_ENV_LOCATION(flash) = {
359 .location = ENVL_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600360 ENV_NAME("Flash")
Simon Glassc10a88e2017-08-03 12:21:58 -0600361#ifdef LOADENV
Simon Glass082af922017-08-03 12:22:01 -0600362 .load = env_flash_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600363#endif
364#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600365 .save = env_save_ptr(env_flash_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600366#endif
367#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600368 .init = env_flash_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600369#endif
370};