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 char *env_name_spec = "NAND";
61 #if defined(ENV_IS_EMBEDDED)
62 extern uchar environment[];
63 env_t *env_ptr = (env_t *)(&environment[0]);
64 #elif defined(CONFIG_NAND_ENV_DST)
65 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
66 #else /* ! ENV_IS_EMBEDDED */
68 #endif /* ENV_IS_EMBEDDED */
70 DECLARE_GLOBAL_DATA_PTR;
72 uchar env_get_char_spec (int index)
74 return ( *((uchar *)(gd->env_addr + index)) );
78 * This is called before nand_init() so we can't read NAND to
81 * Mark it OK for now. env_relocate() in env_common.c will call our
82 * relocate function which does the real validation.
84 * When using a NAND boot image (like sequoia_nand), the environment
85 * can be embedded or attached to the U-Boot image in NAND flash.
86 * This way the SPL loads not only the U-Boot image from NAND but
87 * also the environment.
91 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
92 int crc1_ok = 0, crc2_ok = 0;
95 #ifdef CONFIG_ENV_OFFSET_REDUND
98 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
99 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
104 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
106 if (!crc1_ok && !crc2_ok) {
111 } else if (crc1_ok && !crc2_ok) {
114 #ifdef CONFIG_ENV_OFFSET_REDUND
115 else if (!crc1_ok && crc2_ok) {
118 /* both ok - check serial */
119 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
121 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
123 else if(tmp_env1->flags > tmp_env2->flags)
125 else if(tmp_env2->flags > tmp_env1->flags)
127 else /* flags are equal - almost impossible */
131 if (gd->env_valid == 2)
135 if (gd->env_valid == 1)
138 gd->env_addr = (ulong)env_ptr->data;
140 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
141 gd->env_addr = (ulong)&default_environment[0];
143 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
150 * The legacy NAND code saved the environment in the first NAND device i.e.,
151 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
153 int writeenv(size_t offset, u_char *buf)
155 size_t end = offset + CONFIG_ENV_RANGE;
156 size_t amount_saved = 0;
157 size_t blocksize, len;
161 blocksize = nand_info[0].erasesize;
162 len = min(blocksize, CONFIG_ENV_SIZE);
164 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
165 if (nand_block_isbad(&nand_info[0], offset)) {
168 char_ptr = &buf[amount_saved];
169 if (nand_write(&nand_info[0], offset, &len,
176 if (amount_saved != CONFIG_ENV_SIZE)
182 #ifdef CONFIG_ENV_OFFSET_REDUND
183 static unsigned char env_flags;
191 nand_erase_options_t nand_erase_options;
193 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
194 nand_erase_options.length = CONFIG_ENV_RANGE;
196 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
199 res = (char *)&env_new.data;
200 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
202 error("Cannot export environment: errno = %d\n", errno);
205 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
206 env_new.flags = ++env_flags; /* increase the serial */
208 if(gd->env_valid == 1) {
209 puts("Erasing redundant NAND...\n");
210 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
211 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
214 puts("Writing to redundant NAND... ");
215 ret = writeenv(CONFIG_ENV_OFFSET_REDUND,
218 puts("Erasing NAND...\n");
219 nand_erase_options.offset = CONFIG_ENV_OFFSET;
220 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
223 puts("Writing to NAND... ");
224 ret = writeenv(CONFIG_ENV_OFFSET,
234 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
238 #else /* ! CONFIG_ENV_OFFSET_REDUND */
245 nand_erase_options_t nand_erase_options;
247 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
248 nand_erase_options.length = CONFIG_ENV_RANGE;
249 nand_erase_options.offset = CONFIG_ENV_OFFSET;
251 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
254 res = (char *)&env_new.data;
255 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
257 error("Cannot export environment: errno = %d\n", errno);
260 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
262 puts("Erasing Nand...\n");
263 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
266 puts("Writing to Nand... ");
267 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
275 #endif /* CONFIG_ENV_OFFSET_REDUND */
276 #endif /* CMD_SAVEENV */
278 int readenv(size_t offset, u_char * buf)
280 size_t end = offset + CONFIG_ENV_RANGE;
281 size_t amount_loaded = 0;
282 size_t blocksize, len;
286 blocksize = nand_info[0].erasesize;
289 len = min(blocksize, CONFIG_ENV_SIZE);
291 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
292 if (nand_block_isbad(&nand_info[0], offset)) {
295 char_ptr = &buf[amount_loaded];
296 if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr))
299 amount_loaded += len;
302 if (amount_loaded != CONFIG_ENV_SIZE)
308 #ifdef CONFIG_ENV_OFFSET_OOB
309 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
311 struct mtd_oob_ops ops;
312 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
316 ops.mode = MTD_OOB_AUTO;
318 ops.ooblen = ENV_OFFSET_SIZE;
319 ops.oobbuf = (void *) oob_buf;
321 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
323 printf("error reading OOB block 0\n");
327 if (oob_buf[0] == ENV_OOB_MARKER) {
328 *result = oob_buf[1] * nand->erasesize;
329 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
330 *result = oob_buf[1];
332 printf("No dynamic environment marker in OOB block 0\n");
340 #ifdef CONFIG_ENV_OFFSET_REDUND
341 void env_relocate_spec(void)
343 #if !defined(ENV_IS_EMBEDDED)
344 int crc1_ok = 0, crc2_ok = 0;
345 env_t *ep, *tmp_env1, *tmp_env2;
347 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
348 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
350 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
351 puts("Can't allocate buffers for environment\n");
354 set_default_env("!malloc() failed");
358 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
359 puts("No Valid Environment Area found\n");
361 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
362 puts("No Valid Redundant Environment Area found\n");
364 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
365 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
367 if (!crc1_ok && !crc2_ok) {
370 set_default_env("!bad CRC");
372 } else if (crc1_ok && !crc2_ok) {
374 } else if (!crc1_ok && crc2_ok) {
377 /* both ok - check serial */
378 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
380 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
382 else if (tmp_env1->flags > tmp_env2->flags)
384 else if (tmp_env2->flags > tmp_env1->flags)
386 else /* flags are equal - almost impossible */
393 if (gd->env_valid == 1)
398 env_flags = ep->flags;
399 env_import((char *)ep, 0);
404 #endif /* ! ENV_IS_EMBEDDED */
406 #else /* ! CONFIG_ENV_OFFSET_REDUND */
408 * The legacy NAND code saved the environment in the first NAND
409 * device i.e., nand_dev_desc + 0. This is also the behaviour using
412 void env_relocate_spec (void)
414 #if !defined(ENV_IS_EMBEDDED)
416 char buf[CONFIG_ENV_SIZE];
418 #if defined(CONFIG_ENV_OFFSET_OOB)
419 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
421 * If unable to read environment offset from NAND OOB then fall through
422 * to the normal environment reading code below
425 printf("Found Environment offset in OOB..\n");
427 set_default_env("!no env offset in OOB");
432 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
434 set_default_env("!readenv() failed");
439 #endif /* ! ENV_IS_EMBEDDED */
441 #endif /* CONFIG_ENV_OFFSET_REDUND */