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;
70 * This is called before nand_init() so we can't read NAND to
73 * Mark it OK for now. env_relocate() in env_common.c will call our
74 * relocate function which does the real validation.
76 * When using a NAND boot image (like sequoia_nand), the environment
77 * can be embedded or attached to the U-Boot image in NAND flash.
78 * This way the SPL loads not only the U-Boot image from NAND but
79 * also the environment.
83 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
84 int crc1_ok = 0, crc2_ok = 0;
87 #ifdef CONFIG_ENV_OFFSET_REDUND
90 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
91 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
94 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
96 if (!crc1_ok && !crc2_ok) {
101 } else if (crc1_ok && !crc2_ok) {
104 #ifdef CONFIG_ENV_OFFSET_REDUND
105 else if (!crc1_ok && crc2_ok) {
108 /* both ok - check serial */
109 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
111 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
113 else if (tmp_env1->flags > tmp_env2->flags)
115 else if (tmp_env2->flags > tmp_env1->flags)
117 else /* flags are equal - almost impossible */
121 if (gd->env_valid == 2)
125 if (gd->env_valid == 1)
128 gd->env_addr = (ulong)env_ptr->data;
130 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
131 gd->env_addr = (ulong)&default_environment[0];
133 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
140 * The legacy NAND code saved the environment in the first NAND device i.e.,
141 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
143 int writeenv(size_t offset, u_char *buf)
145 size_t end = offset + CONFIG_ENV_RANGE;
146 size_t amount_saved = 0;
147 size_t blocksize, len;
150 blocksize = nand_info[0].erasesize;
151 len = min(blocksize, CONFIG_ENV_SIZE);
153 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
154 if (nand_block_isbad(&nand_info[0], offset)) {
157 char_ptr = &buf[amount_saved];
158 if (nand_write(&nand_info[0], offset, &len, char_ptr))
165 if (amount_saved != CONFIG_ENV_SIZE)
171 #ifdef CONFIG_ENV_OFFSET_REDUND
172 static unsigned char env_flags;
180 nand_erase_options_t nand_erase_options;
182 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
183 nand_erase_options.length = CONFIG_ENV_RANGE;
185 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
188 res = (char *)&env_new.data;
189 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
191 error("Cannot export environment: errno = %d\n", errno);
194 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
195 env_new.flags = ++env_flags; /* increase the serial */
197 if (gd->env_valid == 1) {
198 puts("Erasing redundant NAND...\n");
199 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
200 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
203 puts("Writing to redundant NAND... ");
204 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
206 puts("Erasing NAND...\n");
207 nand_erase_options.offset = CONFIG_ENV_OFFSET;
208 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
211 puts("Writing to NAND... ");
212 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
221 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
225 #else /* ! CONFIG_ENV_OFFSET_REDUND */
229 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
232 nand_erase_options_t nand_erase_options;
234 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
235 nand_erase_options.length = CONFIG_ENV_RANGE;
236 nand_erase_options.offset = CONFIG_ENV_OFFSET;
238 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
241 res = (char *)&env_new->data;
242 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
244 error("Cannot export environment: errno = %d\n", errno);
247 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
249 puts("Erasing Nand...\n");
250 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
253 puts("Writing to Nand... ");
254 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)env_new)) {
262 #endif /* CONFIG_ENV_OFFSET_REDUND */
263 #endif /* CMD_SAVEENV */
265 int readenv(size_t offset, u_char *buf)
267 size_t end = offset + CONFIG_ENV_RANGE;
268 size_t amount_loaded = 0;
269 size_t blocksize, len;
272 blocksize = nand_info[0].erasesize;
276 len = min(blocksize, CONFIG_ENV_SIZE);
278 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
279 if (nand_block_isbad(&nand_info[0], offset)) {
282 char_ptr = &buf[amount_loaded];
283 if (nand_read_skip_bad(&nand_info[0], offset,
285 nand_info[0].size, char_ptr))
289 amount_loaded += len;
293 if (amount_loaded != CONFIG_ENV_SIZE)
299 #ifdef CONFIG_ENV_OFFSET_OOB
300 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
302 struct mtd_oob_ops ops;
303 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
307 ops.mode = MTD_OOB_AUTO;
309 ops.ooblen = ENV_OFFSET_SIZE;
310 ops.oobbuf = (void *)oob_buf;
312 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
314 printf("error reading OOB block 0\n");
318 if (oob_buf[0] == ENV_OOB_MARKER) {
319 *result = oob_buf[1] * nand->erasesize;
320 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
321 *result = oob_buf[1];
323 printf("No dynamic environment marker in OOB block 0\n");
331 #ifdef CONFIG_ENV_OFFSET_REDUND
332 void env_relocate_spec(void)
334 #if !defined(ENV_IS_EMBEDDED)
335 int read1_fail = 0, read2_fail = 0;
336 int crc1_ok = 0, crc2_ok = 0;
337 env_t *ep, *tmp_env1, *tmp_env2;
339 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
340 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
341 if (tmp_env1 == NULL || tmp_env2 == NULL) {
342 puts("Can't allocate buffers for environment\n");
343 set_default_env("!malloc() failed");
347 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
348 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
350 if (read1_fail && read2_fail)
351 puts("*** Error - No Valid Environment Area found\n");
352 else if (read1_fail || read2_fail)
353 puts("*** Warning - some problems detected "
354 "reading environment; recovered successfully\n");
356 crc1_ok = !read1_fail &&
357 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
358 crc2_ok = !read2_fail &&
359 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
361 if (!crc1_ok && !crc2_ok) {
362 set_default_env("!bad CRC");
364 } else if (crc1_ok && !crc2_ok) {
366 } else if (!crc1_ok && crc2_ok) {
369 /* both ok - check serial */
370 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
372 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
374 else if (tmp_env1->flags > tmp_env2->flags)
376 else if (tmp_env2->flags > tmp_env1->flags)
378 else /* flags are equal - almost impossible */
384 if (gd->env_valid == 1)
389 env_flags = ep->flags;
390 env_import((char *)ep, 0);
396 #endif /* ! ENV_IS_EMBEDDED */
398 #else /* ! CONFIG_ENV_OFFSET_REDUND */
400 * The legacy NAND code saved the environment in the first NAND
401 * device i.e., nand_dev_desc + 0. This is also the behaviour using
404 void env_relocate_spec(void)
406 #if !defined(ENV_IS_EMBEDDED)
408 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
410 #if defined(CONFIG_ENV_OFFSET_OOB)
411 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
413 * If unable to read environment offset from NAND OOB then fall through
414 * to the normal environment reading code below
417 printf("Found Environment offset in OOB..\n");
419 set_default_env("!no env offset in OOB");
424 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
426 set_default_env("!readenv() failed");
431 #endif /* ! ENV_IS_EMBEDDED */
433 #endif /* CONFIG_ENV_OFFSET_REDUND */