3 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
6 * Jian Zhang, Texas Instruments, jzhang@ti.com.
8 * (C) Copyright 2000-2006
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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>
42 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
44 #elif defined(CONFIG_ENV_OFFSET_REDUND)
45 #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
48 #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
49 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
53 #error CONFIG_INFERNO not supported yet
56 #ifndef CONFIG_ENV_RANGE
57 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
60 /* references to names in env_common.c */
61 extern uchar default_environment[];
63 char * env_name_spec = "NAND";
66 #if defined(ENV_IS_EMBEDDED)
67 extern uchar environment[];
68 env_t *env_ptr = (env_t *)(&environment[0]);
69 #elif defined(CONFIG_NAND_ENV_DST)
70 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
71 #else /* ! ENV_IS_EMBEDDED */
73 #endif /* ENV_IS_EMBEDDED */
77 #if !defined(ENV_IS_EMBEDDED)
78 static void use_default(void);
81 DECLARE_GLOBAL_DATA_PTR;
83 uchar env_get_char_spec (int index)
85 return ( *((uchar *)(gd->env_addr + index)) );
89 /* this is called before nand_init()
90 * so we can't read Nand to validate env data.
91 * Mark it OK for now. env_relocate() in env_common.c
92 * will call our relocate function which does the real
95 * When using a NAND boot image (like sequoia_nand), the environment
96 * can be embedded or attached to the U-Boot image in NAND flash. This way
97 * the SPL loads not only the U-Boot image from NAND but also the
102 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
103 int crc1_ok = 0, crc2_ok = 0;
106 #ifdef CONFIG_ENV_OFFSET_REDUND
109 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
110 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
115 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
117 if (!crc1_ok && !crc2_ok) {
122 } else if (crc1_ok && !crc2_ok) {
125 #ifdef CONFIG_ENV_OFFSET_REDUND
126 else if (!crc1_ok && crc2_ok) {
129 /* both ok - check serial */
130 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
132 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
134 else if(tmp_env1->flags > tmp_env2->flags)
136 else if(tmp_env2->flags > tmp_env1->flags)
138 else /* flags are equal - almost impossible */
142 if (gd->env_valid == 2)
146 if (gd->env_valid == 1)
149 gd->env_addr = (ulong)env_ptr->data;
151 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
152 gd->env_addr = (ulong)&default_environment[0];
154 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
161 * The legacy NAND code saved the environment in the first NAND device i.e.,
162 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
164 int writeenv(size_t offset, u_char *buf)
166 size_t end = offset + CONFIG_ENV_RANGE;
167 size_t amount_saved = 0;
168 size_t blocksize, len;
172 blocksize = nand_info[0].erasesize;
173 len = min(blocksize, CONFIG_ENV_SIZE);
175 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
176 if (nand_block_isbad(&nand_info[0], offset)) {
179 char_ptr = &buf[amount_saved];
180 if (nand_write(&nand_info[0], offset, &len,
187 if (amount_saved != CONFIG_ENV_SIZE)
192 #ifdef CONFIG_ENV_OFFSET_REDUND
196 nand_erase_options_t nand_erase_options;
200 nand_erase_options.length = CONFIG_ENV_RANGE;
201 nand_erase_options.quiet = 0;
202 nand_erase_options.jffs2 = 0;
203 nand_erase_options.scrub = 0;
205 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
207 if(gd->env_valid == 1) {
208 puts ("Erasing redundant Nand...\n");
209 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
210 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
213 puts ("Writing to redundant Nand... ");
214 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
216 puts ("Erasing Nand...\n");
217 nand_erase_options.offset = CONFIG_ENV_OFFSET;
218 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
221 puts ("Writing to Nand... ");
222 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
230 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
233 #else /* ! CONFIG_ENV_OFFSET_REDUND */
237 nand_erase_options_t nand_erase_options;
239 nand_erase_options.length = CONFIG_ENV_RANGE;
240 nand_erase_options.quiet = 0;
241 nand_erase_options.jffs2 = 0;
242 nand_erase_options.scrub = 0;
243 nand_erase_options.offset = CONFIG_ENV_OFFSET;
245 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
247 puts ("Erasing Nand...\n");
248 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
251 puts ("Writing to Nand... ");
252 if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
260 #endif /* CONFIG_ENV_OFFSET_REDUND */
261 #endif /* CMD_SAVEENV */
263 int readenv (size_t offset, u_char * buf)
265 size_t end = offset + CONFIG_ENV_RANGE;
266 size_t amount_loaded = 0;
267 size_t blocksize, len;
271 blocksize = nand_info[0].erasesize;
272 len = min(blocksize, CONFIG_ENV_SIZE);
274 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
275 if (nand_block_isbad(&nand_info[0], offset)) {
278 char_ptr = &buf[amount_loaded];
279 if (nand_read(&nand_info[0], offset, &len, char_ptr))
282 amount_loaded += len;
285 if (amount_loaded != CONFIG_ENV_SIZE)
291 #ifdef CONFIG_ENV_OFFSET_REDUND
292 void env_relocate_spec (void)
294 #if !defined(ENV_IS_EMBEDDED)
295 int crc1_ok = 0, crc2_ok = 0;
296 env_t *tmp_env1, *tmp_env2;
298 tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
299 tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
301 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
302 puts("Can't allocate buffers for environment\n");
305 return use_default();
308 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
309 puts("No Valid Environment Area Found\n");
310 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
311 puts("No Valid Reundant Environment Area Found\n");
313 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
314 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
316 if(!crc1_ok && !crc2_ok) {
319 return use_default();
320 } else if(crc1_ok && !crc2_ok)
322 else if(!crc1_ok && crc2_ok)
325 /* both ok - check serial */
326 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
328 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
330 else if(tmp_env1->flags > tmp_env2->flags)
332 else if(tmp_env2->flags > tmp_env1->flags)
334 else /* flags are equal - almost impossible */
340 if(gd->env_valid == 1) {
348 #endif /* ! ENV_IS_EMBEDDED */
350 #else /* ! CONFIG_ENV_OFFSET_REDUND */
352 * The legacy NAND code saved the environment in the first NAND device i.e.,
353 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
355 void env_relocate_spec (void)
357 #if !defined(ENV_IS_EMBEDDED)
360 ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
362 return use_default();
364 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
365 return use_default();
366 #endif /* ! ENV_IS_EMBEDDED */
368 #endif /* CONFIG_ENV_OFFSET_REDUND */
370 #if !defined(ENV_IS_EMBEDDED)
371 static void use_default()
373 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");