blob: 90a1c454720ae4fe5ad483cf78cddbba5b4194ec [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>
wdenk13a56952004-06-09 14:58:14 +000041
Mike Frysingerbdab39d2009-01-28 19:08:14 -050042#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk13a56952004-06-09 14:58:14 +000043#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020044#elif defined(CONFIG_ENV_OFFSET_REDUND)
Mike Frysingerbdab39d2009-01-28 19:08:14 -050045#error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000046#endif
47
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020048#if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
49#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000050#endif
51
wdenk13a56952004-06-09 14:58:14 +000052#ifdef CONFIG_INFERNO
53#error CONFIG_INFERNO not supported yet
54#endif
55
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020056#ifndef CONFIG_ENV_RANGE
57#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Woodcc49cad2008-05-30 16:05:28 -040058#endif
59
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010060int nand_legacy_rw (struct nand_chip* nand, int cmd,
wdenk13a56952004-06-09 14:58:14 +000061 size_t start, size_t len,
62 size_t * retlen, u_char * buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010063
wdenk13a56952004-06-09 14:58:14 +000064/* references to names in env_common.c */
65extern uchar default_environment[];
66extern int default_environment_size;
67
68char * env_name_spec = "NAND";
69
70
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020071#if defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +000072extern uchar environment[];
73env_t *env_ptr = (env_t *)(&environment[0]);
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020074#elif defined(CONFIG_NAND_ENV_DST)
75env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk13a56952004-06-09 14:58:14 +000076#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000077env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000078#endif /* ENV_IS_EMBEDDED */
79
80
81/* local functions */
Stefan Roesed12ae802006-09-12 20:19:10 +020082#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +000083static void use_default(void);
Stefan Roesed12ae802006-09-12 20:19:10 +020084#endif
wdenk13a56952004-06-09 14:58:14 +000085
Wolfgang Denkd87080b2006-03-31 18:32:53 +020086DECLARE_GLOBAL_DATA_PTR;
wdenk13a56952004-06-09 14:58:14 +000087
88uchar env_get_char_spec (int index)
89{
wdenk13a56952004-06-09 14:58:14 +000090 return ( *((uchar *)(gd->env_addr + index)) );
91}
92
93
94/* this is called before nand_init()
95 * so we can't read Nand to validate env data.
96 * Mark it OK for now. env_relocate() in env_common.c
Marcel Ziswiler99c2b432008-06-22 16:13:46 +020097 * will call our relocate function which does the real
98 * validation.
Stefan Roesed12ae802006-09-12 20:19:10 +020099 *
100 * When using a NAND boot image (like sequoia_nand), the environment
101 * can be embedded or attached to the U-Boot image in NAND flash. This way
102 * the SPL loads not only the U-Boot image from NAND but also the
103 * environment.
wdenk13a56952004-06-09 14:58:14 +0000104 */
105int env_init(void)
106{
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200107#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesed12ae802006-09-12 20:19:10 +0200108 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200109 env_t *tmp_env1;
110
111#ifdef CONFIG_ENV_OFFSET_REDUND
112 env_t *tmp_env2;
113
114 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
115 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
116#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200117
Stefan Roesed12ae802006-09-12 20:19:10 +0200118 tmp_env1 = env_ptr;
Stefan Roesed12ae802006-09-12 20:19:10 +0200119
120 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
Stefan Roesed12ae802006-09-12 20:19:10 +0200121
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200122 if (!crc1_ok && !crc2_ok) {
123 gd->env_addr = 0;
Stefan Roesed12ae802006-09-12 20:19:10 +0200124 gd->env_valid = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200125
126 return 0;
127 } else if (crc1_ok && !crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200128 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200129 }
130#ifdef CONFIG_ENV_OFFSET_REDUND
131 else if (!crc1_ok && crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200132 gd->env_valid = 2;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200133 } else {
Stefan Roesed12ae802006-09-12 20:19:10 +0200134 /* both ok - check serial */
135 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
136 gd->env_valid = 2;
137 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
138 gd->env_valid = 1;
139 else if(tmp_env1->flags > tmp_env2->flags)
140 gd->env_valid = 1;
141 else if(tmp_env2->flags > tmp_env1->flags)
142 gd->env_valid = 2;
143 else /* flags are equal - almost impossible */
144 gd->env_valid = 1;
145 }
146
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200147 if (gd->env_valid == 2)
148 env_ptr = tmp_env2;
149 else
150#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200151 if (gd->env_valid == 1)
152 env_ptr = tmp_env1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200153
154 gd->env_addr = (ulong)env_ptr->data;
155
156#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100157 gd->env_addr = (ulong)&default_environment[0];
wdenk13a56952004-06-09 14:58:14 +0000158 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200159#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk13a56952004-06-09 14:58:14 +0000160
161 return (0);
162}
163
164#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100165/*
166 * The legacy NAND code saved the environment in the first NAND device i.e.,
167 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
168 */
Stuart Woodcc49cad2008-05-30 16:05:28 -0400169int writeenv(size_t offset, u_char *buf)
170{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200171 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400172 size_t amount_saved = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200173 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400174
175 u_char *char_ptr;
176
177 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200178 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400179
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200180 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400181 if (nand_block_isbad(&nand_info[0], offset)) {
182 offset += blocksize;
183 } else {
184 char_ptr = &buf[amount_saved];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200185 if (nand_write(&nand_info[0], offset, &len,
Stuart Woodcc49cad2008-05-30 16:05:28 -0400186 char_ptr))
187 return 1;
188 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200189 amount_saved += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400190 }
191 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200192 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400193 return 1;
194
195 return 0;
196}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200197#ifdef CONFIG_ENV_OFFSET_REDUND
wdenk13a56952004-06-09 14:58:14 +0000198int saveenv(void)
199{
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100200 int ret = 0;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400201 nand_erase_options_t nand_erase_options;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100202
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100203 env_ptr->flags++;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100204
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200205 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400206 nand_erase_options.quiet = 0;
207 nand_erase_options.jffs2 = 0;
208 nand_erase_options.scrub = 0;
209
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200210 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400211 return 1;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100212 if(gd->env_valid == 1) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400213 puts ("Erasing redundant Nand...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200214 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
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;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400217
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100218 puts ("Writing to redundant Nand... ");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200219 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100220 } else {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400221 puts ("Erasing Nand...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200222 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400223 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100224 return 1;
225
226 puts ("Writing to Nand... ");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200227 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100228 }
Stuart Woodcc49cad2008-05-30 16:05:28 -0400229 if (ret) {
230 puts("FAILED!\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100231 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400232 }
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100233
234 puts ("done\n");
235 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
236 return ret;
237}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200238#else /* ! CONFIG_ENV_OFFSET_REDUND */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100239int saveenv(void)
240{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100241 int ret = 0;
Philip Balister9e4006bc2008-06-16 08:58:07 -0400242 nand_erase_options_t nand_erase_options;
Wolfgang Denke093a242008-06-28 23:34:37 +0200243
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200244 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400245 nand_erase_options.quiet = 0;
246 nand_erase_options.jffs2 = 0;
247 nand_erase_options.scrub = 0;
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;
252 puts ("Erasing Nand...\n");
253 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
wdenk13a56952004-06-09 14:58:14 +0000254 return 1;
255
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100256 puts ("Writing to Nand... ");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200257 if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400258 puts("FAILED!\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100259 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400260 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100261
262 puts ("done\n");
263 return ret;
wdenk13a56952004-06-09 14:58:14 +0000264}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200265#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000266#endif /* CMD_SAVEENV */
267
Stuart Woodcc49cad2008-05-30 16:05:28 -0400268int readenv (size_t offset, u_char * buf)
269{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200270 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400271 size_t amount_loaded = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200272 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400273
274 u_char *char_ptr;
275
276 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200277 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400278
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200279 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400280 if (nand_block_isbad(&nand_info[0], offset)) {
281 offset += blocksize;
282 } else {
283 char_ptr = &buf[amount_loaded];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200284 if (nand_read(&nand_info[0], offset, &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400285 return 1;
286 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200287 amount_loaded += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400288 }
289 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200290 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400291 return 1;
292
293 return 0;
294}
295
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200296#ifdef CONFIG_ENV_OFFSET_REDUND
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100297void env_relocate_spec (void)
298{
299#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100300 int crc1_ok = 0, crc2_ok = 0;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100301 env_t *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000302
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200303 tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
304 tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100305
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200306 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400307 puts("No Valid Environment Area Found\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200308 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400309 puts("No Valid Reundant Environment Area Found\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100310
311 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
312 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
313
derek@siconix.com5a9427d2009-01-26 14:08:17 -0700314 if(!crc1_ok && !crc2_ok) {
315 free(tmp_env1);
316 free(tmp_env2);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100317 return use_default();
derek@siconix.com5a9427d2009-01-26 14:08:17 -0700318 } else if(crc1_ok && !crc2_ok)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100319 gd->env_valid = 1;
320 else if(!crc1_ok && crc2_ok)
321 gd->env_valid = 2;
322 else {
323 /* both ok - check serial */
324 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
325 gd->env_valid = 2;
326 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
327 gd->env_valid = 1;
328 else if(tmp_env1->flags > tmp_env2->flags)
329 gd->env_valid = 1;
330 else if(tmp_env2->flags > tmp_env1->flags)
331 gd->env_valid = 2;
332 else /* flags are equal - almost impossible */
333 gd->env_valid = 1;
334
335 }
336
337 free(env_ptr);
338 if(gd->env_valid == 1) {
339 env_ptr = tmp_env1;
340 free(tmp_env2);
341 } else {
342 env_ptr = tmp_env2;
343 free(tmp_env1);
344 }
345
346#endif /* ! ENV_IS_EMBEDDED */
347}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200348#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100349/*
350 * The legacy NAND code saved the environment in the first NAND device i.e.,
351 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
352 */
wdenk13a56952004-06-09 14:58:14 +0000353void env_relocate_spec (void)
354{
355#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100356 int ret;
wdenk13a56952004-06-09 14:58:14 +0000357
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200358 ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200359 if (ret)
wdenk13a56952004-06-09 14:58:14 +0000360 return use_default();
361
362 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
363 return use_default();
364#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000365}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200366#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000367
Stefan Roesed12ae802006-09-12 20:19:10 +0200368#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +0000369static void use_default()
370{
wdenk13a56952004-06-09 14:58:14 +0000371 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +0800372 set_default_env();
wdenk13a56952004-06-09 14:58:14 +0000373}
Stefan Roesed12ae802006-09-12 20:19:10 +0200374#endif