blob: 980425a8ee8df5d4592cf25a53170dbdd2b8813e [file] [log] [blame]
wdenk79b59372004-06-09 14:58:14 +00001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Stuart Wood37e11442008-05-30 16:05:28 -04005 * (C) Copyright 2008
6 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
7 *
wdenk79b59372004-06-09 14:58:14 +00008 * (C) Copyright 2004
9 * Jian Zhang, Texas Instruments, jzhang@ti.com.
wdenk79b59372004-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 Denk460a9ff2010-06-20 23:33:59 +020013 *
wdenk79b59372004-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 Denk460a9ff2010-06-20 23:33:59 +020033#define DEBUG
wdenk79b59372004-06-09 14:58:14 +000034
35#include <common.h>
wdenk79b59372004-06-09 14:58:14 +000036#include <command.h>
37#include <environment.h>
38#include <linux/stddef.h>
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +010039#include <malloc.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010040#include <nand.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020041#include <search.h>
42#include <errno.h>
wdenk79b59372004-06-09 14:58:14 +000043
Mike Frysinger78dcaf42009-01-28 19:08:14 -050044#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk79b59372004-06-09 14:58:14 +000045#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020046#elif defined(CONFIG_ENV_OFFSET_REDUND)
Mike Frysinger78dcaf42009-01-28 19:08:14 -050047#error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk79b59372004-06-09 14:58:14 +000048#endif
49
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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
wdenk79b59372004-06-09 14:58:14 +000052#endif
53
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020054#ifndef CONFIG_ENV_RANGE
55#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Wood37e11442008-05-30 16:05:28 -040056#endif
57
wdenk79b59372004-06-09 14:58:14 +000058/* references to names in env_common.c */
59extern uchar default_environment[];
wdenk79b59372004-06-09 14:58:14 +000060
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020061char *env_name_spec = "NAND";
wdenk79b59372004-06-09 14:58:14 +000062
63
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020064#if defined(ENV_IS_EMBEDDED)
wdenk79b59372004-06-09 14:58:14 +000065extern uchar environment[];
66env_t *env_ptr = (env_t *)(&environment[0]);
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020067#elif defined(CONFIG_NAND_ENV_DST)
68env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk79b59372004-06-09 14:58:14 +000069#else /* ! ENV_IS_EMBEDDED */
wdenk9e930b62004-06-19 21:19:10 +000070env_t *env_ptr = 0;
wdenk79b59372004-06-09 14:58:14 +000071#endif /* ENV_IS_EMBEDDED */
72
Wolfgang Denk6405a152006-03-31 18:32:53 +020073DECLARE_GLOBAL_DATA_PTR;
wdenk79b59372004-06-09 14:58:14 +000074
75uchar env_get_char_spec (int index)
76{
wdenk79b59372004-06-09 14:58:14 +000077 return ( *((uchar *)(gd->env_addr + index)) );
78}
79
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020080/*
81 * This is called before nand_init() so we can't read NAND to
82 * validate env data.
83 *
84 * Mark it OK for now. env_relocate() in env_common.c will call our
85 * relocate function which does the real validation.
Stefan Roesebbfcbb72006-09-12 20:19:10 +020086 *
87 * When using a NAND boot image (like sequoia_nand), the environment
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088 * can be embedded or attached to the U-Boot image in NAND flash.
89 * This way the SPL loads not only the U-Boot image from NAND but
90 * also the environment.
wdenk79b59372004-06-09 14:58:14 +000091 */
92int env_init(void)
93{
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020094#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesebbfcbb72006-09-12 20:19:10 +020095 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020096 env_t *tmp_env1;
Stefan Roesebbfcbb72006-09-12 20:19:10 +020097
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020098#ifdef CONFIG_ENV_OFFSET_REDUND
99 env_t *tmp_env2;
100
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200101 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200102 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
103#endif
104
105 tmp_env1 = env_ptr;
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200106
107 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200108
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200109 if (!crc1_ok && !crc2_ok) {
110 gd->env_addr = 0;
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200111 gd->env_valid = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200112
113 return 0;
114 } else if (crc1_ok && !crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200115 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200116 }
117#ifdef CONFIG_ENV_OFFSET_REDUND
118 else if (!crc1_ok && crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200119 gd->env_valid = 2;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200120 } else {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200121 /* both ok - check serial */
122 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
123 gd->env_valid = 2;
124 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
125 gd->env_valid = 1;
126 else if(tmp_env1->flags > tmp_env2->flags)
127 gd->env_valid = 1;
128 else if(tmp_env2->flags > tmp_env1->flags)
129 gd->env_valid = 2;
130 else /* flags are equal - almost impossible */
131 gd->env_valid = 1;
132 }
133
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200134 if (gd->env_valid == 2)
135 env_ptr = tmp_env2;
136 else
137#endif
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200138 if (gd->env_valid == 1)
139 env_ptr = tmp_env1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200140
141 gd->env_addr = (ulong)env_ptr->data;
142
143#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100144 gd->env_addr = (ulong)&default_environment[0];
wdenk79b59372004-06-09 14:58:14 +0000145 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200146#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk79b59372004-06-09 14:58:14 +0000147
148 return (0);
149}
150
151#ifdef CMD_SAVEENV
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100152/*
153 * The legacy NAND code saved the environment in the first NAND device i.e.,
154 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
155 */
Stuart Wood37e11442008-05-30 16:05:28 -0400156int writeenv(size_t offset, u_char *buf)
157{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200158 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400159 size_t amount_saved = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200160 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400161
162 u_char *char_ptr;
163
164 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200165 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400166
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200167 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-05-30 16:05:28 -0400168 if (nand_block_isbad(&nand_info[0], offset)) {
169 offset += blocksize;
170 } else {
171 char_ptr = &buf[amount_saved];
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200172 if (nand_write(&nand_info[0], offset, &len,
Stuart Wood37e11442008-05-30 16:05:28 -0400173 char_ptr))
174 return 1;
175 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200176 amount_saved += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400177 }
178 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200179 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400180 return 1;
181
182 return 0;
183}
Scott Wood00eed7f2011-02-08 15:25:02 -0600184
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200185#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Wood00eed7f2011-02-08 15:25:02 -0600186static unsigned char env_flags;
187
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100188int saveenv(void)
189{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200190 env_t env_new;
191 ssize_t len;
192 char *res;
193 int ret = 0;
Stuart Wood37e11442008-05-30 16:05:28 -0400194 nand_erase_options_t nand_erase_options;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100195
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200196 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400197 nand_erase_options.quiet = 0;
198 nand_erase_options.jffs2 = 0;
199 nand_erase_options.scrub = 0;
200
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200201 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400202 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200203
204 res = (char *)&env_new.data;
Mike Frysinger92d7a992010-12-08 06:26:04 -0500205 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200206 if (len < 0) {
207 error("Cannot export environment: errno = %d\n", errno);
208 return 1;
209 }
210 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Scott Wood00eed7f2011-02-08 15:25:02 -0600211 env_new.flags = ++env_flags; /* increase the serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200212
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100213 if(gd->env_valid == 1) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200214 puts("Erasing redundant NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200215 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Wood37e11442008-05-30 16:05:28 -0400216 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100217 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400218
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200219 puts("Writing to redundant NAND... ");
220 ret = writeenv(CONFIG_ENV_OFFSET_REDUND,
221 (u_char *)&env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100222 } else {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200223 puts("Erasing NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200224 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400225 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100226 return 1;
227
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200228 puts("Writing to NAND... ");
229 ret = writeenv(CONFIG_ENV_OFFSET,
230 (u_char *)&env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100231 }
Stuart Wood37e11442008-05-30 16:05:28 -0400232 if (ret) {
233 puts("FAILED!\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100234 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400235 }
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100236
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200237 puts("done\n");
238
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100239 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200240
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100241 return ret;
242}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200243#else /* ! CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000244int saveenv(void)
245{
Markus Klotzbuecherffe7ef52006-03-24 15:43:16 +0100246 int ret = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200247 env_t env_new;
248 ssize_t len;
249 char *res;
Philip Balister8c57e9a2008-06-16 08:58:07 -0400250 nand_erase_options_t nand_erase_options;
Wolfgang Denk41df50a2008-06-28 23:34:37 +0200251
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200252 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400253 nand_erase_options.quiet = 0;
254 nand_erase_options.jffs2 = 0;
255 nand_erase_options.scrub = 0;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200256 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400257
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200258 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400259 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200260
261 res = (char *)&env_new.data;
Mike Frysinger92d7a992010-12-08 06:26:04 -0500262 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200263 if (len < 0) {
264 error("Cannot export environment: errno = %d\n", errno);
265 return 1;
266 }
267 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
268
269 puts("Erasing Nand...\n");
Stuart Wood37e11442008-05-30 16:05:28 -0400270 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100271 return 1;
wdenk79b59372004-06-09 14:58:14 +0000272
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200273 puts("Writing to Nand... ");
274 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
Stuart Wood37e11442008-05-30 16:05:28 -0400275 puts("FAILED!\n");
wdenk79b59372004-06-09 14:58:14 +0000276 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400277 }
wdenk79b59372004-06-09 14:58:14 +0000278
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200279 puts("done\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100280 return ret;
wdenk79b59372004-06-09 14:58:14 +0000281}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200282#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000283#endif /* CMD_SAVEENV */
284
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200285int readenv(size_t offset, u_char * buf)
Stuart Wood37e11442008-05-30 16:05:28 -0400286{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200287 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400288 size_t amount_loaded = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200289 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400290
291 u_char *char_ptr;
292
293 blocksize = nand_info[0].erasesize;
Mike Frysinger3dc4ccd2010-08-11 23:42:26 -0400294 if (!blocksize)
295 return 1;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200296 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400297
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200298 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-05-30 16:05:28 -0400299 if (nand_block_isbad(&nand_info[0], offset)) {
300 offset += blocksize;
301 } else {
302 char_ptr = &buf[amount_loaded];
Steve Sakomanc5e84fa2010-12-03 13:25:43 +0000303 if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400304 return 1;
305 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200306 amount_loaded += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400307 }
308 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200309 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400310 return 1;
311
312 return 0;
313}
314
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400315#ifdef CONFIG_ENV_OFFSET_OOB
316int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
317{
318 struct mtd_oob_ops ops;
319 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
320 int ret;
321
322 ops.datbuf = NULL;
323 ops.mode = MTD_OOB_AUTO;
324 ops.ooboffs = 0;
325 ops.ooblen = ENV_OFFSET_SIZE;
326 ops.oobbuf = (void *) oob_buf;
327
328 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500329 if (ret) {
330 printf("error reading OOB block 0\n");
331 return ret;
332 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400333
Scott Wood0268da02010-07-12 18:17:40 -0500334 if (oob_buf[0] == ENV_OOB_MARKER) {
335 *result = oob_buf[1] * nand->erasesize;
336 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
337 *result = oob_buf[1];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400338 } else {
Scott Wood0268da02010-07-12 18:17:40 -0500339 printf("No dynamic environment marker in OOB block 0\n");
340 return -ENOENT;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400341 }
Scott Wood0268da02010-07-12 18:17:40 -0500342
343 return 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400344}
345#endif
346
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200347#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200348void env_relocate_spec(void)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100349{
350#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecherffe7ef52006-03-24 15:43:16 +0100351 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200352 env_t *ep, *tmp_env1, *tmp_env2;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100353
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200354 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
355 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100356
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700357 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
358 puts("Can't allocate buffers for environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200359 free(tmp_env1);
360 free(tmp_env2);
361 set_default_env("!malloc() failed");
362 return;
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700363 }
364
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200365 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200366 puts("No Valid Environment Area found\n");
367
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200368 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200369 puts("No Valid Redundant Environment Area found\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100370
371 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
372 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
373
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200374 if (!crc1_ok && !crc2_ok) {
derek@siconix.com49043852009-01-26 14:08:17 -0700375 free(tmp_env1);
376 free(tmp_env2);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200377 set_default_env("!bad CRC");
378 return;
379 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100380 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200381 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100382 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200383 } else {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100384 /* both ok - check serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200385 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100386 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200387 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100388 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200389 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100390 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200391 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100392 gd->env_valid = 2;
393 else /* flags are equal - almost impossible */
394 gd->env_valid = 1;
wdenk79b59372004-06-09 14:58:14 +0000395
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100396 }
397
398 free(env_ptr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200399
400 if (gd->env_valid == 1)
401 ep = tmp_env1;
402 else
403 ep = tmp_env2;
404
Scott Wood00eed7f2011-02-08 15:25:02 -0600405 env_flags = ep->flags;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200406 env_import((char *)ep, 0);
407
408 free(tmp_env1);
409 free(tmp_env2);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100410
411#endif /* ! ENV_IS_EMBEDDED */
412}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200413#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100414/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200415 * The legacy NAND code saved the environment in the first NAND
416 * device i.e., nand_dev_desc + 0. This is also the behaviour using
417 * the new NAND code.
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100418 */
wdenk79b59372004-06-09 14:58:14 +0000419void env_relocate_spec (void)
420{
421#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100422 int ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200423 char buf[CONFIG_ENV_SIZE];
wdenk79b59372004-06-09 14:58:14 +0000424
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400425#if defined(CONFIG_ENV_OFFSET_OOB)
426 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200427 /*
428 * If unable to read environment offset from NAND OOB then fall through
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400429 * to the normal environment reading code below
430 */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200431 if (!ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400432 printf("Found Environment offset in OOB..\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200433 } else {
434 set_default_env("!no env offset in OOB");
435 return;
436 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400437#endif
438
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200439 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
440 if (ret) {
441 set_default_env("!readenv() failed");
442 return;
443 }
wdenk79b59372004-06-09 14:58:14 +0000444
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200445 env_import(buf, 1);
wdenk79b59372004-06-09 14:58:14 +0000446#endif /* ! ENV_IS_EMBEDDED */
wdenk79b59372004-06-09 14:58:14 +0000447}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200448#endif /* CONFIG_ENV_OFFSET_REDUND */