blob: 231a5fdf24d0038248759b420ea2747ceabb1593 [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>
wdenkc6097192002-11-03 00:24:07 +000020
Wolfgang Denkd87080b2006-03-31 18:32:53 +020021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass4415f1d2017-08-03 12:21:58 -060023#ifndef CONFIG_SPL_BUILD
24# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
25# define CMD_SAVEENV
26# elif defined(CONFIG_ENV_ADDR_REDUND)
27# error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
28# endif
wdenkc6097192002-11-03 00:24:07 +000029#endif
30
Igor Grinberg82b54b92011-11-07 01:14:09 +000031#if defined(CONFIG_ENV_SIZE_REDUND) && \
32 (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020033#error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
wdenkc6097192002-11-03 00:24:07 +000034#endif
35
Simon Glass4415f1d2017-08-03 12:21:58 -060036/* TODO(sjg@chromium.org): Figure out all these special cases */
37#if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
38 !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
39 !defined(CONFIG_TARGET_EDMINIV2)) || \
40 !defined(CONFIG_SPL_BUILD)
41#define LOADENV
42#endif
43
44#if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
45#define INITENV
46#endif
47
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000048#if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
49 !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
wdenkc6097192002-11-03 00:24:07 +000050#ifdef ENV_IS_EMBEDDED
Simon Glassbe54ec12019-08-01 09:47:03 -060051static env_t *env_ptr = &embedded_environment;
wdenkc6097192002-11-03 00:24:07 +000052#else /* ! ENV_IS_EMBEDDED */
53
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000054static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000055#endif /* ENV_IS_EMBEDDED */
Rajesh Bhagatb6cba292018-11-05 18:01:10 +000056#endif
57static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000058
Wolfgang Denkea882ba2010-06-20 23:33:59 +020059/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
Simon Glasse5bce242017-08-03 12:22:01 -060060static ulong __maybe_unused end_addr =
61 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020062
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020063#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glasse5bce242017-08-03 12:22:01 -060064
65static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
wdenkc6097192002-11-03 00:24:07 +000066
Wolfgang Denkea882ba2010-06-20 23:33:59 +020067/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Simon Glasse5bce242017-08-03 12:22:01 -060068static ulong __maybe_unused end_addr_new =
69 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020070#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000071
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020072#ifdef CONFIG_ENV_ADDR_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060073#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -060074static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +000075{
wdenk8ed96042005-01-09 23:16:25 +000076 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000077
78 uchar flag1 = flash_addr->flags;
79 uchar flag2 = flash_addr_new->flags;
80
81 ulong addr_default = (ulong)&default_environment[0];
82 ulong addr1 = (ulong)&(flash_addr->data);
83 ulong addr2 = (ulong)&(flash_addr_new->data);
84
Igor Grinberg82b54b92011-11-07 01:14:09 +000085 crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
86 crc2_ok =
87 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
wdenk8ed96042005-01-09 23:16:25 +000088
Igor Grinberg82b54b92011-11-07 01:14:09 +000089 if (crc1_ok && !crc2_ok) {
90 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -060091 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +000092 } else if (!crc1_ok && crc2_ok) {
93 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -060094 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +000095 } else if (!crc1_ok && !crc2_ok) {
96 gd->env_addr = addr_default;
Simon Glass2d7cb5b2017-08-20 04:45:15 -060097 gd->env_valid = ENV_INVALID;
Simon Glassd3716dd2019-08-01 09:47:08 -060098 } else if (flag1 == ENV_REDUND_ACTIVE &&
99 flag2 == ENV_REDUND_OBSOLETE) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000100 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -0600101 gd->env_valid = ENV_VALID;
Simon Glassd3716dd2019-08-01 09:47:08 -0600102 } else if (flag1 == ENV_REDUND_OBSOLETE &&
103 flag2 == ENV_REDUND_ACTIVE) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000104 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -0600105 gd->env_valid = ENV_VALID;
wdenkc3f9d492004-03-14 00:59:59 +0000106 } else if (flag1 == flag2) {
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 (flag1 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000110 gd->env_addr = addr1;
Simon Glass203e94f2017-08-03 12:21:56 -0600111 gd->env_valid = ENV_REDUND;
wdenkc3f9d492004-03-14 00:59:59 +0000112 } else if (flag2 == 0xFF) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000113 gd->env_addr = addr2;
Simon Glass203e94f2017-08-03 12:21:56 -0600114 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000115 }
116
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200117 return 0;
wdenkc6097192002-11-03 00:24:07 +0000118}
Simon Glass4415f1d2017-08-03 12:21:58 -0600119#endif
wdenkc6097192002-11-03 00:24:07 +0000120
121#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600122static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000123{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200124 env_t env_new;
Marek Vasut7ce15262014-03-05 19:59:50 +0100125 char *saved_data = NULL;
Simon Glassd3716dd2019-08-01 09:47:08 -0600126 char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000127 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200128#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200129 ulong up_data = 0;
wdenk500545c2003-03-06 14:23:06 +0000130#endif
wdenkc6097192002-11-03 00:24:07 +0000131
Igor Grinberg82b54b92011-11-07 01:14:09 +0000132 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000133
Igor Grinberg82b54b92011-11-07 01:14:09 +0000134 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200135 goto done;
wdenkc6097192002-11-03 00:24:07 +0000136
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200137 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000138 (ulong)flash_addr_new, end_addr_new);
139
Igor Grinberg82b54b92011-11-07 01:14:09 +0000140 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200141 goto done;
wdenkc6097192002-11-03 00:24:07 +0000142
Marek Vasut7ce15262014-03-05 19:59:50 +0100143 rc = env_export(&env_new);
144 if (rc)
145 return rc;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000146 env_new.flags = new_flag;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200147
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200148#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg82b54b92011-11-07 01:14:09 +0000149 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200150 debug("Data to save 0x%lX\n", up_data);
wdenk47cd00f2003-03-06 13:39:27 +0000151 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000152 saved_data = malloc(up_data);
153 if (saved_data == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000154 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000155 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200156 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000157 }
wdenk8bde7f72003-06-27 21:31:46 +0000158 memcpy(saved_data,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000159 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
160 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200161 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
162 (long)flash_addr_new + CONFIG_ENV_SIZE,
163 up_data, saved_data);
wdenk47cd00f2003-03-06 13:39:27 +0000164 }
165#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200166 puts("Erasing Flash...");
Igor Grinberg82b54b92011-11-07 01:14:09 +0000167 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000168
Igor Grinberg82b54b92011-11-07 01:14:09 +0000169 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200170 goto done;
wdenkc6097192002-11-03 00:24:07 +0000171
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200172 puts("Writing to Flash... ");
173 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000174 (ulong)&(flash_addr_new->data),
Igor Grinberg82b54b92011-11-07 01:14:09 +0000175 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
176 rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
177 sizeof(env_new));
178 if (rc)
179 goto perror;
180
181 rc = flash_write(&flag, (ulong)&(flash_addr->flags),
182 sizeof(flash_addr->flags));
183 if (rc)
184 goto perror;
wdenkc6097192002-11-03 00:24:07 +0000185
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200186#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenk47cd00f2003-03-06 13:39:27 +0000187 if (up_data) { /* restore the rest of sector */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200188 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
189 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk8bde7f72003-06-27 21:31:46 +0000190 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200191 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000192 up_data))
193 goto perror;
wdenk47cd00f2003-03-06 13:39:27 +0000194 }
195#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200196 puts("done\n");
197
wdenkc6097192002-11-03 00:24:07 +0000198 {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000199 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000200 ulong ltmp = end_addr;
201
202 flash_addr = flash_addr_new;
203 flash_addr_new = etmp;
204
205 end_addr = end_addr_new;
206 end_addr_new = ltmp;
207 }
208
209 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000210 goto done;
211perror:
212 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200213done:
wdenk47cd00f2003-03-06 13:39:27 +0000214 if (saved_data)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200215 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000216 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000217 flash_sect_protect(1, (ulong)flash_addr, end_addr);
218 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000219
220 return rc;
221}
222#endif /* CMD_SAVEENV */
223
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200224#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000225
Simon Glass4415f1d2017-08-03 12:21:58 -0600226#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -0600227static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +0000228{
wdenkc6097192002-11-03 00:24:07 +0000229 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000230 gd->env_addr = (ulong)&(env_ptr->data);
Simon Glass203e94f2017-08-03 12:21:56 -0600231 gd->env_valid = ENV_VALID;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000232 return 0;
wdenkc6097192002-11-03 00:24:07 +0000233 }
Dirk Behme9bb8b202007-08-20 07:09:05 +0200234
Igor Grinberg82b54b92011-11-07 01:14:09 +0000235 gd->env_addr = (ulong)&default_environment[0];
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600236 gd->env_valid = ENV_INVALID;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200237 return 0;
wdenkc6097192002-11-03 00:24:07 +0000238}
Simon Glass4415f1d2017-08-03 12:21:58 -0600239#endif
wdenkc6097192002-11-03 00:24:07 +0000240
241#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600242static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000243{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200244 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200245 int rc = 1;
Tom Rini3fa19812014-04-17 16:20:39 -0400246 char *saved_data = NULL;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
248 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000249
Igor Grinberg82b54b92011-11-07 01:14:09 +0000250 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200251 debug("Data to save 0x%lx\n", up_data);
252 if (up_data) {
Igor Grinberg82b54b92011-11-07 01:14:09 +0000253 saved_data = malloc(up_data);
254 if (saved_data == NULL) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200255 printf("Unable to save the rest of sector (%ld)\n",
256 up_data);
257 goto done;
258 }
259 memcpy(saved_data,
260 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
261 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
262 (ulong)flash_addr + CONFIG_ENV_SIZE,
263 up_data,
264 (ulong)saved_data);
wdenkc6097192002-11-03 00:24:07 +0000265 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200266#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000267
Igor Grinberg82b54b92011-11-07 01:14:09 +0000268 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200269
270 if (flash_sect_protect(0, (long)flash_addr, end_addr))
271 goto done;
272
Marek Vasut7ce15262014-03-05 19:59:50 +0100273 rc = env_export(&env_new);
274 if (rc)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200275 goto done;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200276
277 puts("Erasing Flash...");
278 if (flash_sect_erase((long)flash_addr, end_addr))
279 goto done;
280
281 puts("Writing to Flash... ");
282 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg82b54b92011-11-07 01:14:09 +0000283 if (rc != 0)
284 goto perror;
285
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200286#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
287 if (up_data) { /* restore the rest of sector */
288 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
289 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
290 if (flash_write(saved_data,
291 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg82b54b92011-11-07 01:14:09 +0000292 up_data))
293 goto perror;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200294 }
295#endif
296 puts("done\n");
297 rc = 0;
Igor Grinberg82b54b92011-11-07 01:14:09 +0000298 goto done;
299perror:
300 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200301done:
302 if (saved_data)
303 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000304 /* try to re-protect */
Igor Grinberg82b54b92011-11-07 01:14:09 +0000305 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200306 return rc;
wdenkc6097192002-11-03 00:24:07 +0000307}
wdenkc6097192002-11-03 00:24:07 +0000308#endif /* CMD_SAVEENV */
309
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200310#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000311
Simon Glasse5bce242017-08-03 12:22:01 -0600312#ifdef LOADENV
Simon Glassc5951992017-08-03 12:22:17 -0600313static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000314{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200315#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc3f9d492004-03-14 00:59:59 +0000316 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200317 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000318 ulong ltmp = end_addr;
319
320 flash_addr = flash_addr_new;
321 flash_addr_new = etmp;
322
323 end_addr = end_addr_new;
324 end_addr_new = ltmp;
325 }
326
Simon Glassd3716dd2019-08-01 09:47:08 -0600327 if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
Igor Grinberg82b54b92011-11-07 01:14:09 +0000328 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
Simon Glassd3716dd2019-08-01 09:47:08 -0600329 char flag = ENV_REDUND_OBSOLETE;
wdenkc3f9d492004-03-14 00:59:59 +0000330
Simon Glass203e94f2017-08-03 12:21:56 -0600331 gd->env_valid = ENV_REDUND;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200332 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkc3f9d492004-03-14 00:59:59 +0000333 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000334 (ulong)&(flash_addr_new->flags),
335 sizeof(flash_addr_new->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200336 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000337 }
338
Simon Glassd3716dd2019-08-01 09:47:08 -0600339 if (flash_addr->flags != ENV_REDUND_ACTIVE &&
340 (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
341 char flag = ENV_REDUND_ACTIVE;
wdenkc3f9d492004-03-14 00:59:59 +0000342
Simon Glass203e94f2017-08-03 12:21:56 -0600343 gd->env_valid = ENV_REDUND;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200344 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkc3f9d492004-03-14 00:59:59 +0000345 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000346 (ulong)&(flash_addr->flags),
347 sizeof(flash_addr->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200348 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000349 }
350
Simon Glass203e94f2017-08-03 12:21:56 -0600351 if (gd->env_valid == ENV_REDUND)
Igor Grinberg82b54b92011-11-07 01:14:09 +0000352 puts("*** Warning - some problems detected "
353 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200354#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200355
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100356 return env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000357}
Simon Glasse5bce242017-08-03 12:22:01 -0600358#endif /* LOADENV */
Simon Glass4415f1d2017-08-03 12:21:58 -0600359
360U_BOOT_ENV_LOCATION(flash) = {
361 .location = ENVL_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600362 ENV_NAME("Flash")
Simon Glass4415f1d2017-08-03 12:21:58 -0600363#ifdef LOADENV
Simon Glasse5bce242017-08-03 12:22:01 -0600364 .load = env_flash_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600365#endif
366#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600367 .save = env_save_ptr(env_flash_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600368#endif
369#ifdef INITENV
Simon Glasse5bce242017-08-03 12:22:01 -0600370 .init = env_flash_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600371#endif
372};