blob: a8f0de7ae24b29a835771e3fb25fb8c7141a2ebc [file] [log] [blame]
wdenk13a56952004-06-09 14:58:14 +00001/*
Stuart Woodcc49cad2008-05-30 16:05:28 -04002 * (C) Copyright 2008
3 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
4 *
wdenk13a56952004-06-09 14:58:14 +00005 * (C) Copyright 2004
6 * Jian Zhang, Texas Instruments, jzhang@ti.com.
7
Stefan Roesed12ae802006-09-12 20:19:10 +02008 * (C) Copyright 2000-2006
wdenk13a56952004-06-09 14:58:14 +00009 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 *
11 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Andreas Heppel <aheppel@sysgo.de>
13
14 * 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
33/* #define DEBUG */
34
35#include <common.h>
36
37#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
38
39#include <command.h>
40#include <environment.h>
41#include <linux/stddef.h>
Markus Klotzbuechere443c942006-03-20 18:02:44 +010042#include <malloc.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010043#include <nand.h>
wdenk13a56952004-06-09 14:58:14 +000044
Jon Loeligerc3517f92007-07-08 18:10:08 -050045#if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)
wdenk13a56952004-06-09 14:58:14 +000046#define CMD_SAVEENV
Markus Klotzbuechere443c942006-03-20 18:02:44 +010047#elif defined(CFG_ENV_OFFSET_REDUND)
Jon Loeliger90253172007-07-10 11:02:44 -050048#error Cannot use CFG_ENV_OFFSET_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000049#endif
50
Markus Klotzbuechere443c942006-03-20 18:02:44 +010051#if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE)
52#error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000053#endif
54
wdenk13a56952004-06-09 14:58:14 +000055#ifdef CONFIG_INFERNO
56#error CONFIG_INFERNO not supported yet
57#endif
58
Stuart Woodcc49cad2008-05-30 16:05:28 -040059#ifndef CFG_ENV_RANGE
60#define CFG_ENV_RANGE CFG_ENV_SIZE
61#endif
62
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010063int nand_legacy_rw (struct nand_chip* nand, int cmd,
wdenk13a56952004-06-09 14:58:14 +000064 size_t start, size_t len,
65 size_t * retlen, u_char * buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010066
wdenk13a56952004-06-09 14:58:14 +000067/* references to names in env_common.c */
68extern uchar default_environment[];
69extern int default_environment_size;
70
71char * env_name_spec = "NAND";
72
73
74#ifdef ENV_IS_EMBEDDED
75extern uchar environment[];
76env_t *env_ptr = (env_t *)(&environment[0]);
77#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000078env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000079#endif /* ENV_IS_EMBEDDED */
80
81
82/* local functions */
Stefan Roesed12ae802006-09-12 20:19:10 +020083#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +000084static void use_default(void);
Stefan Roesed12ae802006-09-12 20:19:10 +020085#endif
wdenk13a56952004-06-09 14:58:14 +000086
Wolfgang Denkd87080b2006-03-31 18:32:53 +020087DECLARE_GLOBAL_DATA_PTR;
wdenk13a56952004-06-09 14:58:14 +000088
89uchar env_get_char_spec (int index)
90{
wdenk13a56952004-06-09 14:58:14 +000091 return ( *((uchar *)(gd->env_addr + index)) );
92}
93
94
95/* this is called before nand_init()
96 * so we can't read Nand to validate env data.
97 * Mark it OK for now. env_relocate() in env_common.c
Marcel Ziswiler99c2b432008-06-22 16:13:46 +020098 * will call our relocate function which does the real
99 * validation.
Stefan Roesed12ae802006-09-12 20:19:10 +0200100 *
101 * When using a NAND boot image (like sequoia_nand), the environment
102 * can be embedded or attached to the U-Boot image in NAND flash. This way
103 * the SPL loads not only the U-Boot image from NAND but also the
104 * environment.
wdenk13a56952004-06-09 14:58:14 +0000105 */
106int env_init(void)
107{
Stefan Roesed12ae802006-09-12 20:19:10 +0200108#if defined(ENV_IS_EMBEDDED)
dirk.behme@googlemail.com378e7ec2008-04-30 18:02:59 +0200109 size_t total;
Stefan Roesed12ae802006-09-12 20:19:10 +0200110 int crc1_ok = 0, crc2_ok = 0;
111 env_t *tmp_env1, *tmp_env2;
112
113 total = CFG_ENV_SIZE;
114
115 tmp_env1 = env_ptr;
116 tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
117
118 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
119 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
120
121 if (!crc1_ok && !crc2_ok)
122 gd->env_valid = 0;
123 else if(crc1_ok && !crc2_ok)
124 gd->env_valid = 1;
125 else if(!crc1_ok && crc2_ok)
126 gd->env_valid = 2;
127 else {
128 /* both ok - check serial */
129 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
130 gd->env_valid = 2;
131 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
132 gd->env_valid = 1;
133 else if(tmp_env1->flags > tmp_env2->flags)
134 gd->env_valid = 1;
135 else if(tmp_env2->flags > tmp_env1->flags)
136 gd->env_valid = 2;
137 else /* flags are equal - almost impossible */
138 gd->env_valid = 1;
139 }
140
141 if (gd->env_valid == 1)
142 env_ptr = tmp_env1;
143 else if (gd->env_valid == 2)
144 env_ptr = tmp_env2;
145#else /* ENV_IS_EMBEDDED */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100146 gd->env_addr = (ulong)&default_environment[0];
wdenk13a56952004-06-09 14:58:14 +0000147 gd->env_valid = 1;
Stefan Roesed12ae802006-09-12 20:19:10 +0200148#endif /* ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000149
150 return (0);
151}
152
153#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100154/*
155 * The legacy NAND code saved the environment in the first NAND device i.e.,
156 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
157 */
Stuart Woodcc49cad2008-05-30 16:05:28 -0400158int writeenv(size_t offset, u_char *buf)
159{
160 size_t end = offset + CFG_ENV_RANGE;
161 size_t amount_saved = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200162 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400163
164 u_char *char_ptr;
165
166 blocksize = nand_info[0].erasesize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200167 len = min(blocksize, CFG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400168
169 while (amount_saved < CFG_ENV_SIZE && offset < end) {
170 if (nand_block_isbad(&nand_info[0], offset)) {
171 offset += blocksize;
172 } else {
173 char_ptr = &buf[amount_saved];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200174 if (nand_write(&nand_info[0], offset, &len,
Stuart Woodcc49cad2008-05-30 16:05:28 -0400175 char_ptr))
176 return 1;
177 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200178 amount_saved += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400179 }
180 }
181 if (amount_saved != CFG_ENV_SIZE)
182 return 1;
183
184 return 0;
185}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100186#ifdef CFG_ENV_OFFSET_REDUND
wdenk13a56952004-06-09 14:58:14 +0000187int saveenv(void)
188{
Wolfgang Denk4ca79f42008-04-28 12:08:18 +0200189 size_t total;
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100190 int ret = 0;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400191 nand_erase_options_t nand_erase_options;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100192
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100193 env_ptr->flags++;
194 total = CFG_ENV_SIZE;
195
Stuart Woodcc49cad2008-05-30 16:05:28 -0400196 nand_erase_options.length = CFG_ENV_RANGE;
197 nand_erase_options.quiet = 0;
198 nand_erase_options.jffs2 = 0;
199 nand_erase_options.scrub = 0;
200
201 if (CFG_ENV_RANGE < CFG_ENV_SIZE)
202 return 1;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100203 if(gd->env_valid == 1) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400204 puts ("Erasing redundant Nand...\n");
205 nand_erase_options.offset = CFG_ENV_OFFSET_REDUND;
206 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100207 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400208
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100209 puts ("Writing to redundant Nand... ");
Stuart Woodcc49cad2008-05-30 16:05:28 -0400210 ret = writeenv(CFG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100211 } else {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400212 puts ("Erasing Nand...\n");
213 nand_erase_options.offset = CFG_ENV_OFFSET;
214 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100215 return 1;
216
217 puts ("Writing to Nand... ");
Stuart Woodcc49cad2008-05-30 16:05:28 -0400218 ret = writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100219 }
Stuart Woodcc49cad2008-05-30 16:05:28 -0400220 if (ret) {
221 puts("FAILED!\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100222 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400223 }
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100224
225 puts ("done\n");
226 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
227 return ret;
228}
229#else /* ! CFG_ENV_OFFSET_REDUND */
230int saveenv(void)
231{
dirk.behme@googlemail.com378e7ec2008-04-30 18:02:59 +0200232 size_t total;
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100233 int ret = 0;
Philip Balister9e4006bc2008-06-16 08:58:07 -0400234 nand_erase_options_t nand_erase_options;
Wolfgang Denke093a242008-06-28 23:34:37 +0200235
Stuart Woodcc49cad2008-05-30 16:05:28 -0400236 nand_erase_options.length = CFG_ENV_RANGE;
237 nand_erase_options.quiet = 0;
238 nand_erase_options.jffs2 = 0;
239 nand_erase_options.scrub = 0;
240 nand_erase_options.offset = CFG_ENV_OFFSET;
241
242 if (CFG_ENV_RANGE < CFG_ENV_SIZE)
243 return 1;
244 puts ("Erasing Nand...\n");
245 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
wdenk13a56952004-06-09 14:58:14 +0000246 return 1;
247
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100248 puts ("Writing to Nand... ");
249 total = CFG_ENV_SIZE;
Marcel Ziswiler99c2b432008-06-22 16:13:46 +0200250 if (writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr)) {
Stuart Woodcc49cad2008-05-30 16:05:28 -0400251 puts("FAILED!\n");
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100252 return 1;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400253 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100254
255 puts ("done\n");
256 return ret;
wdenk13a56952004-06-09 14:58:14 +0000257}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100258#endif /* CFG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000259#endif /* CMD_SAVEENV */
260
Stuart Woodcc49cad2008-05-30 16:05:28 -0400261int readenv (size_t offset, u_char * buf)
262{
263 size_t end = offset + CFG_ENV_RANGE;
264 size_t amount_loaded = 0;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200265 size_t blocksize, len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400266
267 u_char *char_ptr;
268
269 blocksize = nand_info[0].erasesize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200270 len = min(blocksize, CFG_ENV_SIZE);
Stuart Woodcc49cad2008-05-30 16:05:28 -0400271
272 while (amount_loaded < CFG_ENV_SIZE && offset < end) {
273 if (nand_block_isbad(&nand_info[0], offset)) {
274 offset += blocksize;
275 } else {
276 char_ptr = &buf[amount_loaded];
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200277 if (nand_read(&nand_info[0], offset, &len, char_ptr))
Stuart Woodcc49cad2008-05-30 16:05:28 -0400278 return 1;
279 offset += blocksize;
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200280 amount_loaded += len;
Stuart Woodcc49cad2008-05-30 16:05:28 -0400281 }
282 }
283 if (amount_loaded != CFG_ENV_SIZE)
284 return 1;
285
286 return 0;
287}
288
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100289#ifdef CFG_ENV_OFFSET_REDUND
290void env_relocate_spec (void)
291{
292#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkf7b16a02008-04-29 23:32:20 +0200293 size_t total;
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100294 int crc1_ok = 0, crc2_ok = 0;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100295 env_t *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000296
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100297 total = CFG_ENV_SIZE;
298
299 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
300 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
301
Stuart Woodcc49cad2008-05-30 16:05:28 -0400302 if (readenv(CFG_ENV_OFFSET, (u_char *) tmp_env1))
303 puts("No Valid Environment Area Found\n");
304 if (readenv(CFG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
305 puts("No Valid Reundant Environment Area Found\n");
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100306
307 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
308 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
309
310 if(!crc1_ok && !crc2_ok)
311 return use_default();
312 else if(crc1_ok && !crc2_ok)
313 gd->env_valid = 1;
314 else if(!crc1_ok && crc2_ok)
315 gd->env_valid = 2;
316 else {
317 /* both ok - check serial */
318 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
319 gd->env_valid = 2;
320 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
321 gd->env_valid = 1;
322 else if(tmp_env1->flags > tmp_env2->flags)
323 gd->env_valid = 1;
324 else if(tmp_env2->flags > tmp_env1->flags)
325 gd->env_valid = 2;
326 else /* flags are equal - almost impossible */
327 gd->env_valid = 1;
328
329 }
330
331 free(env_ptr);
332 if(gd->env_valid == 1) {
333 env_ptr = tmp_env1;
334 free(tmp_env2);
335 } else {
336 env_ptr = tmp_env2;
337 free(tmp_env1);
338 }
339
340#endif /* ! ENV_IS_EMBEDDED */
341}
342#else /* ! CFG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100343/*
344 * The legacy NAND code saved the environment in the first NAND device i.e.,
345 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
346 */
wdenk13a56952004-06-09 14:58:14 +0000347void env_relocate_spec (void)
348{
349#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100350 int ret;
wdenk13a56952004-06-09 14:58:14 +0000351
Marcel Ziswiler99c2b432008-06-22 16:13:46 +0200352 ret = readenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
Guennadi Liakhovetskic3db8c62008-07-31 12:38:26 +0200353 if (ret)
wdenk13a56952004-06-09 14:58:14 +0000354 return use_default();
355
356 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
357 return use_default();
358#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000359}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100360#endif /* CFG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000361
Stefan Roesed12ae802006-09-12 20:19:10 +0200362#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +0000363static void use_default()
364{
wdenk13a56952004-06-09 14:58:14 +0000365 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +0800366 set_default_env();
wdenk13a56952004-06-09 14:58:14 +0000367}
Stefan Roesed12ae802006-09-12 20:19:10 +0200368#endif
wdenk13a56952004-06-09 14:58:14 +0000369
370#endif /* CFG_ENV_IS_IN_NAND */