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