blob: 38a07f8993f7ab1226044ed2497f05788ee9bfc5 [file] [log] [blame]
wdenk13a56952004-06-09 14:58:14 +00001/*
2 * (C) Copyright 2004
3 * Jian Zhang, Texas Instruments, jzhang@ti.com.
4
Stefan Roesed12ae802006-09-12 20:19:10 +02005 * (C) Copyright 2000-2006
wdenk13a56952004-06-09 14:58:14 +00006 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Andreas Heppel <aheppel@sysgo.de>
10
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 */
29
30/* #define DEBUG */
31
32#include <common.h>
33
34#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
35
36#include <command.h>
37#include <environment.h>
38#include <linux/stddef.h>
Markus Klotzbuechere443c942006-03-20 18:02:44 +010039#include <malloc.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010040#include <nand.h>
wdenk13a56952004-06-09 14:58:14 +000041
Jon Loeligerc3517f92007-07-08 18:10:08 -050042#if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)
wdenk13a56952004-06-09 14:58:14 +000043#define CMD_SAVEENV
Markus Klotzbuechere443c942006-03-20 18:02:44 +010044#elif defined(CFG_ENV_OFFSET_REDUND)
Jon Loeliger90253172007-07-10 11:02:44 -050045#error Cannot use CFG_ENV_OFFSET_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000046#endif
47
Markus Klotzbuechere443c942006-03-20 18:02:44 +010048#if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE)
49#error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE
wdenk13a56952004-06-09 14:58:14 +000050#endif
51
wdenk13a56952004-06-09 14:58:14 +000052#ifdef CONFIG_INFERNO
53#error CONFIG_INFERNO not supported yet
54#endif
55
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010056int nand_legacy_rw (struct nand_chip* nand, int cmd,
wdenk13a56952004-06-09 14:58:14 +000057 size_t start, size_t len,
58 size_t * retlen, u_char * buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010059
60/* info for NAND chips, defined in drivers/nand/nand.c */
61extern nand_info_t nand_info[];
wdenk13a56952004-06-09 14:58:14 +000062
63/* references to names in env_common.c */
64extern uchar default_environment[];
65extern int default_environment_size;
66
67char * env_name_spec = "NAND";
68
69
70#ifdef ENV_IS_EMBEDDED
71extern uchar environment[];
72env_t *env_ptr = (env_t *)(&environment[0]);
73#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000074env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000075#endif /* ENV_IS_EMBEDDED */
76
77
78/* local functions */
Stefan Roesed12ae802006-09-12 20:19:10 +020079#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +000080static void use_default(void);
Stefan Roesed12ae802006-09-12 20:19:10 +020081#endif
wdenk13a56952004-06-09 14:58:14 +000082
Wolfgang Denkd87080b2006-03-31 18:32:53 +020083DECLARE_GLOBAL_DATA_PTR;
wdenk13a56952004-06-09 14:58:14 +000084
85uchar env_get_char_spec (int index)
86{
wdenk13a56952004-06-09 14:58:14 +000087 return ( *((uchar *)(gd->env_addr + index)) );
88}
89
90
91/* this is called before nand_init()
92 * so we can't read Nand to validate env data.
93 * Mark it OK for now. env_relocate() in env_common.c
94 * will call our relocate function which will does
95 * the real validation.
Stefan Roesed12ae802006-09-12 20:19:10 +020096 *
97 * When using a NAND boot image (like sequoia_nand), the environment
98 * can be embedded or attached to the U-Boot image in NAND flash. This way
99 * the SPL loads not only the U-Boot image from NAND but also the
100 * environment.
wdenk13a56952004-06-09 14:58:14 +0000101 */
102int env_init(void)
103{
Stefan Roesed12ae802006-09-12 20:19:10 +0200104#if defined(ENV_IS_EMBEDDED)
105 ulong total;
106 int crc1_ok = 0, crc2_ok = 0;
107 env_t *tmp_env1, *tmp_env2;
108
109 total = CFG_ENV_SIZE;
110
111 tmp_env1 = env_ptr;
112 tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
113
114 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
115 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
116
117 if (!crc1_ok && !crc2_ok)
118 gd->env_valid = 0;
119 else if(crc1_ok && !crc2_ok)
120 gd->env_valid = 1;
121 else if(!crc1_ok && crc2_ok)
122 gd->env_valid = 2;
123 else {
124 /* both ok - check serial */
125 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
126 gd->env_valid = 2;
127 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
128 gd->env_valid = 1;
129 else if(tmp_env1->flags > tmp_env2->flags)
130 gd->env_valid = 1;
131 else if(tmp_env2->flags > tmp_env1->flags)
132 gd->env_valid = 2;
133 else /* flags are equal - almost impossible */
134 gd->env_valid = 1;
135 }
136
137 if (gd->env_valid == 1)
138 env_ptr = tmp_env1;
139 else if (gd->env_valid == 2)
140 env_ptr = tmp_env2;
141#else /* ENV_IS_EMBEDDED */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100142 gd->env_addr = (ulong)&default_environment[0];
wdenk13a56952004-06-09 14:58:14 +0000143 gd->env_valid = 1;
Stefan Roesed12ae802006-09-12 20:19:10 +0200144#endif /* ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000145
146 return (0);
147}
148
149#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100150/*
151 * The legacy NAND code saved the environment in the first NAND device i.e.,
152 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
153 */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100154#ifdef CFG_ENV_OFFSET_REDUND
wdenk13a56952004-06-09 14:58:14 +0000155int saveenv(void)
156{
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100157 ulong total;
158 int ret = 0;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100159
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100160 env_ptr->flags++;
161 total = CFG_ENV_SIZE;
162
163 if(gd->env_valid == 1) {
164 puts ("Erasing redundant Nand...");
165 if (nand_erase(&nand_info[0],
166 CFG_ENV_OFFSET_REDUND, CFG_ENV_SIZE))
167 return 1;
168 puts ("Writing to redundant Nand... ");
169 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
170 (u_char*) env_ptr);
171 } else {
172 puts ("Erasing Nand...");
173 if (nand_erase(&nand_info[0],
174 CFG_ENV_OFFSET, CFG_ENV_SIZE))
175 return 1;
176
177 puts ("Writing to Nand... ");
178 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
179 (u_char*) env_ptr);
180 }
181 if (ret || total != CFG_ENV_SIZE)
182 return 1;
183
184 puts ("done\n");
185 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
186 return ret;
187}
188#else /* ! CFG_ENV_OFFSET_REDUND */
189int saveenv(void)
190{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100191 ulong total;
192 int ret = 0;
wdenk13a56952004-06-09 14:58:14 +0000193
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100194 puts ("Erasing Nand...");
Markus Klotzbuecher3c4eb082006-03-08 00:04:04 +0100195 if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
wdenk13a56952004-06-09 14:58:14 +0000196 return 1;
197
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100198 puts ("Writing to Nand... ");
199 total = CFG_ENV_SIZE;
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100200 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100201 if (ret || total != CFG_ENV_SIZE)
202 return 1;
203
204 puts ("done\n");
205 return ret;
wdenk13a56952004-06-09 14:58:14 +0000206}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100207#endif /* CFG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000208#endif /* CMD_SAVEENV */
209
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100210#ifdef CFG_ENV_OFFSET_REDUND
211void env_relocate_spec (void)
212{
213#if !defined(ENV_IS_EMBEDDED)
Markus Klotzbuecher2770bcb2006-03-24 15:43:16 +0100214 ulong total;
215 int crc1_ok = 0, crc2_ok = 0;
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100216 env_t *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000217
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100218 total = CFG_ENV_SIZE;
219
220 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
221 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
222
223 nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
224 (u_char*) tmp_env1);
225 nand_read(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
226 (u_char*) tmp_env2);
227
228 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
229 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
230
231 if(!crc1_ok && !crc2_ok)
232 return use_default();
233 else if(crc1_ok && !crc2_ok)
234 gd->env_valid = 1;
235 else if(!crc1_ok && crc2_ok)
236 gd->env_valid = 2;
237 else {
238 /* both ok - check serial */
239 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
240 gd->env_valid = 2;
241 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
242 gd->env_valid = 1;
243 else if(tmp_env1->flags > tmp_env2->flags)
244 gd->env_valid = 1;
245 else if(tmp_env2->flags > tmp_env1->flags)
246 gd->env_valid = 2;
247 else /* flags are equal - almost impossible */
248 gd->env_valid = 1;
249
250 }
251
252 free(env_ptr);
253 if(gd->env_valid == 1) {
254 env_ptr = tmp_env1;
255 free(tmp_env2);
256 } else {
257 env_ptr = tmp_env2;
258 free(tmp_env1);
259 }
260
261#endif /* ! ENV_IS_EMBEDDED */
262}
263#else /* ! CFG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100264/*
265 * The legacy NAND code saved the environment in the first NAND device i.e.,
266 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
267 */
wdenk13a56952004-06-09 14:58:14 +0000268void env_relocate_spec (void)
269{
270#if !defined(ENV_IS_EMBEDDED)
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100271 ulong total;
272 int ret;
wdenk13a56952004-06-09 14:58:14 +0000273
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100274 total = CFG_ENV_SIZE;
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100275 ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
wdenk13a56952004-06-09 14:58:14 +0000276 if (ret || total != CFG_ENV_SIZE)
277 return use_default();
278
279 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
280 return use_default();
281#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000282}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100283#endif /* CFG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000284
Stefan Roesed12ae802006-09-12 20:19:10 +0200285#if !defined(ENV_IS_EMBEDDED)
wdenk13a56952004-06-09 14:58:14 +0000286static void use_default()
287{
wdenk13a56952004-06-09 14:58:14 +0000288 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
289
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100290 if (default_environment_size > CFG_ENV_SIZE){
wdenk13a56952004-06-09 14:58:14 +0000291 puts ("*** Error - default environment is too large\n\n");
292 return;
293 }
294
295 memset (env_ptr, 0, sizeof(env_t));
296 memcpy (env_ptr->data,
297 default_environment,
298 default_environment_size);
299 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100300 gd->env_valid = 1;
wdenk13a56952004-06-09 14:58:14 +0000301
302}
Stefan Roesed12ae802006-09-12 20:19:10 +0200303#endif
wdenk13a56952004-06-09 14:58:14 +0000304
305#endif /* CFG_ENV_IS_IN_NAND */