blob: ce13def90684e6736ef7cfc97721584c34843c69 [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)
Igor Grinbergde152b92011-11-07 01:14:10 +000047#error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000048#endif
49
Igor Grinbergde152b92011-11-07 01:14:10 +000050#if defined(CONFIG_ENV_SIZE_REDUND) && \
51 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020052#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000053#endif
54
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020055#ifndef CONFIG_ENV_RANGE
56#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Woodcc49cad2008-05-30 16:05:28 -040057#endif
58
Wolfgang Denkea882ba2010-06-20 23:33:59 +020059char *env_name_spec = "NAND";
wdenk13a56952004-06-09 14:58:14 +000060
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 */
Igor Grinbergde152b92011-11-07 01:14:10 +000066env_t *env_ptr;
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
Igor Grinbergde152b92011-11-07 01:14:10 +000071uchar env_get_char_spec(int index)
wdenk13a56952004-06-09 14:58:14 +000072{
Igor Grinbergde152b92011-11-07 01:14:10 +000073 return *((uchar *)(gd->env_addr + index));
wdenk13a56952004-06-09 14:58:14 +000074}
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);
Igor Grinbergde152b92011-11-07 01:14:10 +000098 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020099#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200100 tmp_env1 = env_ptr;
Igor Grinbergde152b92011-11-07 01:14:10 +0000101 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
Stefan Roesed12ae802006-09-12 20:19:10 +0200102
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200103 if (!crc1_ok && !crc2_ok) {
Igor Grinbergde152b92011-11-07 01:14:10 +0000104 gd->env_addr = 0;
105 gd->env_valid = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200106
107 return 0;
108 } else if (crc1_ok && !crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200109 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200110 }
111#ifdef CONFIG_ENV_OFFSET_REDUND
112 else if (!crc1_ok && crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200113 gd->env_valid = 2;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200114 } else {
Stefan Roesed12ae802006-09-12 20:19:10 +0200115 /* both ok - check serial */
Igor Grinbergde152b92011-11-07 01:14:10 +0000116 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Stefan Roesed12ae802006-09-12 20:19:10 +0200117 gd->env_valid = 2;
Igor Grinbergde152b92011-11-07 01:14:10 +0000118 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Stefan Roesed12ae802006-09-12 20:19:10 +0200119 gd->env_valid = 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000120 else if (tmp_env1->flags > tmp_env2->flags)
Stefan Roesed12ae802006-09-12 20:19:10 +0200121 gd->env_valid = 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000122 else if (tmp_env2->flags > tmp_env1->flags)
Stefan Roesed12ae802006-09-12 20:19:10 +0200123 gd->env_valid = 2;
124 else /* flags are equal - almost impossible */
125 gd->env_valid = 1;
126 }
127
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200128 if (gd->env_valid == 2)
129 env_ptr = tmp_env2;
130 else
131#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200132 if (gd->env_valid == 1)
133 env_ptr = tmp_env1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200134
135 gd->env_addr = (ulong)env_ptr->data;
136
137#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Igor Grinbergde152b92011-11-07 01:14:10 +0000138 gd->env_addr = (ulong)&default_environment[0];
139 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200140#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk13a56952004-06-09 14:58:14 +0000141
Igor Grinbergde152b92011-11-07 01:14:10 +0000142 return 0;
wdenk13a56952004-06-09 14:58:14 +0000143}
144
145#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100146/*
147 * The legacy NAND code saved the environment in the first NAND device i.e.,
148 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
149 */
Stuart Woodcc49cad2008-05-30 16:05:28 -0400150int writeenv(size_t offset, u_char *buf)
151{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200152 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400153 size_t amount_saved = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200154 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400155 u_char *char_ptr;
156
157 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200158 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400159
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200160 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400161 if (nand_block_isbad(&nand_info[0], offset)) {
162 offset += blocksize;
163 } else {
164 char_ptr = &buf[amount_saved];
Igor Grinbergde152b92011-11-07 01:14:10 +0000165 if (nand_write(&nand_info[0], offset, &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400166 return 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000167
Stuart Woodcc49cad2008-05-30 16:05:28 -0400168 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200169 amount_saved += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400170 }
171 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200172 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400173 return 1;
174
175 return 0;
176}
Scott Woodeef1d712011-02-08 15:25:02 -0600177
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200178#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Woodeef1d712011-02-08 15:25:02 -0600179static unsigned char env_flags;
180
wdenk13a56952004-06-09 14:58:14 +0000181int saveenv(void)
182{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200183 env_t env_new;
184 ssize_t len;
185 char *res;
186 int ret = 0;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400187 nand_erase_options_t nand_erase_options;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100188
Daniel Hobi3b250ff2011-05-18 15:21:08 +0200189 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200190 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400191
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200192 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400193 return 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200194
195 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100196 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197 if (len < 0) {
198 error("Cannot export environment: errno = %d\n", errno);
199 return 1;
200 }
Igor Grinbergde152b92011-11-07 01:14:10 +0000201 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
202 env_new.flags = ++env_flags; /* increase the serial */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200203
Igor Grinbergde152b92011-11-07 01:14:10 +0000204 if (gd->env_valid == 1) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200205 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
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200210 puts("Writing to redundant NAND... ");
Igor Grinbergde152b92011-11-07 01:14:10 +0000211 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100212 } else {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200213 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
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200218 puts("Writing to NAND... ");
Igor Grinbergde152b92011-11-07 01:14:10 +0000219 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
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
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200226 puts("done\n");
227
Igor Grinbergde152b92011-11-07 01:14:10 +0000228 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200229
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100230 return ret;
231}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200232#else /* ! CONFIG_ENV_OFFSET_REDUND */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100233int saveenv(void)
234{
Igor Grinbergde152b92011-11-07 01:14:10 +0000235 int ret = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200236 env_t env_new;
237 ssize_t len;
238 char *res;
Philip Balister9e4006bc2008-06-16 08:58:07 -0400239 nand_erase_options_t nand_erase_options;
Wolfgang Denke093a242008-06-28 23:34:37 +0200240
Daniel Hobi3b250ff2011-05-18 15:21:08 +0200241 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200242 nand_erase_options.length = CONFIG_ENV_RANGE;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200243 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400244
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200245 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400246 return 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247
248 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100249 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200250 if (len < 0) {
251 error("Cannot export environment: errno = %d\n", errno);
252 return 1;
253 }
Igor Grinbergde152b92011-11-07 01:14:10 +0000254 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200255
256 puts("Erasing Nand...\n");
Stuart Woodcc49cad2008-05-30 16:05:28 -0400257 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
wdenk13a56952004-06-09 14:58:14 +0000258 return 1;
259
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200260 puts("Writing to Nand... ");
261 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400262 puts("FAILED!\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100263 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400264 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100265
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200266 puts("done\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100267 return ret;
wdenk13a56952004-06-09 14:58:14 +0000268}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200269#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000270#endif /* CMD_SAVEENV */
271
Igor Grinbergde152b92011-11-07 01:14:10 +0000272int readenv(size_t offset, u_char *buf)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400273{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200274 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400275 size_t amount_loaded = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200276 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400277 u_char *char_ptr;
278
279 blocksize = nand_info[0].erasesize;
Mike Frysinger962ad592010-08-11 23:42:26 -0400280 if (!blocksize)
281 return 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000282
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200283 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400284
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200285 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400286 if (nand_block_isbad(&nand_info[0], offset)) {
287 offset += blocksize;
288 } else {
289 char_ptr = &buf[amount_loaded];
Igor Grinbergde152b92011-11-07 01:14:10 +0000290 if (nand_read_skip_bad(&nand_info[0], offset,
291 &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400292 return 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000293
Stuart Woodcc49cad2008-05-30 16:05:28 -0400294 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200295 amount_loaded += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400296 }
297 }
Igor Grinbergde152b92011-11-07 01:14:10 +0000298
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200299 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400300 return 1;
301
302 return 0;
303}
304
Ben Gardinerc9f73512010-07-05 13:27:07 -0400305#ifdef CONFIG_ENV_OFFSET_OOB
306int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
307{
308 struct mtd_oob_ops ops;
Igor Grinbergde152b92011-11-07 01:14:10 +0000309 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
Ben Gardinerc9f73512010-07-05 13:27:07 -0400310 int ret;
311
Igor Grinbergde152b92011-11-07 01:14:10 +0000312 ops.datbuf = NULL;
313 ops.mode = MTD_OOB_AUTO;
314 ops.ooboffs = 0;
315 ops.ooblen = ENV_OFFSET_SIZE;
316 ops.oobbuf = (void *)oob_buf;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400317
318 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500319 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400320 printf("error reading OOB block 0\n");
Scott Wood53504a22010-07-12 18:17:40 -0500321 return ret;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400322 }
Scott Wood53504a22010-07-12 18:17:40 -0500323
324 if (oob_buf[0] == ENV_OOB_MARKER) {
325 *result = oob_buf[1] * nand->erasesize;
326 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
327 *result = oob_buf[1];
328 } else {
329 printf("No dynamic environment marker in OOB block 0\n");
330 return -ENOENT;
331 }
332
333 return 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400334}
335#endif
336
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200337#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200338void env_relocate_spec(void)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100339{
340#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100341 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200342 env_t *ep, *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000343
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200344 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
345 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Igor Grinbergde152b92011-11-07 01:14:10 +0000346 if (tmp_env1 == NULL || tmp_env2 == NULL) {
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700347 puts("Can't allocate buffers for environment\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200348 set_default_env("!malloc() failed");
Igor Grinbergde152b92011-11-07 01:14:10 +0000349 goto done;
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700350 }
351
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200352 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200353 puts("No Valid Environment Area found\n");
354
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200355 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200356 puts("No Valid Redundant Environment Area found\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100357
Igor Grinbergde152b92011-11-07 01:14:10 +0000358 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
359 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100360
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200361 if (!crc1_ok && !crc2_ok) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200362 set_default_env("!bad CRC");
Igor Grinbergde152b92011-11-07 01:14:10 +0000363 goto done;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200364 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100365 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200366 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100367 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200368 } else {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100369 /* both ok - check serial */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200370 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100371 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200372 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100373 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200374 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100375 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200376 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100377 gd->env_valid = 2;
378 else /* flags are equal - almost impossible */
379 gd->env_valid = 1;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100380 }
381
382 free(env_ptr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200383
384 if (gd->env_valid == 1)
385 ep = tmp_env1;
386 else
387 ep = tmp_env2;
388
Scott Woodeef1d712011-02-08 15:25:02 -0600389 env_flags = ep->flags;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200390 env_import((char *)ep, 0);
391
Igor Grinbergde152b92011-11-07 01:14:10 +0000392done:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200393 free(tmp_env1);
394 free(tmp_env2);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100395
396#endif /* ! ENV_IS_EMBEDDED */
397}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200398#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100399/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200400 * The legacy NAND code saved the environment in the first NAND
401 * device i.e., nand_dev_desc + 0. This is also the behaviour using
402 * the new NAND code.
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100403 */
Igor Grinbergde152b92011-11-07 01:14:10 +0000404void env_relocate_spec(void)
wdenk13a56952004-06-09 14:58:14 +0000405{
406#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100407 int ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200408 char buf[CONFIG_ENV_SIZE];
wdenk13a56952004-06-09 14:58:14 +0000409
Ben Gardinerc9f73512010-07-05 13:27:07 -0400410#if defined(CONFIG_ENV_OFFSET_OOB)
411 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200412 /*
413 * If unable to read environment offset from NAND OOB then fall through
Ben Gardinerc9f73512010-07-05 13:27:07 -0400414 * to the normal environment reading code below
415 */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200416 if (!ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400417 printf("Found Environment offset in OOB..\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200418 } else {
419 set_default_env("!no env offset in OOB");
420 return;
421 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400422#endif
423
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200424 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
425 if (ret) {
426 set_default_env("!readenv() failed");
427 return;
428 }
wdenk13a56952004-06-09 14:58:14 +0000429
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200430 env_import(buf, 1);
wdenk13a56952004-06-09 14:58:14 +0000431#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000432}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200433#endif /* CONFIG_ENV_OFFSET_REDUND */