blob: ce13def90684e6736ef7cfc97721584c34843c69 [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)
Igor Grinberg90f2ca82011-11-07 01:14:10 +000047#error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk79b59372004-06-09 14:58:14 +000048#endif
49
Igor Grinberg90f2ca82011-11-07 01:14:10 +000050#if defined(CONFIG_ENV_SIZE_REDUND) && \
51 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020052#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk79b59372004-06-09 14:58:14 +000053#endif
54
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020055#ifndef CONFIG_ENV_RANGE
56#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Wood37e11442008-05-30 16:05:28 -040057#endif
58
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020059char *env_name_spec = "NAND";
wdenk79b59372004-06-09 14:58:14 +000060
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020061#if defined(ENV_IS_EMBEDDED)
Igor Grinberg23b54b92011-11-17 06:07:23 +000062env_t *env_ptr = &environment;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020063#elif defined(CONFIG_NAND_ENV_DST)
64env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk79b59372004-06-09 14:58:14 +000065#else /* ! ENV_IS_EMBEDDED */
Igor Grinberg90f2ca82011-11-07 01:14:10 +000066env_t *env_ptr;
wdenk79b59372004-06-09 14:58:14 +000067#endif /* ENV_IS_EMBEDDED */
68
Wolfgang Denk6405a152006-03-31 18:32:53 +020069DECLARE_GLOBAL_DATA_PTR;
wdenk79b59372004-06-09 14:58:14 +000070
Igor Grinberg90f2ca82011-11-07 01:14:10 +000071uchar env_get_char_spec(int index)
wdenk79b59372004-06-09 14:58:14 +000072{
Igor Grinberg90f2ca82011-11-07 01:14:10 +000073 return *((uchar *)(gd->env_addr + index));
wdenk79b59372004-06-09 14:58:14 +000074}
75
Wolfgang Denk460a9ff2010-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 Roesebbfcbb72006-09-12 20:19:10 +020082 *
83 * When using a NAND boot image (like sequoia_nand), the environment
Wolfgang Denk460a9ff2010-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.
wdenk79b59372004-06-09 14:58:14 +000087 */
88int env_init(void)
89{
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020090#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesebbfcbb72006-09-12 20:19:10 +020091 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020092 env_t *tmp_env1;
Stefan Roesebbfcbb72006-09-12 20:19:10 +020093
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020094#ifdef CONFIG_ENV_OFFSET_REDUND
95 env_t *tmp_env2;
96
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020097 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
Igor Grinberg90f2ca82011-11-07 01:14:10 +000098 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020099#endif
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200100 tmp_env1 = env_ptr;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000101 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200102
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200103 if (!crc1_ok && !crc2_ok) {
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000104 gd->env_addr = 0;
105 gd->env_valid = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200106
107 return 0;
108 } else if (crc1_ok && !crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200109 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200110 }
111#ifdef CONFIG_ENV_OFFSET_REDUND
112 else if (!crc1_ok && crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200113 gd->env_valid = 2;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200114 } else {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200115 /* both ok - check serial */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000116 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200117 gd->env_valid = 2;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000118 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200119 gd->env_valid = 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000120 else if (tmp_env1->flags > tmp_env2->flags)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200121 gd->env_valid = 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000122 else if (tmp_env2->flags > tmp_env1->flags)
Stefan Roesebbfcbb72006-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 Liakhovetskifad24442009-05-18 16:07:22 +0200128 if (gd->env_valid == 2)
129 env_ptr = tmp_env2;
130 else
131#endif
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200132 if (gd->env_valid == 1)
133 env_ptr = tmp_env1;
Guennadi Liakhovetskifad24442009-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 Grinberg90f2ca82011-11-07 01:14:10 +0000138 gd->env_addr = (ulong)&default_environment[0];
139 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200140#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk79b59372004-06-09 14:58:14 +0000141
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000142 return 0;
wdenk79b59372004-06-09 14:58:14 +0000143}
144
145#ifdef CMD_SAVEENV
Bartlomiej Sieka582f1a32006-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 Wood37e11442008-05-30 16:05:28 -0400150int writeenv(size_t offset, u_char *buf)
151{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200152 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400153 size_t amount_saved = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200154 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400155 u_char *char_ptr;
156
157 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200158 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400159
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200160 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-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 Grinberg90f2ca82011-11-07 01:14:10 +0000165 if (nand_write(&nand_info[0], offset, &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400166 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000167
Stuart Wood37e11442008-05-30 16:05:28 -0400168 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200169 amount_saved += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400170 }
171 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200172 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400173 return 1;
174
175 return 0;
176}
Scott Wood00eed7f2011-02-08 15:25:02 -0600177
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200178#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Wood00eed7f2011-02-08 15:25:02 -0600179static unsigned char env_flags;
180
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100181int saveenv(void)
182{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200183 env_t env_new;
184 ssize_t len;
185 char *res;
186 int ret = 0;
Stuart Wood37e11442008-05-30 16:05:28 -0400187 nand_erase_options_t nand_erase_options;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100188
Daniel Hobi201e7a22011-05-18 15:21:08 +0200189 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200190 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400191
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200192 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400193 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200194
195 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100196 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200197 if (len < 0) {
198 error("Cannot export environment: errno = %d\n", errno);
199 return 1;
200 }
Igor Grinberg90f2ca82011-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 Denk460a9ff2010-06-20 23:33:59 +0200203
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000204 if (gd->env_valid == 1) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200205 puts("Erasing redundant NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200206 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Wood37e11442008-05-30 16:05:28 -0400207 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100208 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400209
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200210 puts("Writing to redundant NAND... ");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000211 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100212 } else {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200213 puts("Erasing NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200214 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400215 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100216 return 1;
217
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200218 puts("Writing to NAND... ");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000219 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100220 }
Stuart Wood37e11442008-05-30 16:05:28 -0400221 if (ret) {
222 puts("FAILED!\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100223 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400224 }
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100225
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200226 puts("done\n");
227
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000228 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200229
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100230 return ret;
231}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200232#else /* ! CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000233int saveenv(void)
234{
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000235 int ret = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200236 env_t env_new;
237 ssize_t len;
238 char *res;
Philip Balister8c57e9a2008-06-16 08:58:07 -0400239 nand_erase_options_t nand_erase_options;
Wolfgang Denk41df50a2008-06-28 23:34:37 +0200240
Daniel Hobi201e7a22011-05-18 15:21:08 +0200241 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200242 nand_erase_options.length = CONFIG_ENV_RANGE;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200243 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400244
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200245 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400246 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200247
248 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100249 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200250 if (len < 0) {
251 error("Cannot export environment: errno = %d\n", errno);
252 return 1;
253 }
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000254 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200255
256 puts("Erasing Nand...\n");
Stuart Wood37e11442008-05-30 16:05:28 -0400257 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100258 return 1;
wdenk79b59372004-06-09 14:58:14 +0000259
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200260 puts("Writing to Nand... ");
261 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
Stuart Wood37e11442008-05-30 16:05:28 -0400262 puts("FAILED!\n");
wdenk79b59372004-06-09 14:58:14 +0000263 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400264 }
wdenk79b59372004-06-09 14:58:14 +0000265
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200266 puts("done\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100267 return ret;
wdenk79b59372004-06-09 14:58:14 +0000268}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200269#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000270#endif /* CMD_SAVEENV */
271
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000272int readenv(size_t offset, u_char *buf)
Stuart Wood37e11442008-05-30 16:05:28 -0400273{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200274 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400275 size_t amount_loaded = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200276 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400277 u_char *char_ptr;
278
279 blocksize = nand_info[0].erasesize;
Mike Frysinger3dc4ccd2010-08-11 23:42:26 -0400280 if (!blocksize)
281 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000282
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200283 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400284
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200285 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-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 Grinberg90f2ca82011-11-07 01:14:10 +0000290 if (nand_read_skip_bad(&nand_info[0], offset,
291 &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400292 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000293
Stuart Wood37e11442008-05-30 16:05:28 -0400294 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200295 amount_loaded += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400296 }
297 }
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000298
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200299 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400300 return 1;
301
302 return 0;
303}
304
Ben Gardiner5fc8b7f2010-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 Grinberg90f2ca82011-11-07 01:14:10 +0000309 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400310 int ret;
311
Igor Grinberg90f2ca82011-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 Gardiner5fc8b7f2010-07-05 13:27:07 -0400317
318 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500319 if (ret) {
320 printf("error reading OOB block 0\n");
321 return ret;
322 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400323
Scott Wood0268da02010-07-12 18:17:40 -0500324 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];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400328 } else {
Scott Wood0268da02010-07-12 18:17:40 -0500329 printf("No dynamic environment marker in OOB block 0\n");
330 return -ENOENT;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400331 }
Scott Wood0268da02010-07-12 18:17:40 -0500332
333 return 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400334}
335#endif
336
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200337#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200338void env_relocate_spec(void)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100339{
340#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecherffe7ef52006-03-24 15:43:16 +0100341 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200342 env_t *ep, *tmp_env1, *tmp_env2;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100343
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200344 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
345 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000346 if (tmp_env1 == NULL || tmp_env2 == NULL) {
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700347 puts("Can't allocate buffers for environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200348 set_default_env("!malloc() failed");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000349 goto done;
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700350 }
351
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200352 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200353 puts("No Valid Environment Area found\n");
354
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200355 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200356 puts("No Valid Redundant Environment Area found\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100357
Igor Grinberg90f2ca82011-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 Klotzbuecher5d113e02006-03-20 18:02:44 +0100360
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200361 if (!crc1_ok && !crc2_ok) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200362 set_default_env("!bad CRC");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000363 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200364 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100365 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200366 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100367 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200368 } else {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100369 /* both ok - check serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200370 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100371 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200372 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100373 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200374 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100375 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200376 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100377 gd->env_valid = 2;
378 else /* flags are equal - almost impossible */
379 gd->env_valid = 1;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100380 }
381
382 free(env_ptr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200383
384 if (gd->env_valid == 1)
385 ep = tmp_env1;
386 else
387 ep = tmp_env2;
388
Scott Wood00eed7f2011-02-08 15:25:02 -0600389 env_flags = ep->flags;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200390 env_import((char *)ep, 0);
391
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000392done:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200393 free(tmp_env1);
394 free(tmp_env2);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100395
396#endif /* ! ENV_IS_EMBEDDED */
397}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200398#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100399/*
Wolfgang Denk460a9ff2010-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 Sieka582f1a32006-03-05 18:57:33 +0100403 */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000404void env_relocate_spec(void)
wdenk79b59372004-06-09 14:58:14 +0000405{
406#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100407 int ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200408 char buf[CONFIG_ENV_SIZE];
wdenk79b59372004-06-09 14:58:14 +0000409
Ben Gardiner5fc8b7f2010-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 Denk460a9ff2010-06-20 23:33:59 +0200412 /*
413 * If unable to read environment offset from NAND OOB then fall through
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400414 * to the normal environment reading code below
415 */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200416 if (!ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400417 printf("Found Environment offset in OOB..\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200418 } else {
419 set_default_env("!no env offset in OOB");
420 return;
421 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400422#endif
423
Wolfgang Denk460a9ff2010-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 }
wdenk79b59372004-06-09 14:58:14 +0000429
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200430 env_import(buf, 1);
wdenk79b59372004-06-09 14:58:14 +0000431#endif /* ! ENV_IS_EMBEDDED */
wdenk79b59372004-06-09 14:58:14 +0000432}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200433#endif /* CONFIG_ENV_OFFSET_REDUND */