2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
9 * Jian Zhang, Texas Instruments, jzhang@ti.com.
11 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Andreas Heppel <aheppel@sysgo.de>
14 * See file CREDITS for list of people who contributed to this
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.
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.
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,
37 #include <environment.h>
38 #include <linux/stddef.h>
44 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
46 #elif defined(CONFIG_ENV_OFFSET_REDUND)
47 #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
50 #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
51 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
54 #ifndef CONFIG_ENV_RANGE
55 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
58 /* references to names in env_common.c */
59 extern uchar default_environment[];
61 char *env_name_spec = "NAND";
64 #if defined(ENV_IS_EMBEDDED)
65 extern uchar environment[];
66 env_t *env_ptr = (env_t *)(&environment[0]);
67 #elif defined(CONFIG_NAND_ENV_DST)
68 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
69 #else /* ! ENV_IS_EMBEDDED */
71 #endif /* ENV_IS_EMBEDDED */
73 DECLARE_GLOBAL_DATA_PTR;
75 uchar env_get_char_spec (int index)
77 return ( *((uchar *)(gd->env_addr + index)) );
81 * This is called before nand_init() so we can't read NAND to
84 * Mark it OK for now. env_relocate() in env_common.c will call our
85 * relocate function which does the real validation.
87 * When using a NAND boot image (like sequoia_nand), the environment
88 * can be embedded or attached to the U-Boot image in NAND flash.
89 * This way the SPL loads not only the U-Boot image from NAND but
90 * also the environment.
94 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
95 int crc1_ok = 0, crc2_ok = 0;
98 #ifdef CONFIG_ENV_OFFSET_REDUND
101 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
102 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
107 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
109 if (!crc1_ok && !crc2_ok) {
114 } else if (crc1_ok && !crc2_ok) {
117 #ifdef CONFIG_ENV_OFFSET_REDUND
118 else if (!crc1_ok && crc2_ok) {
121 /* both ok - check serial */
122 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
124 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
126 else if(tmp_env1->flags > tmp_env2->flags)
128 else if(tmp_env2->flags > tmp_env1->flags)
130 else /* flags are equal - almost impossible */
134 if (gd->env_valid == 2)
138 if (gd->env_valid == 1)
141 gd->env_addr = (ulong)env_ptr->data;
143 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
144 gd->env_addr = (ulong)&default_environment[0];
146 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
153 * The legacy NAND code saved the environment in the first NAND device i.e.,
154 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
156 int writeenv(size_t offset, u_char *buf)
158 size_t end = offset + CONFIG_ENV_RANGE;
159 size_t amount_saved = 0;
160 size_t blocksize, len;
164 blocksize = nand_info[0].erasesize;
165 len = min(blocksize, CONFIG_ENV_SIZE);
167 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
168 if (nand_block_isbad(&nand_info[0], offset)) {
171 char_ptr = &buf[amount_saved];
172 if (nand_write(&nand_info[0], offset, &len,
179 if (amount_saved != CONFIG_ENV_SIZE)
185 #ifdef CONFIG_ENV_OFFSET_REDUND
186 static unsigned char env_flags;
194 nand_erase_options_t nand_erase_options;
196 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
197 nand_erase_options.length = CONFIG_ENV_RANGE;
199 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
202 res = (char *)&env_new.data;
203 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
205 error("Cannot export environment: errno = %d\n", errno);
208 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
209 env_new.flags = ++env_flags; /* increase the serial */
211 if(gd->env_valid == 1) {
212 puts("Erasing redundant NAND...\n");
213 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
214 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
217 puts("Writing to redundant NAND... ");
218 ret = writeenv(CONFIG_ENV_OFFSET_REDUND,
221 puts("Erasing NAND...\n");
222 nand_erase_options.offset = CONFIG_ENV_OFFSET;
223 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
226 puts("Writing to NAND... ");
227 ret = writeenv(CONFIG_ENV_OFFSET,
237 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
241 #else /* ! CONFIG_ENV_OFFSET_REDUND */
248 nand_erase_options_t nand_erase_options;
250 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
251 nand_erase_options.length = CONFIG_ENV_RANGE;
252 nand_erase_options.offset = CONFIG_ENV_OFFSET;
254 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
257 res = (char *)&env_new.data;
258 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
260 error("Cannot export environment: errno = %d\n", errno);
263 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
265 puts("Erasing Nand...\n");
266 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
269 puts("Writing to Nand... ");
270 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
278 #endif /* CONFIG_ENV_OFFSET_REDUND */
279 #endif /* CMD_SAVEENV */
281 int readenv(size_t offset, u_char * buf)
283 size_t end = offset + CONFIG_ENV_RANGE;
284 size_t amount_loaded = 0;
285 size_t blocksize, len;
289 blocksize = nand_info[0].erasesize;
292 len = min(blocksize, CONFIG_ENV_SIZE);
294 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
295 if (nand_block_isbad(&nand_info[0], offset)) {
298 char_ptr = &buf[amount_loaded];
299 if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr))
302 amount_loaded += len;
305 if (amount_loaded != CONFIG_ENV_SIZE)
311 #ifdef CONFIG_ENV_OFFSET_OOB
312 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
314 struct mtd_oob_ops ops;
315 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
319 ops.mode = MTD_OOB_AUTO;
321 ops.ooblen = ENV_OFFSET_SIZE;
322 ops.oobbuf = (void *) oob_buf;
324 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
326 printf("error reading OOB block 0\n");
330 if (oob_buf[0] == ENV_OOB_MARKER) {
331 *result = oob_buf[1] * nand->erasesize;
332 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
333 *result = oob_buf[1];
335 printf("No dynamic environment marker in OOB block 0\n");
343 #ifdef CONFIG_ENV_OFFSET_REDUND
344 void env_relocate_spec(void)
346 #if !defined(ENV_IS_EMBEDDED)
347 int crc1_ok = 0, crc2_ok = 0;
348 env_t *ep, *tmp_env1, *tmp_env2;
350 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
351 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
353 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
354 puts("Can't allocate buffers for environment\n");
357 set_default_env("!malloc() failed");
361 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
362 puts("No Valid Environment Area found\n");
364 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
365 puts("No Valid Redundant Environment Area found\n");
367 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
368 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
370 if (!crc1_ok && !crc2_ok) {
373 set_default_env("!bad CRC");
375 } else if (crc1_ok && !crc2_ok) {
377 } else if (!crc1_ok && crc2_ok) {
380 /* both ok - check serial */
381 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
383 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
385 else if (tmp_env1->flags > tmp_env2->flags)
387 else if (tmp_env2->flags > tmp_env1->flags)
389 else /* flags are equal - almost impossible */
396 if (gd->env_valid == 1)
401 env_flags = ep->flags;
402 env_import((char *)ep, 0);
407 #endif /* ! ENV_IS_EMBEDDED */
409 #else /* ! CONFIG_ENV_OFFSET_REDUND */
411 * The legacy NAND code saved the environment in the first NAND
412 * device i.e., nand_dev_desc + 0. This is also the behaviour using
415 void env_relocate_spec (void)
417 #if !defined(ENV_IS_EMBEDDED)
419 char buf[CONFIG_ENV_SIZE];
421 #if defined(CONFIG_ENV_OFFSET_OOB)
422 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
424 * If unable to read environment offset from NAND OOB then fall through
425 * to the normal environment reading code below
428 printf("Found Environment offset in OOB..\n");
430 set_default_env("!no env offset in OOB");
435 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
437 set_default_env("!readenv() failed");
442 #endif /* ! ENV_IS_EMBEDDED */
444 #endif /* CONFIG_ENV_OFFSET_REDUND */