blob: 65a867d50014c34c8bb569b3e89af88e88e105b4 [file] [log] [blame]
wdenk13a56952004-06-09 14:58:14 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Stuart Woodcc49cad2008-05-30 16:05:28 -04005 * (C) Copyright 2008
6 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
7 *
wdenk13a56952004-06-09 14:58:14 +00008 * (C) Copyright 2004
9 * Jian Zhang, Texas Instruments, jzhang@ti.com.
wdenk13a56952004-06-09 14:58:14 +000010 *
11 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Andreas Heppel <aheppel@sysgo.de>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020013 *
wdenk13a56952004-06-09 14:58:14 +000014 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
31 */
32
Wolfgang Denkea882ba2010-06-20 23:33:59 +020033#define DEBUG
wdenk13a56952004-06-09 14:58:14 +000034
35#include <common.h>
wdenk13a56952004-06-09 14:58:14 +000036#include <command.h>
37#include <environment.h>
38#include <linux/stddef.h>
Markus Klotzbuechere443c942006-03-20 18:02:44 +010039#include <malloc.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010040#include <nand.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020041#include <search.h>
42#include <errno.h>
wdenk13a56952004-06-09 14:58:14 +000043
Mike Frysingerbdab39d2009-01-28 19:08:14 -050044#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk13a56952004-06-09 14:58:14 +000045#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020046#elif defined(CONFIG_ENV_OFFSET_REDUND)
Mike Frysingerbdab39d2009-01-28 19:08:14 -050047#error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000048#endif
49
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020050#if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
51#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000052#endif
53
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020054#ifndef CONFIG_ENV_RANGE
55#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Woodcc49cad2008-05-30 16:05:28 -040056#endif
57
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058char *env_name_spec = "NAND";
wdenk13a56952004-06-09 14:58:14 +000059
60
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020061#if defined(ENV_IS_EMBEDDED)
Igor Grinberg994bc672011-11-17 06:07:23 +000062env_t *env_ptr = &environment;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020063#elif defined(CONFIG_NAND_ENV_DST)
64env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk13a56952004-06-09 14:58:14 +000065#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000066env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000067#endif /* ENV_IS_EMBEDDED */
68
Wolfgang Denkd87080b2006-03-31 18:32:53 +020069DECLARE_GLOBAL_DATA_PTR;
wdenk13a56952004-06-09 14:58:14 +000070
71uchar env_get_char_spec (int index)
72{
wdenk13a56952004-06-09 14:58:14 +000073 return ( *((uchar *)(gd->env_addr + index)) );
74}
75
Wolfgang Denkea882ba2010-06-20 23:33:59 +020076/*
77 * This is called before nand_init() so we can't read NAND to
78 * validate env data.
79 *
80 * Mark it OK for now. env_relocate() in env_common.c will call our
81 * relocate function which does the real validation.
Stefan Roesed12ae802006-09-12 20:19:10 +020082 *
83 * When using a NAND boot image (like sequoia_nand), the environment
Wolfgang Denkea882ba2010-06-20 23:33:59 +020084 * can be embedded or attached to the U-Boot image in NAND flash.
85 * This way the SPL loads not only the U-Boot image from NAND but
86 * also the environment.
wdenk13a56952004-06-09 14:58:14 +000087 */
88int env_init(void)
89{
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020090#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesed12ae802006-09-12 20:19:10 +020091 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020092 env_t *tmp_env1;
93
94#ifdef CONFIG_ENV_OFFSET_REDUND
95 env_t *tmp_env2;
96
97 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
98 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
99#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200100
Stefan Roesed12ae802006-09-12 20:19:10 +0200101 tmp_env1 = env_ptr;
Stefan Roesed12ae802006-09-12 20:19:10 +0200102
103 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
Stefan Roesed12ae802006-09-12 20:19:10 +0200104
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200105 if (!crc1_ok && !crc2_ok) {
106 gd->env_addr = 0;
Stefan Roesed12ae802006-09-12 20:19:10 +0200107 gd->env_valid = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200108
109 return 0;
110 } else if (crc1_ok && !crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200111 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200112 }
113#ifdef CONFIG_ENV_OFFSET_REDUND
114 else if (!crc1_ok && crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200115 gd->env_valid = 2;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200116 } else {
Stefan Roesed12ae802006-09-12 20:19:10 +0200117 /* both ok - check serial */
118 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
119 gd->env_valid = 2;
120 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
121 gd->env_valid = 1;
122 else if(tmp_env1->flags > tmp_env2->flags)
123 gd->env_valid = 1;
124 else if(tmp_env2->flags > tmp_env1->flags)
125 gd->env_valid = 2;
126 else /* flags are equal - almost impossible */
127 gd->env_valid = 1;
128 }
129
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200130 if (gd->env_valid == 2)
131 env_ptr = tmp_env2;
132 else
133#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200134 if (gd->env_valid == 1)
135 env_ptr = tmp_env1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200136
137 gd->env_addr = (ulong)env_ptr->data;
138
139#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100140 gd->env_addr = (ulong)&default_environment[0];
wdenk13a56952004-06-09 14:58:14 +0000141 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200142#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk13a56952004-06-09 14:58:14 +0000143
144 return (0);
145}
146
147#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100148/*
149 * The legacy NAND code saved the environment in the first NAND device i.e.,
150 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
151 */
Stuart Woodcc49cad2008-05-30 16:05:28 -0400152int writeenv(size_t offset, u_char *buf)
153{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200154 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400155 size_t amount_saved = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200156 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400157
158 u_char *char_ptr;
159
160 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200161 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400162
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200163 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400164 if (nand_block_isbad(&nand_info[0], offset)) {
165 offset += blocksize;
166 } else {
167 char_ptr = &buf[amount_saved];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200168 if (nand_write(&nand_info[0], offset, &len,
Stuart Woodcc49cad2008-05-30 16:05:28 -0400169 char_ptr))
170 return 1;
171 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200172 amount_saved += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400173 }
174 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200175 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400176 return 1;
177
178 return 0;
179}
Scott Woodeef1d712011-02-08 15:25:02 -0600180
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200181#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Woodeef1d712011-02-08 15:25:02 -0600182static unsigned char env_flags;
183
wdenk13a56952004-06-09 14:58:14 +0000184int saveenv(void)
185{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200186 env_t env_new;
187 ssize_t len;
188 char *res;
189 int ret = 0;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400190 nand_erase_options_t nand_erase_options;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100191
Daniel Hobi3b250ff2011-05-18 15:21:08 +0200192 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200193 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400194
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200195 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400196 return 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197
198 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100199 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200200 if (len < 0) {
201 error("Cannot export environment: errno = %d\n", errno);
202 return 1;
203 }
204 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Scott Woodeef1d712011-02-08 15:25:02 -0600205 env_new.flags = ++env_flags; /* increase the serial */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200206
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100207 if(gd->env_valid == 1) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200208 puts("Erasing redundant NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200209 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400210 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100211 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400212
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200213 puts("Writing to redundant NAND... ");
214 ret = writeenv(CONFIG_ENV_OFFSET_REDUND,
215 (u_char *)&env_new);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100216 } else {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200217 puts("Erasing NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200218 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400219 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100220 return 1;
221
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200222 puts("Writing to NAND... ");
223 ret = writeenv(CONFIG_ENV_OFFSET,
224 (u_char *)&env_new);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100225 }
Stuart Woodcc49cad2008-05-30 16:05:28 -0400226 if (ret) {
227 puts("FAILED!\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100228 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400229 }
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100230
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200231 puts("done\n");
232
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100233 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200234
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100235 return ret;
236}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200237#else /* ! CONFIG_ENV_OFFSET_REDUND */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100238int saveenv(void)
239{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100240 int ret = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200241 env_t env_new;
242 ssize_t len;
243 char *res;
Philip Balister9e4006bc2008-06-16 08:58:07 -0400244 nand_erase_options_t nand_erase_options;
Wolfgang Denke093a242008-06-28 23:34:37 +0200245
Daniel Hobi3b250ff2011-05-18 15:21:08 +0200246 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200247 nand_erase_options.length = CONFIG_ENV_RANGE;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200248 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400249
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200250 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400251 return 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252
253 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100254 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200255 if (len < 0) {
256 error("Cannot export environment: errno = %d\n", errno);
257 return 1;
258 }
259 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
260
261 puts("Erasing Nand...\n");
Stuart Woodcc49cad2008-05-30 16:05:28 -0400262 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
wdenk13a56952004-06-09 14:58:14 +0000263 return 1;
264
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200265 puts("Writing to Nand... ");
266 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400267 puts("FAILED!\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100268 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400269 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100270
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200271 puts("done\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100272 return ret;
wdenk13a56952004-06-09 14:58:14 +0000273}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200274#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000275#endif /* CMD_SAVEENV */
276
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200277int readenv(size_t offset, u_char * buf)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400278{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200279 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400280 size_t amount_loaded = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200281 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400282
283 u_char *char_ptr;
284
285 blocksize = nand_info[0].erasesize;
Mike Frysinger962ad592010-08-11 23:42:26 -0400286 if (!blocksize)
287 return 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200288 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400289
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200290 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400291 if (nand_block_isbad(&nand_info[0], offset)) {
292 offset += blocksize;
293 } else {
294 char_ptr = &buf[amount_loaded];
Steve Sakoman068a2082010-12-03 13:25:43 +0000295 if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400296 return 1;
297 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200298 amount_loaded += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400299 }
300 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200301 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400302 return 1;
303
304 return 0;
305}
306
Ben Gardinerc9f73512010-07-05 13:27:07 -0400307#ifdef CONFIG_ENV_OFFSET_OOB
308int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
309{
310 struct mtd_oob_ops ops;
311 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
312 int ret;
313
314 ops.datbuf = NULL;
315 ops.mode = MTD_OOB_AUTO;
316 ops.ooboffs = 0;
317 ops.ooblen = ENV_OFFSET_SIZE;
318 ops.oobbuf = (void *) oob_buf;
319
320 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500321 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400322 printf("error reading OOB block 0\n");
Scott Wood53504a22010-07-12 18:17:40 -0500323 return ret;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400324 }
Scott Wood53504a22010-07-12 18:17:40 -0500325
326 if (oob_buf[0] == ENV_OOB_MARKER) {
327 *result = oob_buf[1] * nand->erasesize;
328 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
329 *result = oob_buf[1];
330 } else {
331 printf("No dynamic environment marker in OOB block 0\n");
332 return -ENOENT;
333 }
334
335 return 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400336}
337#endif
338
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200339#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200340void env_relocate_spec(void)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100341{
342#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100343 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200344 env_t *ep, *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000345
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200346 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
347 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100348
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700349 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
350 puts("Can't allocate buffers for environment\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200351 free(tmp_env1);
352 free(tmp_env2);
353 set_default_env("!malloc() failed");
354 return;
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700355 }
356
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200357 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200358 puts("No Valid Environment Area found\n");
359
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200360 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200361 puts("No Valid Redundant Environment Area found\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100362
363 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
364 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
365
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200366 if (!crc1_ok && !crc2_ok) {
derek@siconix.com5a9427d2009-01-26 14:08:17 -0700367 free(tmp_env1);
368 free(tmp_env2);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200369 set_default_env("!bad CRC");
370 return;
371 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100372 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200373 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100374 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200375 } else {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100376 /* both ok - check serial */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200377 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100378 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200379 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100380 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200381 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100382 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200383 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100384 gd->env_valid = 2;
385 else /* flags are equal - almost impossible */
386 gd->env_valid = 1;
387
388 }
389
390 free(env_ptr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200391
392 if (gd->env_valid == 1)
393 ep = tmp_env1;
394 else
395 ep = tmp_env2;
396
Scott Woodeef1d712011-02-08 15:25:02 -0600397 env_flags = ep->flags;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200398 env_import((char *)ep, 0);
399
400 free(tmp_env1);
401 free(tmp_env2);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100402
403#endif /* ! ENV_IS_EMBEDDED */
404}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200405#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100406/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200407 * The legacy NAND code saved the environment in the first NAND
408 * device i.e., nand_dev_desc + 0. This is also the behaviour using
409 * the new NAND code.
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100410 */
wdenk13a56952004-06-09 14:58:14 +0000411void env_relocate_spec (void)
412{
413#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100414 int ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200415 char buf[CONFIG_ENV_SIZE];
wdenk13a56952004-06-09 14:58:14 +0000416
Ben Gardinerc9f73512010-07-05 13:27:07 -0400417#if defined(CONFIG_ENV_OFFSET_OOB)
418 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200419 /*
420 * If unable to read environment offset from NAND OOB then fall through
Ben Gardinerc9f73512010-07-05 13:27:07 -0400421 * to the normal environment reading code below
422 */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200423 if (!ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400424 printf("Found Environment offset in OOB..\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200425 } else {
426 set_default_env("!no env offset in OOB");
427 return;
428 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400429#endif
430
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200431 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
432 if (ret) {
433 set_default_env("!readenv() failed");
434 return;
435 }
wdenk13a56952004-06-09 14:58:14 +0000436
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200437 env_import(buf, 1);
wdenk13a56952004-06-09 14:58:14 +0000438#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000439}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200440#endif /* CONFIG_ENV_OFFSET_REDUND */