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,
35 #include <environment.h>
36 #include <linux/stddef.h>
42 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
44 #elif defined(CONFIG_ENV_OFFSET_REDUND)
45 #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
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
53 #ifndef CONFIG_ENV_RANGE
54 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
57 char *env_name_spec = "NAND";
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 */
65 #endif /* ENV_IS_EMBEDDED */
67 DECLARE_GLOBAL_DATA_PTR;
69 uchar env_get_char_spec(int index)
71 return *((uchar *)(gd->env_addr + index));
75 * This is called before nand_init() so we can't read NAND to
78 * Mark it OK for now. env_relocate() in env_common.c will call our
79 * relocate function which does the real validation.
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.
88 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
89 int crc1_ok = 0, crc2_ok = 0;
92 #ifdef CONFIG_ENV_OFFSET_REDUND
95 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
96 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
99 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
101 if (!crc1_ok && !crc2_ok) {
106 } else if (crc1_ok && !crc2_ok) {
109 #ifdef CONFIG_ENV_OFFSET_REDUND
110 else if (!crc1_ok && crc2_ok) {
113 /* both ok - check serial */
114 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
116 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
118 else if (tmp_env1->flags > tmp_env2->flags)
120 else if (tmp_env2->flags > tmp_env1->flags)
122 else /* flags are equal - almost impossible */
126 if (gd->env_valid == 2)
130 if (gd->env_valid == 1)
133 gd->env_addr = (ulong)env_ptr->data;
135 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
136 gd->env_addr = (ulong)&default_environment[0];
138 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
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.
148 int writeenv(size_t offset, u_char *buf)
150 size_t end = offset + CONFIG_ENV_RANGE;
151 size_t amount_saved = 0;
152 size_t blocksize, len;
155 blocksize = nand_info[0].erasesize;
156 len = min(blocksize, CONFIG_ENV_SIZE);
158 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
159 if (nand_block_isbad(&nand_info[0], offset)) {
162 char_ptr = &buf[amount_saved];
163 if (nand_write(&nand_info[0], offset, &len, char_ptr))
170 if (amount_saved != CONFIG_ENV_SIZE)
176 #ifdef CONFIG_ENV_OFFSET_REDUND
177 static unsigned char env_flags;
185 nand_erase_options_t nand_erase_options;
187 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
188 nand_erase_options.length = CONFIG_ENV_RANGE;
190 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
193 res = (char *)&env_new.data;
194 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
196 error("Cannot export environment: errno = %d\n", errno);
199 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
200 env_new.flags = ++env_flags; /* increase the serial */
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))
208 puts("Writing to redundant NAND... ");
209 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
211 puts("Erasing NAND...\n");
212 nand_erase_options.offset = CONFIG_ENV_OFFSET;
213 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
216 puts("Writing to NAND... ");
217 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
226 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
230 #else /* ! CONFIG_ENV_OFFSET_REDUND */
237 nand_erase_options_t nand_erase_options;
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;
243 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
246 res = (char *)&env_new.data;
247 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
249 error("Cannot export environment: errno = %d\n", errno);
252 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
254 puts("Erasing Nand...\n");
255 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
258 puts("Writing to Nand... ");
259 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
267 #endif /* CONFIG_ENV_OFFSET_REDUND */
268 #endif /* CMD_SAVEENV */
270 int readenv(size_t offset, u_char *buf)
272 size_t end = offset + CONFIG_ENV_RANGE;
273 size_t amount_loaded = 0;
274 size_t blocksize, len;
277 blocksize = nand_info[0].erasesize;
281 len = min(blocksize, CONFIG_ENV_SIZE);
283 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
284 if (nand_block_isbad(&nand_info[0], offset)) {
287 char_ptr = &buf[amount_loaded];
288 if (nand_read_skip_bad(&nand_info[0], offset,
293 amount_loaded += len;
297 if (amount_loaded != CONFIG_ENV_SIZE)
303 #ifdef CONFIG_ENV_OFFSET_OOB
304 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
306 struct mtd_oob_ops ops;
307 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
311 ops.mode = MTD_OOB_AUTO;
313 ops.ooblen = ENV_OFFSET_SIZE;
314 ops.oobbuf = (void *)oob_buf;
316 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
318 printf("error reading OOB block 0\n");
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];
327 printf("No dynamic environment marker in OOB block 0\n");
335 #ifdef CONFIG_ENV_OFFSET_REDUND
336 void env_relocate_spec(void)
338 #if !defined(ENV_IS_EMBEDDED)
339 int crc1_ok = 0, crc2_ok = 0;
340 env_t *ep, *tmp_env1, *tmp_env2;
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");
350 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
351 puts("No Valid Environment Area found\n");
353 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
354 puts("No Valid Redundant Environment Area found\n");
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;
359 if (!crc1_ok && !crc2_ok) {
360 set_default_env("!bad CRC");
362 } else if (crc1_ok && !crc2_ok) {
364 } else if (!crc1_ok && crc2_ok) {
367 /* both ok - check serial */
368 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
370 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
372 else if (tmp_env1->flags > tmp_env2->flags)
374 else if (tmp_env2->flags > tmp_env1->flags)
376 else /* flags are equal - almost impossible */
382 if (gd->env_valid == 1)
387 env_flags = ep->flags;
388 env_import((char *)ep, 0);
394 #endif /* ! ENV_IS_EMBEDDED */
396 #else /* ! CONFIG_ENV_OFFSET_REDUND */
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
402 void env_relocate_spec(void)
404 #if !defined(ENV_IS_EMBEDDED)
406 char buf[CONFIG_ENV_SIZE];
408 #if defined(CONFIG_ENV_OFFSET_OOB)
409 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
411 * If unable to read environment offset from NAND OOB then fall through
412 * to the normal environment reading code below
415 printf("Found Environment offset in OOB..\n");
417 set_default_env("!no env offset in OOB");
422 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
424 set_default_env("!readenv() failed");
429 #endif /* ! ENV_IS_EMBEDDED */
431 #endif /* CONFIG_ENV_OFFSET_REDUND */