]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_nand.c
nvedit: rename error comment to CONFIG_ENV_IS_IN_
[people/ms/u-boot.git] / common / env_nand.c
CommitLineData
13a5695b 1/*
cc49cade
SW
2 * (C) Copyright 2008
3 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
4 *
13a5695b
WD
5 * (C) Copyright 2004
6 * Jian Zhang, Texas Instruments, jzhang@ti.com.
7
d12ae808 8 * (C) Copyright 2000-2006
13a5695b
WD
9 * 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>
13a5695b
WD
36#include <command.h>
37#include <environment.h>
38#include <linux/stddef.h>
e443c944 39#include <malloc.h>
addb2e16 40#include <nand.h>
13a5695b 41
c3517f91 42#if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)
13a5695b 43#define CMD_SAVEENV
e443c944 44#elif defined(CFG_ENV_OFFSET_REDUND)
90253178 45#error Cannot use CFG_ENV_OFFSET_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_NAND
13a5695b
WD
46#endif
47
e443c944
MK
48#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
13a5695b
WD
50#endif
51
13a5695b
WD
52#ifdef CONFIG_INFERNO
53#error CONFIG_INFERNO not supported yet
54#endif
55
cc49cade
SW
56#ifndef CFG_ENV_RANGE
57#define CFG_ENV_RANGE CFG_ENV_SIZE
58#endif
59
addb2e16 60int nand_legacy_rw (struct nand_chip* nand, int cmd,
13a5695b
WD
61 size_t start, size_t len,
62 size_t * retlen, u_char * buf);
addb2e16 63
13a5695b
WD
64/* references to names in env_common.c */
65extern uchar default_environment[];
66extern int default_environment_size;
67
68char * env_name_spec = "NAND";
69
70
71#ifdef ENV_IS_EMBEDDED
72extern uchar environment[];
73env_t *env_ptr = (env_t *)(&environment[0]);
74#else /* ! ENV_IS_EMBEDDED */
49822e23 75env_t *env_ptr = 0;
13a5695b
WD
76#endif /* ENV_IS_EMBEDDED */
77
78
79/* local functions */
d12ae808 80#if !defined(ENV_IS_EMBEDDED)
13a5695b 81static void use_default(void);
d12ae808 82#endif
13a5695b 83
d87080b7 84DECLARE_GLOBAL_DATA_PTR;
13a5695b
WD
85
86uchar env_get_char_spec (int index)
87{
13a5695b
WD
88 return ( *((uchar *)(gd->env_addr + index)) );
89}
90
91
92/* this is called before nand_init()
93 * so we can't read Nand to validate env data.
94 * Mark it OK for now. env_relocate() in env_common.c
99c2b434
MZ
95 * will call our relocate function which does the real
96 * validation.
d12ae808
SR
97 *
98 * When using a NAND boot image (like sequoia_nand), the environment
99 * can be embedded or attached to the U-Boot image in NAND flash. This way
100 * the SPL loads not only the U-Boot image from NAND but also the
101 * environment.
13a5695b
WD
102 */
103int env_init(void)
104{
d12ae808 105#if defined(ENV_IS_EMBEDDED)
378e7ec9 106 size_t total;
d12ae808
SR
107 int crc1_ok = 0, crc2_ok = 0;
108 env_t *tmp_env1, *tmp_env2;
109
110 total = CFG_ENV_SIZE;
111
112 tmp_env1 = env_ptr;
113 tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
114
115 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
116 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
117
118 if (!crc1_ok && !crc2_ok)
119 gd->env_valid = 0;
120 else if(crc1_ok && !crc2_ok)
121 gd->env_valid = 1;
122 else if(!crc1_ok && crc2_ok)
123 gd->env_valid = 2;
124 else {
125 /* both ok - check serial */
126 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
127 gd->env_valid = 2;
128 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
129 gd->env_valid = 1;
130 else if(tmp_env1->flags > tmp_env2->flags)
131 gd->env_valid = 1;
132 else if(tmp_env2->flags > tmp_env1->flags)
133 gd->env_valid = 2;
134 else /* flags are equal - almost impossible */
135 gd->env_valid = 1;
136 }
137
138 if (gd->env_valid == 1)
139 env_ptr = tmp_env1;
140 else if (gd->env_valid == 2)
141 env_ptr = tmp_env2;
142#else /* ENV_IS_EMBEDDED */
e443c944 143 gd->env_addr = (ulong)&default_environment[0];
13a5695b 144 gd->env_valid = 1;
d12ae808 145#endif /* ENV_IS_EMBEDDED */
13a5695b
WD
146
147 return (0);
148}
149
150#ifdef CMD_SAVEENV
addb2e16
BS
151/*
152 * The legacy NAND code saved the environment in the first NAND device i.e.,
153 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
154 */
cc49cade
SW
155int writeenv(size_t offset, u_char *buf)
156{
157 size_t end = offset + CFG_ENV_RANGE;
158 size_t amount_saved = 0;
c3db8c64 159 size_t blocksize, len;
cc49cade
SW
160
161 u_char *char_ptr;
162
163 blocksize = nand_info[0].erasesize;
c3db8c64 164 len = min(blocksize, CFG_ENV_SIZE);
cc49cade
SW
165
166 while (amount_saved < CFG_ENV_SIZE && offset < end) {
167 if (nand_block_isbad(&nand_info[0], offset)) {
168 offset += blocksize;
169 } else {
170 char_ptr = &buf[amount_saved];
c3db8c64 171 if (nand_write(&nand_info[0], offset, &len,
cc49cade
SW
172 char_ptr))
173 return 1;
174 offset += blocksize;
c3db8c64 175 amount_saved += len;
cc49cade
SW
176 }
177 }
178 if (amount_saved != CFG_ENV_SIZE)
179 return 1;
180
181 return 0;
182}
e443c944 183#ifdef CFG_ENV_OFFSET_REDUND
13a5695b
WD
184int saveenv(void)
185{
4ca79f47 186 size_t total;
2770bcb2 187 int ret = 0;
cc49cade 188 nand_erase_options_t nand_erase_options;
e443c944 189
e443c944
MK
190 env_ptr->flags++;
191 total = CFG_ENV_SIZE;
192
cc49cade
SW
193 nand_erase_options.length = CFG_ENV_RANGE;
194 nand_erase_options.quiet = 0;
195 nand_erase_options.jffs2 = 0;
196 nand_erase_options.scrub = 0;
197
198 if (CFG_ENV_RANGE < CFG_ENV_SIZE)
199 return 1;
e443c944 200 if(gd->env_valid == 1) {
cc49cade
SW
201 puts ("Erasing redundant Nand...\n");
202 nand_erase_options.offset = CFG_ENV_OFFSET_REDUND;
203 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
e443c944 204 return 1;
cc49cade 205
e443c944 206 puts ("Writing to redundant Nand... ");
cc49cade 207 ret = writeenv(CFG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
e443c944 208 } else {
cc49cade
SW
209 puts ("Erasing Nand...\n");
210 nand_erase_options.offset = CFG_ENV_OFFSET;
211 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
e443c944
MK
212 return 1;
213
214 puts ("Writing to Nand... ");
cc49cade 215 ret = writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
e443c944 216 }
cc49cade
SW
217 if (ret) {
218 puts("FAILED!\n");
e443c944 219 return 1;
cc49cade 220 }
e443c944
MK
221
222 puts ("done\n");
223 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
224 return ret;
225}
226#else /* ! CFG_ENV_OFFSET_REDUND */
227int saveenv(void)
228{
378e7ec9 229 size_t total;
d52fb7e3 230 int ret = 0;
9e4006bc 231 nand_erase_options_t nand_erase_options;
e093a247 232
cc49cade
SW
233 nand_erase_options.length = CFG_ENV_RANGE;
234 nand_erase_options.quiet = 0;
235 nand_erase_options.jffs2 = 0;
236 nand_erase_options.scrub = 0;
237 nand_erase_options.offset = CFG_ENV_OFFSET;
238
239 if (CFG_ENV_RANGE < CFG_ENV_SIZE)
240 return 1;
241 puts ("Erasing Nand...\n");
242 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
addb2e16 243 return 1;
13a5695b
WD
244
245 puts ("Writing to Nand... ");
addb2e16 246 total = CFG_ENV_SIZE;
99c2b434 247 if (writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr)) {
cc49cade 248 puts("FAILED!\n");
13a5695b 249 return 1;
cc49cade 250 }
13a5695b 251
addb2e16
BS
252 puts ("done\n");
253 return ret;
13a5695b 254}
e443c944 255#endif /* CFG_ENV_OFFSET_REDUND */
13a5695b
WD
256#endif /* CMD_SAVEENV */
257
cc49cade
SW
258int readenv (size_t offset, u_char * buf)
259{
260 size_t end = offset + CFG_ENV_RANGE;
261 size_t amount_loaded = 0;
c3db8c64 262 size_t blocksize, len;
cc49cade
SW
263
264 u_char *char_ptr;
265
266 blocksize = nand_info[0].erasesize;
c3db8c64 267 len = min(blocksize, CFG_ENV_SIZE);
cc49cade
SW
268
269 while (amount_loaded < CFG_ENV_SIZE && offset < end) {
270 if (nand_block_isbad(&nand_info[0], offset)) {
271 offset += blocksize;
272 } else {
273 char_ptr = &buf[amount_loaded];
c3db8c64 274 if (nand_read(&nand_info[0], offset, &len, char_ptr))
cc49cade
SW
275 return 1;
276 offset += blocksize;
c3db8c64 277 amount_loaded += len;
cc49cade
SW
278 }
279 }
280 if (amount_loaded != CFG_ENV_SIZE)
281 return 1;
282
283 return 0;
284}
285
e443c944
MK
286#ifdef CFG_ENV_OFFSET_REDUND
287void env_relocate_spec (void)
288{
289#if !defined(ENV_IS_EMBEDDED)
f7b16a0a 290 size_t total;
2770bcb2 291 int crc1_ok = 0, crc2_ok = 0;
e443c944
MK
292 env_t *tmp_env1, *tmp_env2;
293
e443c944
MK
294 total = CFG_ENV_SIZE;
295
296 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
297 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
298
cc49cade
SW
299 if (readenv(CFG_ENV_OFFSET, (u_char *) tmp_env1))
300 puts("No Valid Environment Area Found\n");
301 if (readenv(CFG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
302 puts("No Valid Reundant Environment Area Found\n");
e443c944
MK
303
304 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
305 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
306
307 if(!crc1_ok && !crc2_ok)
308 return use_default();
309 else if(crc1_ok && !crc2_ok)
310 gd->env_valid = 1;
311 else if(!crc1_ok && crc2_ok)
312 gd->env_valid = 2;
313 else {
314 /* both ok - check serial */
315 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
316 gd->env_valid = 2;
317 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
318 gd->env_valid = 1;
319 else if(tmp_env1->flags > tmp_env2->flags)
320 gd->env_valid = 1;
321 else if(tmp_env2->flags > tmp_env1->flags)
322 gd->env_valid = 2;
323 else /* flags are equal - almost impossible */
324 gd->env_valid = 1;
13a5695b 325
e443c944
MK
326 }
327
328 free(env_ptr);
329 if(gd->env_valid == 1) {
330 env_ptr = tmp_env1;
331 free(tmp_env2);
332 } else {
333 env_ptr = tmp_env2;
334 free(tmp_env1);
335 }
336
337#endif /* ! ENV_IS_EMBEDDED */
338}
339#else /* ! CFG_ENV_OFFSET_REDUND */
addb2e16
BS
340/*
341 * The legacy NAND code saved the environment in the first NAND device i.e.,
342 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
343 */
13a5695b
WD
344void env_relocate_spec (void)
345{
346#if !defined(ENV_IS_EMBEDDED)
d52fb7e3 347 int ret;
13a5695b 348
99c2b434 349 ret = readenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
c3db8c64 350 if (ret)
13a5695b
WD
351 return use_default();
352
353 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
354 return use_default();
355#endif /* ! ENV_IS_EMBEDDED */
13a5695b 356}
e443c944 357#endif /* CFG_ENV_OFFSET_REDUND */
13a5695b 358
d12ae808 359#if !defined(ENV_IS_EMBEDDED)
13a5695b
WD
360static void use_default()
361{
13a5695b 362 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
5bb12dbd 363 set_default_env();
13a5695b 364}
d12ae808 365#endif