blob: 8cc2055eb366c6b5bf0d5d3f0dd6df589462f454 [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
Rob Herring04cae822013-03-22 11:26:21 +000067DEFINE_CACHE_ALIGN_BUFFER(char, env_buf, CONFIG_ENV_SIZE);
68
Wolfgang Denk6405a152006-03-31 18:32:53 +020069DECLARE_GLOBAL_DATA_PTR;
wdenk79b59372004-06-09 14:58:14 +000070
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071/*
72 * This is called before nand_init() so we can't read NAND to
73 * validate env data.
74 *
75 * Mark it OK for now. env_relocate() in env_common.c will call our
76 * relocate function which does the real validation.
Stefan Roesebbfcbb72006-09-12 20:19:10 +020077 *
78 * When using a NAND boot image (like sequoia_nand), the environment
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020079 * can be embedded or attached to the U-Boot image in NAND flash.
80 * This way the SPL loads not only the U-Boot image from NAND but
81 * also the environment.
wdenk79b59372004-06-09 14:58:14 +000082 */
83int env_init(void)
84{
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020085#if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
Stefan Roesebbfcbb72006-09-12 20:19:10 +020086 int crc1_ok = 0, crc2_ok = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020087 env_t *tmp_env1;
Stefan Roesebbfcbb72006-09-12 20:19:10 +020088
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020089#ifdef CONFIG_ENV_OFFSET_REDUND
90 env_t *tmp_env2;
91
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020092 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
Igor Grinberg90f2ca82011-11-07 01:14:10 +000093 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020094#endif
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020095 tmp_env1 = env_ptr;
Igor Grinberg90f2ca82011-11-07 01:14:10 +000096 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
Stefan Roesebbfcbb72006-09-12 20:19:10 +020097
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +020098 if (!crc1_ok && !crc2_ok) {
Igor Grinberg90f2ca82011-11-07 01:14:10 +000099 gd->env_addr = 0;
100 gd->env_valid = 0;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200101
102 return 0;
103 } else if (crc1_ok && !crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200104 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200105 }
106#ifdef CONFIG_ENV_OFFSET_REDUND
107 else if (!crc1_ok && crc2_ok) {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200108 gd->env_valid = 2;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200109 } else {
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200110 /* both ok - check serial */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000111 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200112 gd->env_valid = 2;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000113 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200114 gd->env_valid = 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000115 else if (tmp_env1->flags > tmp_env2->flags)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200116 gd->env_valid = 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000117 else if (tmp_env2->flags > tmp_env1->flags)
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200118 gd->env_valid = 2;
119 else /* flags are equal - almost impossible */
120 gd->env_valid = 1;
121 }
122
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200123 if (gd->env_valid == 2)
124 env_ptr = tmp_env2;
125 else
126#endif
Stefan Roesebbfcbb72006-09-12 20:19:10 +0200127 if (gd->env_valid == 1)
128 env_ptr = tmp_env1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200129
130 gd->env_addr = (ulong)env_ptr->data;
131
132#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000133 gd->env_addr = (ulong)&default_environment[0];
134 gd->env_valid = 1;
Guennadi Liakhovetskifad24442009-05-18 16:07:22 +0200135#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
wdenk79b59372004-06-09 14:58:14 +0000136
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000137 return 0;
wdenk79b59372004-06-09 14:58:14 +0000138}
139
140#ifdef CMD_SAVEENV
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100141/*
142 * The legacy NAND code saved the environment in the first NAND device i.e.,
143 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
144 */
Stuart Wood37e11442008-05-30 16:05:28 -0400145int writeenv(size_t offset, u_char *buf)
146{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200147 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400148 size_t amount_saved = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200149 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400150 u_char *char_ptr;
151
152 blocksize = nand_info[0].erasesize;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200153 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400154
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200155 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-05-30 16:05:28 -0400156 if (nand_block_isbad(&nand_info[0], offset)) {
157 offset += blocksize;
158 } else {
159 char_ptr = &buf[amount_saved];
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000160 if (nand_write(&nand_info[0], offset, &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400161 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000162
Stuart Wood37e11442008-05-30 16:05:28 -0400163 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200164 amount_saved += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400165 }
166 }
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200167 if (amount_saved != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400168 return 1;
169
170 return 0;
171}
Scott Wood00eed7f2011-02-08 15:25:02 -0600172
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200173#ifdef CONFIG_ENV_OFFSET_REDUND
Scott Wood00eed7f2011-02-08 15:25:02 -0600174static unsigned char env_flags;
175
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100176int saveenv(void)
177{
Rob Herring04cae822013-03-22 11:26:21 +0000178 env_t *env_new = (env_t *)env_buf;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200179 ssize_t len;
180 char *res;
181 int ret = 0;
Stuart Wood37e11442008-05-30 16:05:28 -0400182 nand_erase_options_t nand_erase_options;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100183
Daniel Hobi201e7a22011-05-18 15:21:08 +0200184 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200185 nand_erase_options.length = CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400186
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200187 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400188 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200189
Rob Herring04cae822013-03-22 11:26:21 +0000190 res = (char *)env_new->data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600191 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200192 if (len < 0) {
193 error("Cannot export environment: errno = %d\n", errno);
194 return 1;
195 }
Rob Herring04cae822013-03-22 11:26:21 +0000196 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
197 env_new->flags = ++env_flags; /* increase the serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200198
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000199 if (gd->env_valid == 1) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200200 puts("Erasing redundant NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200201 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
Stuart Wood37e11442008-05-30 16:05:28 -0400202 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100203 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400204
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200205 puts("Writing to redundant NAND... ");
Rob Herring04cae822013-03-22 11:26:21 +0000206 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100207 } else {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200208 puts("Erasing NAND...\n");
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200209 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400210 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100211 return 1;
212
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200213 puts("Writing to NAND... ");
Rob Herring04cae822013-03-22 11:26:21 +0000214 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)env_new);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100215 }
Stuart Wood37e11442008-05-30 16:05:28 -0400216 if (ret) {
217 puts("FAILED!\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100218 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400219 }
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100220
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200221 puts("done\n");
222
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000223 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200224
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100225 return ret;
226}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200227#else /* ! CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000228int saveenv(void)
229{
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000230 int ret = 0;
Rob Herring04cae822013-03-22 11:26:21 +0000231 env_t *env_new = (env_t *)env_buf;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200232 ssize_t len;
233 char *res;
Philip Balister8c57e9a2008-06-16 08:58:07 -0400234 nand_erase_options_t nand_erase_options;
Wolfgang Denk41df50a2008-06-28 23:34:37 +0200235
Daniel Hobi201e7a22011-05-18 15:21:08 +0200236 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200237 nand_erase_options.length = CONFIG_ENV_RANGE;
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200238 nand_erase_options.offset = CONFIG_ENV_OFFSET;
Stuart Wood37e11442008-05-30 16:05:28 -0400239
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200240 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400241 return 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242
Rob Herring04cae822013-03-22 11:26:21 +0000243 res = (char *)env_new->data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600244 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200245 if (len < 0) {
246 error("Cannot export environment: errno = %d\n", errno);
247 return 1;
248 }
Stephen Warren1150ff82012-07-30 07:38:38 +0000249 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200250
251 puts("Erasing Nand...\n");
Stuart Wood37e11442008-05-30 16:05:28 -0400252 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100253 return 1;
wdenk79b59372004-06-09 14:58:14 +0000254
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200255 puts("Writing to Nand... ");
Stephen Warren1150ff82012-07-30 07:38:38 +0000256 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)env_new)) {
Stuart Wood37e11442008-05-30 16:05:28 -0400257 puts("FAILED!\n");
wdenk79b59372004-06-09 14:58:14 +0000258 return 1;
Stuart Wood37e11442008-05-30 16:05:28 -0400259 }
wdenk79b59372004-06-09 14:58:14 +0000260
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200261 puts("done\n");
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100262 return ret;
wdenk79b59372004-06-09 14:58:14 +0000263}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200264#endif /* CONFIG_ENV_OFFSET_REDUND */
wdenk79b59372004-06-09 14:58:14 +0000265#endif /* CMD_SAVEENV */
266
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000267int readenv(size_t offset, u_char *buf)
Stuart Wood37e11442008-05-30 16:05:28 -0400268{
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200269 size_t end = offset + CONFIG_ENV_RANGE;
Stuart Wood37e11442008-05-30 16:05:28 -0400270 size_t amount_loaded = 0;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200271 size_t blocksize, len;
Stuart Wood37e11442008-05-30 16:05:28 -0400272 u_char *char_ptr;
273
274 blocksize = nand_info[0].erasesize;
Mike Frysinger3dc4ccd2010-08-11 23:42:26 -0400275 if (!blocksize)
276 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000277
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200278 len = min(blocksize, CONFIG_ENV_SIZE);
Stuart Wood37e11442008-05-30 16:05:28 -0400279
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200280 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
Stuart Wood37e11442008-05-30 16:05:28 -0400281 if (nand_block_isbad(&nand_info[0], offset)) {
282 offset += blocksize;
283 } else {
284 char_ptr = &buf[amount_loaded];
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000285 if (nand_read_skip_bad(&nand_info[0], offset,
286 &len, char_ptr))
Stuart Wood37e11442008-05-30 16:05:28 -0400287 return 1;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000288
Stuart Wood37e11442008-05-30 16:05:28 -0400289 offset += blocksize;
Guennadi Liakhovetski0c40ae942008-07-31 12:38:26 +0200290 amount_loaded += len;
Stuart Wood37e11442008-05-30 16:05:28 -0400291 }
292 }
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000293
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200294 if (amount_loaded != CONFIG_ENV_SIZE)
Stuart Wood37e11442008-05-30 16:05:28 -0400295 return 1;
296
297 return 0;
298}
299
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400300#ifdef CONFIG_ENV_OFFSET_OOB
301int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
302{
303 struct mtd_oob_ops ops;
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000304 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400305 int ret;
306
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000307 ops.datbuf = NULL;
308 ops.mode = MTD_OOB_AUTO;
309 ops.ooboffs = 0;
310 ops.ooblen = ENV_OFFSET_SIZE;
311 ops.oobbuf = (void *)oob_buf;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400312
313 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
Scott Wood0268da02010-07-12 18:17:40 -0500314 if (ret) {
315 printf("error reading OOB block 0\n");
316 return ret;
317 }
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400318
Scott Wood0268da02010-07-12 18:17:40 -0500319 if (oob_buf[0] == ENV_OOB_MARKER) {
320 *result = oob_buf[1] * nand->erasesize;
321 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
322 *result = oob_buf[1];
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400323 } else {
Scott Wood0268da02010-07-12 18:17:40 -0500324 printf("No dynamic environment marker in OOB block 0\n");
325 return -ENOENT;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400326 }
Scott Wood0268da02010-07-12 18:17:40 -0500327
328 return 0;
Ben Gardiner5fc8b7f2010-07-05 13:27:07 -0400329}
330#endif
331
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200332#ifdef CONFIG_ENV_OFFSET_REDUND
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200333void env_relocate_spec(void)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100334{
335#if !defined(ENV_IS_EMBEDDED)
Phil Sutter190e58a2013-02-21 18:21:55 +0100336 int read1_fail = 0, read2_fail = 0;
Markus Klotzbuecherffe7ef52006-03-24 15:43:16 +0100337 int crc1_ok = 0, crc2_ok = 0;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200338 env_t *ep, *tmp_env1, *tmp_env2;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100339
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200340 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
341 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000342 if (tmp_env1 == NULL || tmp_env2 == NULL) {
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700343 puts("Can't allocate buffers for environment\n");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200344 set_default_env("!malloc() failed");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000345 goto done;
Wolfgang Denkbe14eb12010-01-16 21:50:26 -0700346 }
347
Phil Sutter190e58a2013-02-21 18:21:55 +0100348 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
349 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200350
Phil Sutter190e58a2013-02-21 18:21:55 +0100351 if (read1_fail && read2_fail)
352 puts("*** Error - No Valid Environment Area found\n");
353 else if (read1_fail || read2_fail)
354 puts("*** Warning - some problems detected "
355 "reading environment; recovered successfully\n");
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100356
Phil Sutter3c51a852013-02-21 18:21:56 +0100357 crc1_ok = !read1_fail &&
358 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
359 crc2_ok = !read2_fail &&
360 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100361
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200362 if (!crc1_ok && !crc2_ok) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200363 set_default_env("!bad CRC");
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000364 goto done;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200365 } else if (crc1_ok && !crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100366 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200367 } else if (!crc1_ok && crc2_ok) {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100368 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200369 } else {
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100370 /* both ok - check serial */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200371 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100372 gd->env_valid = 2;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200373 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100374 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200375 else if (tmp_env1->flags > tmp_env2->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100376 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200377 else if (tmp_env2->flags > tmp_env1->flags)
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100378 gd->env_valid = 2;
379 else /* flags are equal - almost impossible */
380 gd->env_valid = 1;
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100381 }
382
383 free(env_ptr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200384
385 if (gd->env_valid == 1)
386 ep = tmp_env1;
387 else
388 ep = tmp_env2;
389
Scott Wood00eed7f2011-02-08 15:25:02 -0600390 env_flags = ep->flags;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200391 env_import((char *)ep, 0);
392
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000393done:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200394 free(tmp_env1);
395 free(tmp_env2);
Markus Klotzbuecher5d113e02006-03-20 18:02:44 +0100396
397#endif /* ! ENV_IS_EMBEDDED */
398}
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200399#else /* ! CONFIG_ENV_OFFSET_REDUND */
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100400/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200401 * The legacy NAND code saved the environment in the first NAND
402 * device i.e., nand_dev_desc + 0. This is also the behaviour using
403 * the new NAND code.
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100404 */
Igor Grinberg90f2ca82011-11-07 01:14:10 +0000405void env_relocate_spec(void)
wdenk79b59372004-06-09 14:58:14 +0000406{
407#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100408 int ret;
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
Rob Herring04cae822013-03-22 11:26:21 +0000424 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)env_buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200425 if (ret) {
426 set_default_env("!readenv() failed");
427 return;
428 }
wdenk79b59372004-06-09 14:58:14 +0000429
Rob Herring04cae822013-03-22 11:26:21 +0000430 env_import(env_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 */