]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_nand.c
Merge branch 'master' of /home/wd/git/u-boot/master
[karo-tx-uboot.git] / common / env_nand.c
1 /*
2  * (C) Copyright 2008
3  * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
4  *
5  * (C) Copyright 2004
6  * Jian Zhang, Texas Instruments, jzhang@ti.com.
7
8  * (C) Copyright 2000-2006
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>
36 #include <command.h>
37 #include <environment.h>
38 #include <linux/stddef.h>
39 #include <malloc.h>
40 #include <nand.h>
41 #include <asm/errno.h>
42
43 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
44 #define CMD_SAVEENV
45 #elif defined(CONFIG_ENV_OFFSET_REDUND)
46 #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
47 #endif
48
49 #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
50 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
51 #endif
52
53 #ifndef CONFIG_ENV_RANGE
54 #define CONFIG_ENV_RANGE        CONFIG_ENV_SIZE
55 #endif
56
57 /* references to names in env_common.c */
58 extern uchar default_environment[];
59
60 char * env_name_spec = "NAND";
61
62
63 #if defined(ENV_IS_EMBEDDED)
64 extern uchar environment[];
65 env_t *env_ptr = (env_t *)(&environment[0]);
66 #elif defined(CONFIG_NAND_ENV_DST)
67 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
68 #else /* ! ENV_IS_EMBEDDED */
69 env_t *env_ptr = 0;
70 #endif /* ENV_IS_EMBEDDED */
71
72
73 /* local functions */
74 #if !defined(ENV_IS_EMBEDDED)
75 static void use_default(void);
76 #endif
77
78 DECLARE_GLOBAL_DATA_PTR;
79
80 uchar env_get_char_spec (int index)
81 {
82         return ( *((uchar *)(gd->env_addr + index)) );
83 }
84
85
86 /* this is called before nand_init()
87  * so we can't read Nand to validate env data.
88  * Mark it OK for now. env_relocate() in env_common.c
89  * will call our relocate function which does the real
90  * validation.
91  *
92  * When using a NAND boot image (like sequoia_nand), the environment
93  * can be embedded or attached to the U-Boot image in NAND flash. This way
94  * the SPL loads not only the U-Boot image from NAND but also the
95  * environment.
96  */
97 int env_init(void)
98 {
99 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
100         int crc1_ok = 0, crc2_ok = 0;
101         env_t *tmp_env1;
102
103 #ifdef CONFIG_ENV_OFFSET_REDUND
104         env_t *tmp_env2;
105
106         tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
107         crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
108 #endif
109
110         tmp_env1 = env_ptr;
111
112         crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
113
114         if (!crc1_ok && !crc2_ok) {
115                 gd->env_addr  = 0;
116                 gd->env_valid = 0;
117
118                 return 0;
119         } else if (crc1_ok && !crc2_ok) {
120                 gd->env_valid = 1;
121         }
122 #ifdef CONFIG_ENV_OFFSET_REDUND
123         else if (!crc1_ok && crc2_ok) {
124                 gd->env_valid = 2;
125         } else {
126                 /* both ok - check serial */
127                 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
128                         gd->env_valid = 2;
129                 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
130                         gd->env_valid = 1;
131                 else if(tmp_env1->flags > tmp_env2->flags)
132                         gd->env_valid = 1;
133                 else if(tmp_env2->flags > tmp_env1->flags)
134                         gd->env_valid = 2;
135                 else /* flags are equal - almost impossible */
136                         gd->env_valid = 1;
137         }
138
139         if (gd->env_valid == 2)
140                 env_ptr = tmp_env2;
141         else
142 #endif
143         if (gd->env_valid == 1)
144                 env_ptr = tmp_env1;
145
146         gd->env_addr = (ulong)env_ptr->data;
147
148 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
149         gd->env_addr  = (ulong)&default_environment[0];
150         gd->env_valid = 1;
151 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
152
153         return (0);
154 }
155
156 #ifdef CMD_SAVEENV
157 /*
158  * The legacy NAND code saved the environment in the first NAND device i.e.,
159  * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
160  */
161 int writeenv(size_t offset, u_char *buf)
162 {
163         size_t end = offset + CONFIG_ENV_RANGE;
164         size_t amount_saved = 0;
165         size_t blocksize, len;
166
167         u_char *char_ptr;
168
169         blocksize = nand_info[0].erasesize;
170         len = min(blocksize, CONFIG_ENV_SIZE);
171
172         while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
173                 if (nand_block_isbad(&nand_info[0], offset)) {
174                         offset += blocksize;
175                 } else {
176                         char_ptr = &buf[amount_saved];
177                         if (nand_write(&nand_info[0], offset, &len,
178                                         char_ptr))
179                                 return 1;
180                         offset += blocksize;
181                         amount_saved += len;
182                 }
183         }
184         if (amount_saved != CONFIG_ENV_SIZE)
185                 return 1;
186
187         return 0;
188 }
189 #ifdef CONFIG_ENV_OFFSET_REDUND
190 int saveenv(void)
191 {
192         int ret = 0;
193         nand_erase_options_t nand_erase_options;
194
195         env_ptr->flags++;
196
197         nand_erase_options.length = CONFIG_ENV_RANGE;
198         nand_erase_options.quiet = 0;
199         nand_erase_options.jffs2 = 0;
200         nand_erase_options.scrub = 0;
201
202         if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
203                 return 1;
204         if(gd->env_valid == 1) {
205                 puts ("Erasing redundant Nand...\n");
206                 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
207                 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
208                         return 1;
209
210                 puts ("Writing to redundant Nand... ");
211                 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
212         } else {
213                 puts ("Erasing Nand...\n");
214                 nand_erase_options.offset = CONFIG_ENV_OFFSET;
215                 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
216                         return 1;
217
218                 puts ("Writing to Nand... ");
219                 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
220         }
221         if (ret) {
222                 puts("FAILED!\n");
223                 return 1;
224         }
225
226         puts ("done\n");
227         gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
228         return ret;
229 }
230 #else /* ! CONFIG_ENV_OFFSET_REDUND */
231 int saveenv(void)
232 {
233         int ret = 0;
234         nand_erase_options_t nand_erase_options;
235
236         nand_erase_options.length = CONFIG_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 = CONFIG_ENV_OFFSET;
241
242         if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
243                 return 1;
244         puts ("Erasing Nand...\n");
245         if (nand_erase_opts(&nand_info[0], &nand_erase_options))
246                 return 1;
247
248         puts ("Writing to Nand... ");
249         if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
250                 puts("FAILED!\n");
251                 return 1;
252         }
253
254         puts ("done\n");
255         return ret;
256 }
257 #endif /* CONFIG_ENV_OFFSET_REDUND */
258 #endif /* CMD_SAVEENV */
259
260 int readenv (size_t offset, u_char * buf)
261 {
262         size_t end = offset + CONFIG_ENV_RANGE;
263         size_t amount_loaded = 0;
264         size_t blocksize, len;
265
266         u_char *char_ptr;
267
268         blocksize = nand_info[0].erasesize;
269         if (!blocksize)
270                 return 1;
271         len = min(blocksize, CONFIG_ENV_SIZE);
272
273         while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
274                 if (nand_block_isbad(&nand_info[0], offset)) {
275                         offset += blocksize;
276                 } else {
277                         char_ptr = &buf[amount_loaded];
278                         if (nand_read(&nand_info[0], offset, &len, char_ptr))
279                                 return 1;
280                         offset += blocksize;
281                         amount_loaded += len;
282                 }
283         }
284         if (amount_loaded != CONFIG_ENV_SIZE)
285                 return 1;
286
287         return 0;
288 }
289
290 #ifdef CONFIG_ENV_OFFSET_OOB
291 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
292 {
293         struct mtd_oob_ops ops;
294         uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
295         int ret;
296
297         ops.datbuf = NULL;
298         ops.mode = MTD_OOB_AUTO;
299         ops.ooboffs = 0;
300         ops.ooblen = ENV_OFFSET_SIZE;
301         ops.oobbuf = (void *) oob_buf;
302
303         ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
304         if (ret) {
305                 printf("error reading OOB block 0\n");
306                 return ret;
307         }
308
309         if (oob_buf[0] == ENV_OOB_MARKER) {
310                 *result = oob_buf[1] * nand->erasesize;
311         } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
312                 *result = oob_buf[1];
313         } else {
314                 printf("No dynamic environment marker in OOB block 0\n");
315                 return -ENOENT;
316         }
317
318         return 0;
319 }
320 #endif
321
322 #ifdef CONFIG_ENV_OFFSET_REDUND
323 void env_relocate_spec (void)
324 {
325 #if !defined(ENV_IS_EMBEDDED)
326         int crc1_ok = 0, crc2_ok = 0;
327         env_t *tmp_env1, *tmp_env2;
328
329         tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
330         tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
331
332         if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
333                 puts("Can't allocate buffers for environment\n");
334                 free (tmp_env1);
335                 free (tmp_env2);
336                 return use_default();
337         }
338
339         if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
340                 puts("No Valid Environment Area Found\n");
341         if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
342                 puts("No Valid Reundant Environment Area Found\n");
343
344         crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
345         crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
346
347         if(!crc1_ok && !crc2_ok) {
348                 free(tmp_env1);
349                 free(tmp_env2);
350                 return use_default();
351         } else if(crc1_ok && !crc2_ok)
352                 gd->env_valid = 1;
353         else if(!crc1_ok && crc2_ok)
354                 gd->env_valid = 2;
355         else {
356                 /* both ok - check serial */
357                 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
358                         gd->env_valid = 2;
359                 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
360                         gd->env_valid = 1;
361                 else if(tmp_env1->flags > tmp_env2->flags)
362                         gd->env_valid = 1;
363                 else if(tmp_env2->flags > tmp_env1->flags)
364                         gd->env_valid = 2;
365                 else /* flags are equal - almost impossible */
366                         gd->env_valid = 1;
367
368         }
369
370         free(env_ptr);
371         if(gd->env_valid == 1) {
372                 env_ptr = tmp_env1;
373                 free(tmp_env2);
374         } else {
375                 env_ptr = tmp_env2;
376                 free(tmp_env1);
377         }
378
379 #endif /* ! ENV_IS_EMBEDDED */
380 }
381 #else /* ! CONFIG_ENV_OFFSET_REDUND */
382 /*
383  * The legacy NAND code saved the environment in the first NAND device i.e.,
384  * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
385  */
386 void env_relocate_spec (void)
387 {
388 #if !defined(ENV_IS_EMBEDDED)
389         int ret;
390
391 #if defined(CONFIG_ENV_OFFSET_OOB)
392         ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
393         /* If unable to read environment offset from NAND OOB then fall through
394          * to the normal environment reading code below
395          */
396         if (!ret)
397                 printf("Found Environment offset in OOB..\n");
398         else
399                 return use_default();
400 #endif
401
402         ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
403         if (ret)
404                 return use_default();
405
406         if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
407                 return use_default();
408 #endif /* ! ENV_IS_EMBEDDED */
409 }
410 #endif /* CONFIG_ENV_OFFSET_REDUND */
411
412 #if !defined(ENV_IS_EMBEDDED)
413 static void use_default()
414 {
415         puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
416         set_default_env();
417 }
418 #endif