blob: 22e72a20b07b74ce8552a31e2d02b4b619d9fe6e [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
wdenk79b59372004-06-09 14:58:14 +000033#include <common.h>
wdenk79b59372004-06-09 14:58:14 +000034#include <command.h>
35#include <environment.h>
36#include <linux/stddef.h>
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +010037#include <malloc.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010038#include <nand.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020039#include <search.h>
40#include <errno.h>
wdenk79b59372004-06-09 14:58:14 +000041
Mike Frysinger78dcaf42009-01-28 19:08:14 -050042#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk79b59372004-06-09 14:58:14 +000043#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020044#elif defined(CONFIG_ENV_OFFSET_REDUND)
Igor Grinberg90f2ca82011-11-07 01:14:10 +000045#error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk79b59372004-06-09 14:58:14 +000046#endif
47
Igor Grinberg90f2ca82011-11-07 01:14:10 +000048#if defined(CONFIG_ENV_SIZE_REDUND) && \
49 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020050#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk79b59372004-06-09 14:58:14 +000051#endif
52
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020053#ifndef CONFIG_ENV_RANGE
54#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Wood37e11442008-05-30 16:05:28 -040055#endif
56
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020057char *env_name_spec = "NAND";
wdenk79b59372004-06-09 14:58:14 +000058
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020059#if defined(ENV_IS_EMBEDDED)
Igor Grinberg23b54b92011-11-17 06:07:23 +000060env_t *env_ptr = &environment;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020061#elif defined(CONFIG_NAND_ENV_DST)
62env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk79b59372004-06-09 14:58:14 +000063#else /* ! ENV_IS_EMBEDDED */
Igor Grinberg90f2ca82011-11-07 01:14:10 +000064env_t *env_ptr;
wdenk79b59372004-06-09 14:58:14 +000065#endif /* ENV_IS_EMBEDDED */
66
Wolfgang Denk6405a152006-03-31 18:32:53 +020067DECLARE_GLOBAL_DATA_PTR;
wdenk79b59372004-06-09 14:58:14 +000068
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020069/*
70 * This is called before nand_init() so we can't read NAND to
71 * validate env data.
72 *
73 * Mark it OK for now. env_relocate() in env_common.c will call our
74 * relocate function which does the real validation.
Stefan Roesebbfcbb72006-09-12 20:19:10 +020075 *
76 * When using a NAND boot image (like sequoia_nand), the environment
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020077 * can be embedded or attached to the U-Boot image in NAND flash.
78 * This way the SPL loads not only the U-Boot image from NAND but
79 * also the environment.
wdenk79b59372004-06-09 14:58:14 +000080 */
81int env_init(void)
82{
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020083#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesebbfcbb72006-09-12 20:19:10 +020084 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020085 env_t *tmp_env1;
Stefan Roesebbfcbb72006-09-12 20:19:10 +020086
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020087#ifdef CONFIG_ENV_OFFSET_REDUND
88 env_t *tmp_env2;
89
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020090 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
Igor Grinberg90f2ca82011-11-07 01:14:10 +000091 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020092#endif
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020093 tmp_env1 = env_ptr;
Igor Grinberg90f2ca82011-11-07 01:14:10 +000094 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
Stefan Roesebbfcbb72006-09-12 20:19:10 +020095
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020096 if (!crc1_ok && !crc2_ok) {
Igor Grinberg90f2ca82011-11-07 01:14:10 +000097 gd->env_addr = 0;
98 gd->env_valid = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020099
100 return 0;
101 } else if (crc1_ok && !crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200102 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200103 }
104#ifdef CONFIG_ENV_OFFSET_REDUND
105 else if (!crc1_ok && crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200106 gd->env_valid = 2;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200107 } else {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200108 /* both ok - check serial */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000109 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200110 gd->env_valid = 2;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000111 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200112 gd->env_valid = 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000113 else if (tmp_env1->flags > tmp_env2->flags)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200114 gd->env_valid = 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000115 else if (tmp_env2->flags > tmp_env1->flags)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200116 gd->env_valid = 2;
117 else /* flags are equal - almost impossible */
118 gd->env_valid = 1;
119 }
120
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200121 if (gd->env_valid == 2)
122 env_ptr = tmp_env2;
123 else
124#endif
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200125 if (gd->env_valid == 1)
126 env_ptr = tmp_env1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200127
128 gd->env_addr = (ulong)env_ptr->data;
129
130#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000131 gd->env_addr = (ulong)&default_environment[0];
132 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200133#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk79b59372004-06-09 14:58:14 +0000134
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000135 return 0;
wdenk79b59372004-06-09 14:58:14 +0000136}
137
138#ifdef CMD_SAVEENV
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100139/*
140 * The legacy NAND code saved the environment in the first NAND device i.e.,
141 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
142 */
Stuart Wood37e11442008-05-30 16:05:28 -0400143int writeenv(size_t offset, u_char *buf)
144{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200145 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400146 size_t amount_saved = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200147 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400148 u_char *char_ptr;
149
150 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200151 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400152
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200153 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-05-30 16:05:28 -0400154 if (nand_block_isbad(&nand_info[0], offset)) {
155 offset += blocksize;
156 } else {
157 char_ptr = &buf[amount_saved];
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000158 if (nand_write(&nand_info[0], offset, &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400159 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000160
Stuart Wood37e11442008-05-30 16:05:28 -0400161 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200162 amount_saved += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400163 }
164 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200165 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400166 return 1;
167
168 return 0;
169}
Scott Wood00eed7f2011-02-08 15:25:02 -0600170
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200171#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Wood00eed7f2011-02-08 15:25:02 -0600172static unsigned char env_flags;
173
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100174int saveenv(void)
175{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200176 env_t env_new;
177 ssize_t len;
178 char *res;
179 int ret = 0;
Stuart Wood37e11442008-05-30 16:05:28 -0400180 nand_erase_options_t nand_erase_options;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100181
Daniel Hobi201e7a22011-05-18 15:21:08 +0200182 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200183 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400184
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200185 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400186 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200187
188 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600189 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200190 if (len < 0) {
191 error("Cannot export environment: errno = %d\n", errno);
192 return 1;
193 }
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000194 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
195 env_new.flags = ++env_flags; /* increase the serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200196
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000197 if (gd->env_valid == 1) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200198 puts("Erasing redundant NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200199 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Wood37e11442008-05-30 16:05:28 -0400200 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100201 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400202
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200203 puts("Writing to redundant NAND... ");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000204 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100205 } else {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200206 puts("Erasing NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200207 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400208 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100209 return 1;
210
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200211 puts("Writing to NAND... ");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000212 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100213 }
Stuart Wood37e11442008-05-30 16:05:28 -0400214 if (ret) {
215 puts("FAILED!\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100216 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400217 }
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100218
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200219 puts("done\n");
220
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000221 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200222
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100223 return ret;
224}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200225#else /* ! CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000226int saveenv(void)
227{
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000228 int ret = 0;
Stephen Warrenf6c92cd2012-09-07 09:15:33 +0000229 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200230 ssize_t len;
231 char *res;
Philip Balister8c57e9a2008-06-16 08:58:07 -0400232 nand_erase_options_t nand_erase_options;
Wolfgang Denk41df50a2008-06-28 23:34:37 +0200233
Daniel Hobi201e7a22011-05-18 15:21:08 +0200234 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200235 nand_erase_options.length = CONFIG_ENV_RANGE;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200236 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400237
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200238 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400239 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200240
Stephen Warren1150ff82012-07-30 07:38:38 +0000241 res = (char *)&env_new->data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600242 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200243 if (len < 0) {
244 error("Cannot export environment: errno = %d\n", errno);
245 return 1;
246 }
Stephen Warren1150ff82012-07-30 07:38:38 +0000247 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200248
249 puts("Erasing Nand...\n");
Stuart Wood37e11442008-05-30 16:05:28 -0400250 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100251 return 1;
wdenk79b59372004-06-09 14:58:14 +0000252
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200253 puts("Writing to Nand... ");
Stephen Warren1150ff82012-07-30 07:38:38 +0000254 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)env_new)) {
Stuart Wood37e11442008-05-30 16:05:28 -0400255 puts("FAILED!\n");
wdenk79b59372004-06-09 14:58:14 +0000256 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400257 }
wdenk79b59372004-06-09 14:58:14 +0000258
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200259 puts("done\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100260 return ret;
wdenk79b59372004-06-09 14:58:14 +0000261}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200262#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000263#endif /* CMD_SAVEENV */
264
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000265int readenv(size_t offset, u_char *buf)
Stuart Wood37e11442008-05-30 16:05:28 -0400266{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200267 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400268 size_t amount_loaded = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200269 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400270 u_char *char_ptr;
271
272 blocksize = nand_info[0].erasesize;
Mike Frysinger3dc4ccd2010-08-11 23:42:26 -0400273 if (!blocksize)
274 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000275
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200276 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400277
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200278 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-05-30 16:05:28 -0400279 if (nand_block_isbad(&nand_info[0], offset)) {
280 offset += blocksize;
281 } else {
282 char_ptr = &buf[amount_loaded];
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000283 if (nand_read_skip_bad(&nand_info[0], offset,
284 &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400285 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000286
Stuart Wood37e11442008-05-30 16:05:28 -0400287 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200288 amount_loaded += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400289 }
290 }
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000291
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200292 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400293 return 1;
294
295 return 0;
296}
297
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400298#ifdef CONFIG_ENV_OFFSET_OOB
299int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
300{
301 struct mtd_oob_ops ops;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000302 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400303 int ret;
304
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000305 ops.datbuf = NULL;
306 ops.mode = MTD_OOB_AUTO;
307 ops.ooboffs = 0;
308 ops.ooblen = ENV_OFFSET_SIZE;
309 ops.oobbuf = (void *)oob_buf;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400310
311 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500312 if (ret) {
313 printf("error reading OOB block 0\n");
314 return ret;
315 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400316
Scott Wood0268da02010-07-12 18:17:40 -0500317 if (oob_buf[0] == ENV_OOB_MARKER) {
318 *result = oob_buf[1] * nand->erasesize;
319 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
320 *result = oob_buf[1];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400321 } else {
Scott Wood0268da02010-07-12 18:17:40 -0500322 printf("No dynamic environment marker in OOB block 0\n");
323 return -ENOENT;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400324 }
Scott Wood0268da02010-07-12 18:17:40 -0500325
326 return 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400327}
328#endif
329
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200330#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200331void env_relocate_spec(void)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100332{
333#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecherffe7ef52006-03-24 15:43:16 +0100334 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200335 env_t *ep, *tmp_env1, *tmp_env2;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100336
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200337 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
338 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000339 if (tmp_env1 == NULL || tmp_env2 == NULL) {
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700340 puts("Can't allocate buffers for environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200341 set_default_env("!malloc() failed");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000342 goto done;
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700343 }
344
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200345 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200346 puts("No Valid Environment Area found\n");
347
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200348 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200349 puts("No Valid Redundant Environment Area found\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100350
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000351 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
352 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100353
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200354 if (!crc1_ok && !crc2_ok) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200355 set_default_env("!bad CRC");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000356 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200357 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100358 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200359 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100360 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200361 } else {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100362 /* both ok - check serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200363 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100364 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200365 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100366 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200367 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100368 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200369 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100370 gd->env_valid = 2;
371 else /* flags are equal - almost impossible */
372 gd->env_valid = 1;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100373 }
374
375 free(env_ptr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200376
377 if (gd->env_valid == 1)
378 ep = tmp_env1;
379 else
380 ep = tmp_env2;
381
Scott Wood00eed7f2011-02-08 15:25:02 -0600382 env_flags = ep->flags;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200383 env_import((char *)ep, 0);
384
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000385done:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200386 free(tmp_env1);
387 free(tmp_env2);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100388
389#endif /* ! ENV_IS_EMBEDDED */
390}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200391#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100392/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200393 * The legacy NAND code saved the environment in the first NAND
394 * device i.e., nand_dev_desc + 0. This is also the behaviour using
395 * the new NAND code.
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100396 */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000397void env_relocate_spec(void)
wdenk79b59372004-06-09 14:58:14 +0000398{
399#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100400 int ret;
Stephen Warren1150ff82012-07-30 07:38:38 +0000401 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
wdenk79b59372004-06-09 14:58:14 +0000402
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400403#if defined(CONFIG_ENV_OFFSET_OOB)
404 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200405 /*
406 * If unable to read environment offset from NAND OOB then fall through
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400407 * to the normal environment reading code below
408 */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200409 if (!ret) {
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400410 printf("Found Environment offset in OOB..\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200411 } else {
412 set_default_env("!no env offset in OOB");
413 return;
414 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400415#endif
416
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200417 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
418 if (ret) {
419 set_default_env("!readenv() failed");
420 return;
421 }
wdenk79b59372004-06-09 14:58:14 +0000422
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200423 env_import(buf, 1);
wdenk79b59372004-06-09 14:58:14 +0000424#endif /* ! ENV_IS_EMBEDDED */
wdenk79b59372004-06-09 14:58:14 +0000425}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200426#endif /* CONFIG_ENV_OFFSET_REDUND */