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