blob: 32236c716ec7971223613ad57b18ba6f34ef7ddf [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>
14#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <linux/stddef.h>
wdenkcc1e2562003-03-06 13:39:27 +000016#include <malloc.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020017#include <search.h>
18#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000019
Wolfgang Denk6405a152006-03-31 18:32:53 +020020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassc10a88e2017-08-03 12:21:58 -060022#ifndef CONFIG_SPL_BUILD
23# if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
24# define CMD_SAVEENV
25# elif defined(CONFIG_ENV_ADDR_REDUND)
26# error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
27# endif
wdenkc6097192002-11-03 00:24:07 +000028#endif
29
Igor Grinberg21821052011-11-07 01:14:09 +000030#if defined(CONFIG_ENV_SIZE_REDUND) && \
31 (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020032#error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
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
wdenkc6097192002-11-03 00:24:07 +000047#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000048env_t *env_ptr = &environment;
wdenkc6097192002-11-03 00:24:07 +000049
Simon Glass082af922017-08-03 12:22:01 -060050static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000051
52#else /* ! ENV_IS_EMBEDDED */
53
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020054env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
Simon Glass082af922017-08-03 12:22:01 -060055static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000056#endif /* ENV_IS_EMBEDDED */
57
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;
wdenkacd9b102004-03-14 00:59:59 +000097 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
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;
wdenkacd9b102004-03-14 00:59:59 +0000100 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
Igor Grinberg21821052011-11-07 01:14:09 +0000101 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600102 gd->env_valid = ENV_VALID;
wdenkacd9b102004-03-14 00:59:59 +0000103 } else if (flag1 == flag2) {
Igor Grinberg21821052011-11-07 01:14:09 +0000104 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600105 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000106 } else if (flag1 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000107 gd->env_addr = addr1;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600108 gd->env_valid = ENV_REDUND;
wdenkacd9b102004-03-14 00:59:59 +0000109 } else if (flag2 == 0xFF) {
Igor Grinberg21821052011-11-07 01:14:09 +0000110 gd->env_addr = addr2;
Simon Glass4bc2ad22017-08-03 12:21:56 -0600111 gd->env_valid = ENV_REDUND;
wdenkc6097192002-11-03 00:24:07 +0000112 }
113
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200114 return 0;
wdenkc6097192002-11-03 00:24:07 +0000115}
Simon Glassc10a88e2017-08-03 12:21:58 -0600116#endif
wdenkc6097192002-11-03 00:24:07 +0000117
118#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600119static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000120{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200121 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +0100122 char *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200123 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Igor Grinberg21821052011-11-07 01:14:09 +0000124 int rc = 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200125#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200126 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000127#endif
wdenkc6097192002-11-03 00:24:07 +0000128
Igor Grinberg21821052011-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 Grinberg21821052011-11-07 01:14:09 +0000131 if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200132 goto done;
wdenkc6097192002-11-03 00:24:07 +0000133
Wolfgang Denk460a9ff2010-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 Grinberg21821052011-11-07 01:14:09 +0000137 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200138 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200139
Marek Vasutd73c1292014-03-05 19:59:50 +0100140 rc = env_export(&env_new);
141 if (rc)
142 return rc;
Igor Grinberg21821052011-11-07 01:14:09 +0000143 env_new.flags = new_flag;
wdenkc6097192002-11-03 00:24:07 +0000144
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200145#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Igor Grinberg21821052011-11-07 01:14:09 +0000146 up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200147 debug("Data to save 0x%lX\n", up_data);
wdenkcc1e2562003-03-06 13:39:27 +0000148 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000149 saved_data = malloc(up_data);
150 if (saved_data == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000151 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000152 up_data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200153 goto done;
wdenkcc1e2562003-03-06 13:39:27 +0000154 }
wdenk57b2d802003-06-27 21:31:46 +0000155 memcpy(saved_data,
Igor Grinberg21821052011-11-07 01:14:09 +0000156 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
157 up_data);
Wolfgang Denk460a9ff2010-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);
wdenkcc1e2562003-03-06 13:39:27 +0000161 }
162#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200163 puts("Erasing Flash...");
Igor Grinberg21821052011-11-07 01:14:09 +0000164 debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000165
Igor Grinberg21821052011-11-07 01:14:09 +0000166 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200167 goto done;
wdenkc6097192002-11-03 00:24:07 +0000168
Wolfgang Denk460a9ff2010-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 Grinberg21821052011-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-VILLARD7e1cda62008-09-10 22:48:06 +0200183#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000184 if (up_data) { /* restore the rest of sector */
Wolfgang Denk460a9ff2010-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);
wdenk57b2d802003-06-27 21:31:46 +0000187 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200188 (long)flash_addr_new + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000189 up_data))
190 goto perror;
wdenkcc1e2562003-03-06 13:39:27 +0000191 }
192#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200193 puts("done\n");
194
wdenkc6097192002-11-03 00:24:07 +0000195 {
Igor Grinberg21821052011-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 Grinberg21821052011-11-07 01:14:09 +0000207 goto done;
208perror:
209 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200210done:
wdenkcc1e2562003-03-06 13:39:27 +0000211 if (saved_data)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200212 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000213 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000214 flash_sect_protect(1, (ulong)flash_addr, end_addr);
215 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000216
217 return rc;
218}
219#endif /* CMD_SAVEENV */
220
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200221#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000222
Simon Glassc10a88e2017-08-03 12:21:58 -0600223#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600224static int env_flash_init(void)
wdenkc6097192002-11-03 00:24:07 +0000225{
wdenkc6097192002-11-03 00:24:07 +0000226 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Igor Grinberg21821052011-11-07 01:14:09 +0000227 gd->env_addr = (ulong)&(env_ptr->data);
Simon Glass4bc2ad22017-08-03 12:21:56 -0600228 gd->env_valid = ENV_VALID;
Igor Grinberg21821052011-11-07 01:14:09 +0000229 return 0;
wdenkc6097192002-11-03 00:24:07 +0000230 }
Dirk Behmeafa1dd52007-08-20 07:09:05 +0200231
Igor Grinberg21821052011-11-07 01:14:09 +0000232 gd->env_addr = (ulong)&default_environment[0];
Simon Glass1f39d0b2017-08-20 04:45:15 -0600233 gd->env_valid = ENV_INVALID;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200234 return 0;
wdenkc6097192002-11-03 00:24:07 +0000235}
Simon Glassc10a88e2017-08-03 12:21:58 -0600236#endif
wdenkc6097192002-11-03 00:24:07 +0000237
238#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600239static int env_flash_save(void)
wdenkc6097192002-11-03 00:24:07 +0000240{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200241 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242 int rc = 1;
Tom Rinicfbdf562014-04-17 16:20:39 -0400243 char *saved_data = NULL;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200244#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
245 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000246
Igor Grinberg21821052011-11-07 01:14:09 +0000247 up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200248 debug("Data to save 0x%lx\n", up_data);
249 if (up_data) {
Igor Grinberg21821052011-11-07 01:14:09 +0000250 saved_data = malloc(up_data);
251 if (saved_data == NULL) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200252 printf("Unable to save the rest of sector (%ld)\n",
253 up_data);
254 goto done;
255 }
256 memcpy(saved_data,
257 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
258 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
259 (ulong)flash_addr + CONFIG_ENV_SIZE,
260 up_data,
261 (ulong)saved_data);
262 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200263#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000264
Igor Grinberg21821052011-11-07 01:14:09 +0000265 debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000266
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200267 if (flash_sect_protect(0, (long)flash_addr, end_addr))
268 goto done;
wdenkc6097192002-11-03 00:24:07 +0000269
Marek Vasutd73c1292014-03-05 19:59:50 +0100270 rc = env_export(&env_new);
271 if (rc)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200272 goto done;
wdenkc6097192002-11-03 00:24:07 +0000273
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200274 puts("Erasing Flash...");
275 if (flash_sect_erase((long)flash_addr, end_addr))
276 goto done;
wdenkc6097192002-11-03 00:24:07 +0000277
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200278 puts("Writing to Flash... ");
279 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
Igor Grinberg21821052011-11-07 01:14:09 +0000280 if (rc != 0)
281 goto perror;
282
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200283#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
284 if (up_data) { /* restore the rest of sector */
285 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
286 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
287 if (flash_write(saved_data,
288 (long)flash_addr + CONFIG_ENV_SIZE,
Igor Grinberg21821052011-11-07 01:14:09 +0000289 up_data))
290 goto perror;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200291 }
292#endif
293 puts("done\n");
294 rc = 0;
Igor Grinberg21821052011-11-07 01:14:09 +0000295 goto done;
296perror:
297 flash_perror(rc);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200298done:
299 if (saved_data)
300 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000301 /* try to re-protect */
Igor Grinberg21821052011-11-07 01:14:09 +0000302 flash_sect_protect(1, (long)flash_addr, end_addr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200303 return rc;
wdenkc6097192002-11-03 00:24:07 +0000304}
wdenkc6097192002-11-03 00:24:07 +0000305#endif /* CMD_SAVEENV */
306
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200307#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000308
Simon Glass082af922017-08-03 12:22:01 -0600309#ifdef LOADENV
Simon Glass99778492017-08-03 12:22:17 -0600310static int env_flash_load(void)
wdenkc6097192002-11-03 00:24:07 +0000311{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200312#ifdef CONFIG_ENV_ADDR_REDUND
wdenkacd9b102004-03-14 00:59:59 +0000313 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200314 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000315 ulong ltmp = end_addr;
316
317 flash_addr = flash_addr_new;
318 flash_addr_new = etmp;
319
320 end_addr = end_addr_new;
321 end_addr_new = ltmp;
322 }
323
wdenkacd9b102004-03-14 00:59:59 +0000324 if (flash_addr_new->flags != OBSOLETE_FLAG &&
Igor Grinberg21821052011-11-07 01:14:09 +0000325 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
wdenkacd9b102004-03-14 00:59:59 +0000326 char flag = OBSOLETE_FLAG;
327
Simon Glass4bc2ad22017-08-03 12:21:56 -0600328 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200329 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000330 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000331 (ulong)&(flash_addr_new->flags),
332 sizeof(flash_addr_new->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200333 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000334 }
335
wdenkacd9b102004-03-14 00:59:59 +0000336 if (flash_addr->flags != ACTIVE_FLAG &&
337 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
338 char flag = ACTIVE_FLAG;
339
Simon Glass4bc2ad22017-08-03 12:21:56 -0600340 gd->env_valid = ENV_REDUND;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200341 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000342 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000343 (ulong)&(flash_addr->flags),
344 sizeof(flash_addr->flags));
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200345 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000346 }
347
Simon Glass4bc2ad22017-08-03 12:21:56 -0600348 if (gd->env_valid == ENV_REDUND)
Igor Grinberg21821052011-11-07 01:14:09 +0000349 puts("*** Warning - some problems detected "
350 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200351#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200352
Simon Goldschmidt35b4c322018-01-31 14:47:12 +0100353 return env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000354}
Simon Glass082af922017-08-03 12:22:01 -0600355#endif /* LOADENV */
Simon Glassc10a88e2017-08-03 12:21:58 -0600356
357U_BOOT_ENV_LOCATION(flash) = {
358 .location = ENVL_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600359 ENV_NAME("Flash")
Simon Glassc10a88e2017-08-03 12:21:58 -0600360#ifdef LOADENV
Simon Glass082af922017-08-03 12:22:01 -0600361 .load = env_flash_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600362#endif
363#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600364 .save = env_save_ptr(env_flash_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600365#endif
366#ifdef INITENV
Simon Glass082af922017-08-03 12:22:01 -0600367 .init = env_flash_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600368#endif
369};