blob: a5e1038315bd01c520c25809e29fad01359d88d1 [file] [log] [blame]
wdenk13a56952004-06-09 14:58:14 +00001/*
Stuart Woodcc49cad2008-05-30 16:05:28 -04002 * (C) Copyright 2008
3 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
4 *
wdenk13a56952004-06-09 14:58:14 +00005 * (C) Copyright 2004
6 * Jian Zhang, Texas Instruments, jzhang@ti.com.
7
Stefan Roesed12ae802006-09-12 20:19:10 +02008 * (C) Copyright 2000-2006
wdenk13a56952004-06-09 14:58:14 +00009 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 *
11 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Andreas Heppel <aheppel@sysgo.de>
13
14 * 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
33/* #define DEBUG */
34
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>
Ben Gardinerc9f73512010-07-05 13:27:07 -040041#include <asm/errno.h>
wdenk13a56952004-06-09 14:58:14 +000042
Mike Frysingerbdab39d2009-01-28 19:08:14 -050043#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk13a56952004-06-09 14:58:14 +000044#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020045#elif defined(CONFIG_ENV_OFFSET_REDUND)
Mike Frysingerbdab39d2009-01-28 19:08:14 -050046#error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000047#endif
48
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020049#if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
50#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000051#endif
52
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020053#ifndef CONFIG_ENV_RANGE
54#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Woodcc49cad2008-05-30 16:05:28 -040055#endif
56
wdenk13a56952004-06-09 14:58:14 +000057/* references to names in env_common.c */
58extern uchar default_environment[];
wdenk13a56952004-06-09 14:58:14 +000059
60char * env_name_spec = "NAND";
61
62
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020063#if defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +000064extern uchar environment[];
65env_t *env_ptr = (env_t *)(&environment[0]);
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020066#elif defined(CONFIG_NAND_ENV_DST)
67env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk13a56952004-06-09 14:58:14 +000068#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000069env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000070#endif /* ENV_IS_EMBEDDED */
71
72
73/* local functions */
Stefan Roesed12ae802006-09-12 20:19:10 +020074#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +000075static void use_default(void);
Stefan Roesed12ae802006-09-12 20:19:10 +020076#endif
wdenk13a56952004-06-09 14:58:14 +000077
Wolfgang Denkd87080b2006-03-31 18:32:53 +020078DECLARE_GLOBAL_DATA_PTR;
wdenk13a56952004-06-09 14:58:14 +000079
80uchar env_get_char_spec (int index)
81{
wdenk13a56952004-06-09 14:58:14 +000082 return ( *((uchar *)(gd->env_addr + index)) );
83}
84
85
86/* this is called before nand_init()
87 * so we can't read Nand to validate env data.
88 * Mark it OK for now. env_relocate() in env_common.c
Marcel Ziswiler99c2b432008-06-22 16:13:46 +020089 * will call our relocate function which does the real
90 * validation.
Stefan Roesed12ae802006-09-12 20:19:10 +020091 *
92 * When using a NAND boot image (like sequoia_nand), the environment
93 * can be embedded or attached to the U-Boot image in NAND flash. This way
94 * the SPL loads not only the U-Boot image from NAND but also the
95 * environment.
wdenk13a56952004-06-09 14:58:14 +000096 */
97int env_init(void)
98{
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020099#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesed12ae802006-09-12 20:19:10 +0200100 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200101 env_t *tmp_env1;
102
103#ifdef CONFIG_ENV_OFFSET_REDUND
104 env_t *tmp_env2;
105
106 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
107 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
108#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200109
Stefan Roesed12ae802006-09-12 20:19:10 +0200110 tmp_env1 = env_ptr;
Stefan Roesed12ae802006-09-12 20:19:10 +0200111
112 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
Stefan Roesed12ae802006-09-12 20:19:10 +0200113
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200114 if (!crc1_ok && !crc2_ok) {
115 gd->env_addr = 0;
Stefan Roesed12ae802006-09-12 20:19:10 +0200116 gd->env_valid = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200117
118 return 0;
119 } else if (crc1_ok && !crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200120 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200121 }
122#ifdef CONFIG_ENV_OFFSET_REDUND
123 else if (!crc1_ok && crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200124 gd->env_valid = 2;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200125 } else {
Stefan Roesed12ae802006-09-12 20:19:10 +0200126 /* both ok - check serial */
127 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
128 gd->env_valid = 2;
129 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
130 gd->env_valid = 1;
131 else if(tmp_env1->flags > tmp_env2->flags)
132 gd->env_valid = 1;
133 else if(tmp_env2->flags > tmp_env1->flags)
134 gd->env_valid = 2;
135 else /* flags are equal - almost impossible */
136 gd->env_valid = 1;
137 }
138
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200139 if (gd->env_valid == 2)
140 env_ptr = tmp_env2;
141 else
142#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200143 if (gd->env_valid == 1)
144 env_ptr = tmp_env1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200145
146 gd->env_addr = (ulong)env_ptr->data;
147
148#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100149 gd->env_addr = (ulong)&default_environment[0];
wdenk13a56952004-06-09 14:58:14 +0000150 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200151#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk13a56952004-06-09 14:58:14 +0000152
153 return (0);
154}
155
156#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100157/*
158 * The legacy NAND code saved the environment in the first NAND device i.e.,
159 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
160 */
Stuart Woodcc49cad2008-05-30 16:05:28 -0400161int writeenv(size_t offset, u_char *buf)
162{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200163 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400164 size_t amount_saved = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200165 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400166
167 u_char *char_ptr;
168
169 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200170 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400171
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200172 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400173 if (nand_block_isbad(&nand_info[0], offset)) {
174 offset += blocksize;
175 } else {
176 char_ptr = &buf[amount_saved];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200177 if (nand_write(&nand_info[0], offset, &len,
Stuart Woodcc49cad2008-05-30 16:05:28 -0400178 char_ptr))
179 return 1;
180 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200181 amount_saved += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400182 }
183 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200184 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400185 return 1;
186
187 return 0;
188}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200189#ifdef CONFIG_ENV_OFFSET_REDUND
wdenk13a56952004-06-09 14:58:14 +0000190int saveenv(void)
191{
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100192 int ret = 0;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400193 nand_erase_options_t nand_erase_options;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100194
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100195 env_ptr->flags++;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100196
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200197 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400198 nand_erase_options.quiet = 0;
199 nand_erase_options.jffs2 = 0;
200 nand_erase_options.scrub = 0;
201
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200202 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400203 return 1;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100204 if(gd->env_valid == 1) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400205 puts ("Erasing redundant Nand...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200206 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400207 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100208 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400209
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100210 puts ("Writing to redundant Nand... ");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200211 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100212 } else {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400213 puts ("Erasing Nand...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200214 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400215 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100216 return 1;
217
218 puts ("Writing to Nand... ");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200219 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100220 }
Stuart Woodcc49cad2008-05-30 16:05:28 -0400221 if (ret) {
222 puts("FAILED!\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100223 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400224 }
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100225
226 puts ("done\n");
227 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
228 return ret;
229}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200230#else /* ! CONFIG_ENV_OFFSET_REDUND */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100231int saveenv(void)
232{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100233 int ret = 0;
Philip Balister9e4006bc2008-06-16 08:58:07 -0400234 nand_erase_options_t nand_erase_options;
Wolfgang Denke093a242008-06-28 23:34:37 +0200235
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200236 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400237 nand_erase_options.quiet = 0;
238 nand_erase_options.jffs2 = 0;
239 nand_erase_options.scrub = 0;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200240 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400241
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200242 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400243 return 1;
244 puts ("Erasing Nand...\n");
245 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
wdenk13a56952004-06-09 14:58:14 +0000246 return 1;
247
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100248 puts ("Writing to Nand... ");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200249 if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400250 puts("FAILED!\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100251 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400252 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100253
254 puts ("done\n");
255 return ret;
wdenk13a56952004-06-09 14:58:14 +0000256}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200257#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000258#endif /* CMD_SAVEENV */
259
Stuart Woodcc49cad2008-05-30 16:05:28 -0400260int readenv (size_t offset, u_char * buf)
261{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200262 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400263 size_t amount_loaded = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200264 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400265
266 u_char *char_ptr;
267
268 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200269 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400270
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200271 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400272 if (nand_block_isbad(&nand_info[0], offset)) {
273 offset += blocksize;
274 } else {
275 char_ptr = &buf[amount_loaded];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200276 if (nand_read(&nand_info[0], offset, &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400277 return 1;
278 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200279 amount_loaded += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400280 }
281 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200282 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400283 return 1;
284
285 return 0;
286}
287
Ben Gardinerc9f73512010-07-05 13:27:07 -0400288#ifdef CONFIG_ENV_OFFSET_OOB
289int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
290{
291 struct mtd_oob_ops ops;
292 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
293 int ret;
294
295 ops.datbuf = NULL;
296 ops.mode = MTD_OOB_AUTO;
297 ops.ooboffs = 0;
298 ops.ooblen = ENV_OFFSET_SIZE;
299 ops.oobbuf = (void *) oob_buf;
300
301 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500302 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400303 printf("error reading OOB block 0\n");
Scott Wood53504a22010-07-12 18:17:40 -0500304 return ret;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400305 }
Scott Wood53504a22010-07-12 18:17:40 -0500306
307 if (oob_buf[0] == ENV_OOB_MARKER) {
308 *result = oob_buf[1] * nand->erasesize;
309 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
310 *result = oob_buf[1];
311 } else {
312 printf("No dynamic environment marker in OOB block 0\n");
313 return -ENOENT;
314 }
315
316 return 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400317}
318#endif
319
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200320#ifdef CONFIG_ENV_OFFSET_REDUND
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100321void env_relocate_spec (void)
322{
323#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100324 int crc1_ok = 0, crc2_ok = 0;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100325 env_t *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000326
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200327 tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
328 tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100329
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700330 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
331 puts("Can't allocate buffers for environment\n");
332 free (tmp_env1);
333 free (tmp_env2);
334 return use_default();
335 }
336
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200337 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400338 puts("No Valid Environment Area Found\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200339 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400340 puts("No Valid Reundant Environment Area Found\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100341
342 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
343 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
344
derek@siconix.com5a9427d2009-01-26 14:08:17 -0700345 if(!crc1_ok && !crc2_ok) {
346 free(tmp_env1);
347 free(tmp_env2);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100348 return use_default();
derek@siconix.com5a9427d2009-01-26 14:08:17 -0700349 } else if(crc1_ok && !crc2_ok)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100350 gd->env_valid = 1;
351 else if(!crc1_ok && crc2_ok)
352 gd->env_valid = 2;
353 else {
354 /* both ok - check serial */
355 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
356 gd->env_valid = 2;
357 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
358 gd->env_valid = 1;
359 else if(tmp_env1->flags > tmp_env2->flags)
360 gd->env_valid = 1;
361 else if(tmp_env2->flags > tmp_env1->flags)
362 gd->env_valid = 2;
363 else /* flags are equal - almost impossible */
364 gd->env_valid = 1;
365
366 }
367
368 free(env_ptr);
369 if(gd->env_valid == 1) {
370 env_ptr = tmp_env1;
371 free(tmp_env2);
372 } else {
373 env_ptr = tmp_env2;
374 free(tmp_env1);
375 }
376
377#endif /* ! ENV_IS_EMBEDDED */
378}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200379#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100380/*
381 * The legacy NAND code saved the environment in the first NAND device i.e.,
382 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
383 */
wdenk13a56952004-06-09 14:58:14 +0000384void env_relocate_spec (void)
385{
386#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100387 int ret;
wdenk13a56952004-06-09 14:58:14 +0000388
Ben Gardinerc9f73512010-07-05 13:27:07 -0400389#if defined(CONFIG_ENV_OFFSET_OOB)
390 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
391 /* If unable to read environment offset from NAND OOB then fall through
392 * to the normal environment reading code below
393 */
394 if (!ret)
395 printf("Found Environment offset in OOB..\n");
396 else
397 return use_default();
398#endif
399
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200400 ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200401 if (ret)
wdenk13a56952004-06-09 14:58:14 +0000402 return use_default();
403
404 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
405 return use_default();
406#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000407}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200408#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000409
Stefan Roesed12ae802006-09-12 20:19:10 +0200410#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +0000411static void use_default()
412{
wdenk13a56952004-06-09 14:58:14 +0000413 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +0800414 set_default_env();
wdenk13a56952004-06-09 14:58:14 +0000415}
Stefan Roesed12ae802006-09-12 20:19:10 +0200416#endif