blob: 79e803370505dd8e996621d56757e44690817662 [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
wdenk13a56952004-06-09 14:58:14 +000033#include <common.h>
wdenk13a56952004-06-09 14:58:14 +000034#include <command.h>
35#include <environment.h>
36#include <linux/stddef.h>
Markus Klotzbuechere443c942006-03-20 18:02:44 +010037#include <malloc.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010038#include <nand.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020039#include <search.h>
40#include <errno.h>
wdenk13a56952004-06-09 14:58:14 +000041
Mike Frysingerbdab39d2009-01-28 19:08:14 -050042#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
wdenk13a56952004-06-09 14:58:14 +000043#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020044#elif defined(CONFIG_ENV_OFFSET_REDUND)
Igor Grinbergde152b92011-11-07 01:14:10 +000045#error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000046#endif
47
Igor Grinbergde152b92011-11-07 01:14:10 +000048#if defined(CONFIG_ENV_SIZE_REDUND) && \
49 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020050#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000051#endif
52
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020053#ifndef CONFIG_ENV_RANGE
54#define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
Stuart Woodcc49cad2008-05-30 16:05:28 -040055#endif
56
Wolfgang Denkea882ba2010-06-20 23:33:59 +020057char *env_name_spec = "NAND";
wdenk13a56952004-06-09 14:58:14 +000058
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020059#if defined(ENV_IS_EMBEDDED)
Igor Grinberg994bc672011-11-17 06:07:23 +000060env_t *env_ptr = &environment;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020061#elif defined(CONFIG_NAND_ENV_DST)
62env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
wdenk13a56952004-06-09 14:58:14 +000063#else /* ! ENV_IS_EMBEDDED */
Igor Grinbergde152b92011-11-07 01:14:10 +000064env_t *env_ptr;
wdenk13a56952004-06-09 14:58:14 +000065#endif /* ENV_IS_EMBEDDED */
66
Wolfgang Denkd87080b2006-03-31 18:32:53 +020067DECLARE_GLOBAL_DATA_PTR;
wdenk13a56952004-06-09 14:58:14 +000068
Wolfgang Denkea882ba2010-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 Roesed12ae802006-09-12 20:19:10 +020075 *
76 * When using a NAND boot image (like sequoia_nand), the environment
Wolfgang Denkea882ba2010-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.
wdenk13a56952004-06-09 14:58:14 +000080 */
81int env_init(void)
82{
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020083#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesed12ae802006-09-12 20:19:10 +020084 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020085 env_t *tmp_env1;
86
87#ifdef CONFIG_ENV_OFFSET_REDUND
88 env_t *tmp_env2;
89
90 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
Igor Grinbergde152b92011-11-07 01:14:10 +000091 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020092#endif
Stefan Roesed12ae802006-09-12 20:19:10 +020093 tmp_env1 = env_ptr;
Igor Grinbergde152b92011-11-07 01:14:10 +000094 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
Stefan Roesed12ae802006-09-12 20:19:10 +020095
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020096 if (!crc1_ok && !crc2_ok) {
Igor Grinbergde152b92011-11-07 01:14:10 +000097 gd->env_addr = 0;
98 gd->env_valid = 0;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +020099
100 return 0;
101 } else if (crc1_ok && !crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200102 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200103 }
104#ifdef CONFIG_ENV_OFFSET_REDUND
105 else if (!crc1_ok && crc2_ok) {
Stefan Roesed12ae802006-09-12 20:19:10 +0200106 gd->env_valid = 2;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200107 } else {
Stefan Roesed12ae802006-09-12 20:19:10 +0200108 /* both ok - check serial */
Igor Grinbergde152b92011-11-07 01:14:10 +0000109 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Stefan Roesed12ae802006-09-12 20:19:10 +0200110 gd->env_valid = 2;
Igor Grinbergde152b92011-11-07 01:14:10 +0000111 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Stefan Roesed12ae802006-09-12 20:19:10 +0200112 gd->env_valid = 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000113 else if (tmp_env1->flags > tmp_env2->flags)
Stefan Roesed12ae802006-09-12 20:19:10 +0200114 gd->env_valid = 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000115 else if (tmp_env2->flags > tmp_env1->flags)
Stefan Roesed12ae802006-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 Liakhovetskib74ab732009-05-18 16:07:22 +0200121 if (gd->env_valid == 2)
122 env_ptr = tmp_env2;
123 else
124#endif
Stefan Roesed12ae802006-09-12 20:19:10 +0200125 if (gd->env_valid == 1)
126 env_ptr = tmp_env1;
Guennadi Liakhovetskib74ab732009-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 Grinbergde152b92011-11-07 01:14:10 +0000131 gd->env_addr = (ulong)&default_environment[0];
132 gd->env_valid = 1;
Guennadi Liakhovetskib74ab732009-05-18 16:07:22 +0200133#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk13a56952004-06-09 14:58:14 +0000134
Igor Grinbergde152b92011-11-07 01:14:10 +0000135 return 0;
wdenk13a56952004-06-09 14:58:14 +0000136}
137
138#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-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 Woodcc49cad2008-05-30 16:05:28 -0400143int writeenv(size_t offset, u_char *buf)
144{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200145 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400146 size_t amount_saved = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200147 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400148 u_char *char_ptr;
149
150 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200151 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400152
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200153 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-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 Grinbergde152b92011-11-07 01:14:10 +0000158 if (nand_write(&nand_info[0], offset, &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400159 return 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000160
Stuart Woodcc49cad2008-05-30 16:05:28 -0400161 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200162 amount_saved += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400163 }
164 }
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200165 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400166 return 1;
167
168 return 0;
169}
Scott Woodeef1d712011-02-08 15:25:02 -0600170
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200171#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Woodeef1d712011-02-08 15:25:02 -0600172static unsigned char env_flags;
173
wdenk13a56952004-06-09 14:58:14 +0000174int saveenv(void)
175{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200176 env_t env_new;
177 ssize_t len;
178 char *res;
179 int ret = 0;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400180 nand_erase_options_t nand_erase_options;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100181
Daniel Hobi3b250ff2011-05-18 15:21:08 +0200182 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200183 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400184
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200185 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400186 return 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200187
188 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100189 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200190 if (len < 0) {
191 error("Cannot export environment: errno = %d\n", errno);
192 return 1;
193 }
Igor Grinbergde152b92011-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 Denkea882ba2010-06-20 23:33:59 +0200196
Igor Grinbergde152b92011-11-07 01:14:10 +0000197 if (gd->env_valid == 1) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200198 puts("Erasing redundant NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200199 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400200 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100201 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400202
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200203 puts("Writing to redundant NAND... ");
Igor Grinbergde152b92011-11-07 01:14:10 +0000204 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100205 } else {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200206 puts("Erasing NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200207 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400208 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100209 return 1;
210
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200211 puts("Writing to NAND... ");
Igor Grinbergde152b92011-11-07 01:14:10 +0000212 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100213 }
Stuart Woodcc49cad2008-05-30 16:05:28 -0400214 if (ret) {
215 puts("FAILED!\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100216 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400217 }
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100218
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200219 puts("done\n");
220
Igor Grinbergde152b92011-11-07 01:14:10 +0000221 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200222
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100223 return ret;
224}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200225#else /* ! CONFIG_ENV_OFFSET_REDUND */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100226int saveenv(void)
227{
Igor Grinbergde152b92011-11-07 01:14:10 +0000228 int ret = 0;
Stephen Warren78b2de82012-09-07 09:15:33 +0000229 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200230 ssize_t len;
231 char *res;
Philip Balister9e4006bc2008-06-16 08:58:07 -0400232 nand_erase_options_t nand_erase_options;
Wolfgang Denke093a242008-06-28 23:34:37 +0200233
Daniel Hobi3b250ff2011-05-18 15:21:08 +0200234 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200235 nand_erase_options.length = CONFIG_ENV_RANGE;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200236 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400237
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200238 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400239 return 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200240
Stephen Warren3801a152012-07-30 07:38:38 +0000241 res = (char *)&env_new->data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100242 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200243 if (len < 0) {
244 error("Cannot export environment: errno = %d\n", errno);
245 return 1;
246 }
Stephen Warren3801a152012-07-30 07:38:38 +0000247 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200248
249 puts("Erasing Nand...\n");
Stuart Woodcc49cad2008-05-30 16:05:28 -0400250 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
wdenk13a56952004-06-09 14:58:14 +0000251 return 1;
252
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200253 puts("Writing to Nand... ");
Stephen Warren3801a152012-07-30 07:38:38 +0000254 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)env_new)) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400255 puts("FAILED!\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100256 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400257 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100258
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200259 puts("done\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100260 return ret;
wdenk13a56952004-06-09 14:58:14 +0000261}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200262#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000263#endif /* CMD_SAVEENV */
264
Igor Grinbergde152b92011-11-07 01:14:10 +0000265int readenv(size_t offset, u_char *buf)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400266{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200267 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400268 size_t amount_loaded = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200269 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400270 u_char *char_ptr;
271
272 blocksize = nand_info[0].erasesize;
Mike Frysinger962ad592010-08-11 23:42:26 -0400273 if (!blocksize)
274 return 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000275
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200276 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400277
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200278 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Woodcc49cad2008-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 Grinbergde152b92011-11-07 01:14:10 +0000283 if (nand_read_skip_bad(&nand_info[0], offset,
284 &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400285 return 1;
Igor Grinbergde152b92011-11-07 01:14:10 +0000286
Stuart Woodcc49cad2008-05-30 16:05:28 -0400287 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200288 amount_loaded += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400289 }
290 }
Igor Grinbergde152b92011-11-07 01:14:10 +0000291
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200292 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Woodcc49cad2008-05-30 16:05:28 -0400293 return 1;
294
295 return 0;
296}
297
Ben Gardinerc9f73512010-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 Grinbergde152b92011-11-07 01:14:10 +0000302 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
Ben Gardinerc9f73512010-07-05 13:27:07 -0400303 int ret;
304
Igor Grinbergde152b92011-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 Gardinerc9f73512010-07-05 13:27:07 -0400310
311 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood53504a22010-07-12 18:17:40 -0500312 if (ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400313 printf("error reading OOB block 0\n");
Scott Wood53504a22010-07-12 18:17:40 -0500314 return ret;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400315 }
Scott Wood53504a22010-07-12 18:17:40 -0500316
317 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];
321 } else {
322 printf("No dynamic environment marker in OOB block 0\n");
323 return -ENOENT;
324 }
325
326 return 0;
Ben Gardinerc9f73512010-07-05 13:27:07 -0400327}
328#endif
329
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200330#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200331void env_relocate_spec(void)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100332{
333#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100334 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200335 env_t *ep, *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000336
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200337 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
338 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Igor Grinbergde152b92011-11-07 01:14:10 +0000339 if (tmp_env1 == NULL || tmp_env2 == NULL) {
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700340 puts("Can't allocate buffers for environment\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200341 set_default_env("!malloc() failed");
Igor Grinbergde152b92011-11-07 01:14:10 +0000342 goto done;
Wolfgang Denk15b86c32010-01-16 21:50:26 -0700343 }
344
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200345 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200346 puts("No Valid Environment Area found\n");
347
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200348 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200349 puts("No Valid Redundant Environment Area found\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100350
Igor Grinbergde152b92011-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 Klotzbuechere443c942006-03-20 18:02:44 +0100353
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200354 if (!crc1_ok && !crc2_ok) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200355 set_default_env("!bad CRC");
Igor Grinbergde152b92011-11-07 01:14:10 +0000356 goto done;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200357 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100358 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200359 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100360 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200361 } else {
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100362 /* both ok - check serial */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200363 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100364 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200365 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100366 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200367 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100368 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200369 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100370 gd->env_valid = 2;
371 else /* flags are equal - almost impossible */
372 gd->env_valid = 1;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100373 }
374
375 free(env_ptr);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200376
377 if (gd->env_valid == 1)
378 ep = tmp_env1;
379 else
380 ep = tmp_env2;
381
Scott Woodeef1d712011-02-08 15:25:02 -0600382 env_flags = ep->flags;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200383 env_import((char *)ep, 0);
384
Igor Grinbergde152b92011-11-07 01:14:10 +0000385done:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200386 free(tmp_env1);
387 free(tmp_env2);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100388
389#endif /* ! ENV_IS_EMBEDDED */
390}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200391#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100392/*
Wolfgang Denkea882ba2010-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 Siekaaddb2e12006-03-05 18:57:33 +0100396 */
Igor Grinbergde152b92011-11-07 01:14:10 +0000397void env_relocate_spec(void)
wdenk13a56952004-06-09 14:58:14 +0000398{
399#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100400 int ret;
Stephen Warren3801a152012-07-30 07:38:38 +0000401 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
wdenk13a56952004-06-09 14:58:14 +0000402
Ben Gardinerc9f73512010-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 Denkea882ba2010-06-20 23:33:59 +0200405 /*
406 * If unable to read environment offset from NAND OOB then fall through
Ben Gardinerc9f73512010-07-05 13:27:07 -0400407 * to the normal environment reading code below
408 */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200409 if (!ret) {
Ben Gardinerc9f73512010-07-05 13:27:07 -0400410 printf("Found Environment offset in OOB..\n");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200411 } else {
412 set_default_env("!no env offset in OOB");
413 return;
414 }
Ben Gardinerc9f73512010-07-05 13:27:07 -0400415#endif
416
Wolfgang Denkea882ba2010-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 }
wdenk13a56952004-06-09 14:58:14 +0000422
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200423 env_import(buf, 1);
wdenk13a56952004-06-09 14:58:14 +0000424#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000425}
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200426#endif /* CONFIG_ENV_OFFSET_REDUND */