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 CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
50 #if defined(CONFIG_ENV_SIZE_REDUND) && \
51 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
52 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
55 #ifndef CONFIG_ENV_RANGE
56 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
59 char *env_name_spec = "NAND";
61 #if defined(ENV_IS_EMBEDDED)
62 env_t *env_ptr = &environment;
63 #elif defined(CONFIG_NAND_ENV_DST)
64 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
65 #else /* ! ENV_IS_EMBEDDED */
67 #endif /* ENV_IS_EMBEDDED */
69 DECLARE_GLOBAL_DATA_PTR;
71 uchar env_get_char_spec(int index)
73 return *((uchar *)(gd->env_addr + index));
77 * This is called before nand_init() so we can't read NAND to
80 * Mark it OK for now. env_relocate() in env_common.c will call our
81 * relocate function which does the real validation.
83 * When using a NAND boot image (like sequoia_nand), the environment
84 * can be embedded or attached to the U-Boot image in NAND flash.
85 * This way the SPL loads not only the U-Boot image from NAND but
86 * also the environment.
90 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
91 int crc1_ok = 0, crc2_ok = 0;
94 #ifdef CONFIG_ENV_OFFSET_REDUND
97 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
98 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
101 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
103 if (!crc1_ok && !crc2_ok) {
108 } else if (crc1_ok && !crc2_ok) {
111 #ifdef CONFIG_ENV_OFFSET_REDUND
112 else if (!crc1_ok && crc2_ok) {
115 /* both ok - check serial */
116 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
118 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
120 else if (tmp_env1->flags > tmp_env2->flags)
122 else if (tmp_env2->flags > tmp_env1->flags)
124 else /* flags are equal - almost impossible */
128 if (gd->env_valid == 2)
132 if (gd->env_valid == 1)
135 gd->env_addr = (ulong)env_ptr->data;
137 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
138 gd->env_addr = (ulong)&default_environment[0];
140 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
147 * The legacy NAND code saved the environment in the first NAND device i.e.,
148 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
150 int writeenv(size_t offset, u_char *buf)
152 size_t end = offset + CONFIG_ENV_RANGE;
153 size_t amount_saved = 0;
154 size_t blocksize, len;
157 blocksize = nand_info[0].erasesize;
158 len = min(blocksize, CONFIG_ENV_SIZE);
160 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
161 if (nand_block_isbad(&nand_info[0], offset)) {
164 char_ptr = &buf[amount_saved];
165 if (nand_write(&nand_info[0], offset, &len, char_ptr))
172 if (amount_saved != CONFIG_ENV_SIZE)
178 #ifdef CONFIG_ENV_OFFSET_REDUND
179 static unsigned char env_flags;
187 nand_erase_options_t nand_erase_options;
189 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
190 nand_erase_options.length = CONFIG_ENV_RANGE;
192 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
195 res = (char *)&env_new.data;
196 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
198 error("Cannot export environment: errno = %d\n", errno);
201 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
202 env_new.flags = ++env_flags; /* increase the serial */
204 if (gd->env_valid == 1) {
205 puts("Erasing redundant NAND...\n");
206 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
207 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
210 puts("Writing to redundant NAND... ");
211 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
213 puts("Erasing NAND...\n");
214 nand_erase_options.offset = CONFIG_ENV_OFFSET;
215 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
218 puts("Writing to NAND... ");
219 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
228 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
232 #else /* ! CONFIG_ENV_OFFSET_REDUND */
239 nand_erase_options_t nand_erase_options;
241 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
242 nand_erase_options.length = CONFIG_ENV_RANGE;
243 nand_erase_options.offset = CONFIG_ENV_OFFSET;
245 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
248 res = (char *)&env_new.data;
249 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
251 error("Cannot export environment: errno = %d\n", errno);
254 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
256 puts("Erasing Nand...\n");
257 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
260 puts("Writing to Nand... ");
261 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
269 #endif /* CONFIG_ENV_OFFSET_REDUND */
270 #endif /* CMD_SAVEENV */
272 int readenv(size_t offset, u_char *buf)
274 size_t end = offset + CONFIG_ENV_RANGE;
275 size_t amount_loaded = 0;
276 size_t blocksize, len;
279 blocksize = nand_info[0].erasesize;
283 len = min(blocksize, CONFIG_ENV_SIZE);
285 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
286 if (nand_block_isbad(&nand_info[0], offset)) {
289 char_ptr = &buf[amount_loaded];
290 if (nand_read_skip_bad(&nand_info[0], offset,
295 amount_loaded += len;
299 if (amount_loaded != CONFIG_ENV_SIZE)
305 #ifdef CONFIG_ENV_OFFSET_OOB
306 int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
308 struct mtd_oob_ops ops;
309 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
313 ops.mode = MTD_OOB_AUTO;
315 ops.ooblen = ENV_OFFSET_SIZE;
316 ops.oobbuf = (void *)oob_buf;
318 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
320 printf("error reading OOB block 0\n");
324 if (oob_buf[0] == ENV_OOB_MARKER) {
325 *result = oob_buf[1] * nand->erasesize;
326 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
327 *result = oob_buf[1];
329 printf("No dynamic environment marker in OOB block 0\n");
337 #ifdef CONFIG_ENV_OFFSET_REDUND
338 void env_relocate_spec(void)
340 #if !defined(ENV_IS_EMBEDDED)
341 int crc1_ok = 0, crc2_ok = 0;
342 env_t *ep, *tmp_env1, *tmp_env2;
344 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
345 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
346 if (tmp_env1 == NULL || tmp_env2 == NULL) {
347 puts("Can't allocate buffers for environment\n");
348 set_default_env("!malloc() failed");
352 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
353 puts("No Valid Environment Area found\n");
355 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
356 puts("No Valid Redundant Environment Area found\n");
358 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
359 crc2_ok = 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 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 */