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
52 #ifndef CONFIG_ENV_RANGE
53 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
56 /* references to names in env_common.c */
57 extern uchar default_environment[];
59 char * env_name_spec = "NAND";
62 #if defined(ENV_IS_EMBEDDED)
63 extern uchar environment[];
64 env_t *env_ptr = (env_t *)(&environment[0]);
65 #elif defined(CONFIG_NAND_ENV_DST)
66 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
67 #else /* ! ENV_IS_EMBEDDED */
69 #endif /* ENV_IS_EMBEDDED */
73 #if !defined(ENV_IS_EMBEDDED)
74 static void use_default(void);
77 DECLARE_GLOBAL_DATA_PTR;
79 uchar env_get_char_spec (int index)
81 return ( *((uchar *)(gd->env_addr + index)) );
85 /* this is called before nand_init()
86 * so we can't read Nand to validate env data.
87 * Mark it OK for now. env_relocate() in env_common.c
88 * will call our relocate function which does the real
91 * When using a NAND boot image (like sequoia_nand), the environment
92 * can be embedded or attached to the U-Boot image in NAND flash. This way
93 * the SPL loads not only the U-Boot image from NAND but also the
98 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
99 int crc1_ok = 0, crc2_ok = 0;
102 #ifdef CONFIG_ENV_OFFSET_REDUND
105 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
106 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
111 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
113 if (!crc1_ok && !crc2_ok) {
118 } else if (crc1_ok && !crc2_ok) {
121 #ifdef CONFIG_ENV_OFFSET_REDUND
122 else if (!crc1_ok && crc2_ok) {
125 /* both ok - check serial */
126 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
128 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
130 else if(tmp_env1->flags > tmp_env2->flags)
132 else if(tmp_env2->flags > tmp_env1->flags)
134 else /* flags are equal - almost impossible */
138 if (gd->env_valid == 2)
142 if (gd->env_valid == 1)
145 gd->env_addr = (ulong)env_ptr->data;
147 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
148 gd->env_addr = (ulong)&default_environment[0];
150 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
157 * The legacy NAND code saved the environment in the first NAND device i.e.,
158 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
160 int writeenv(size_t offset, u_char *buf)
162 size_t end = offset + CONFIG_ENV_RANGE;
163 size_t amount_saved = 0;
164 size_t blocksize, len;
168 blocksize = nand_info[0].erasesize;
169 len = min(blocksize, CONFIG_ENV_SIZE);
171 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
172 if (nand_block_isbad(&nand_info[0], offset)) {
175 char_ptr = &buf[amount_saved];
176 if (nand_write(&nand_info[0], offset, &len,
183 if (amount_saved != CONFIG_ENV_SIZE)
188 #ifdef CONFIG_ENV_OFFSET_REDUND
192 nand_erase_options_t nand_erase_options;
196 nand_erase_options.length = CONFIG_ENV_RANGE;
197 nand_erase_options.quiet = 0;
198 nand_erase_options.jffs2 = 0;
199 nand_erase_options.scrub = 0;
201 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
203 if(gd->env_valid == 1) {
204 puts ("Erasing redundant Nand...\n");
205 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
206 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
209 puts ("Writing to redundant Nand... ");
210 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
212 puts ("Erasing Nand...\n");
213 nand_erase_options.offset = CONFIG_ENV_OFFSET;
214 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
217 puts ("Writing to Nand... ");
218 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
226 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
229 #else /* ! CONFIG_ENV_OFFSET_REDUND */
233 nand_erase_options_t nand_erase_options;
235 nand_erase_options.length = CONFIG_ENV_RANGE;
236 nand_erase_options.quiet = 0;
237 nand_erase_options.jffs2 = 0;
238 nand_erase_options.scrub = 0;
239 nand_erase_options.offset = CONFIG_ENV_OFFSET;
241 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
243 puts ("Erasing Nand...\n");
244 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
247 puts ("Writing to Nand... ");
248 if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
256 #endif /* CONFIG_ENV_OFFSET_REDUND */
257 #endif /* CMD_SAVEENV */
259 int readenv (size_t offset, u_char * buf)
261 size_t end = offset + CONFIG_ENV_RANGE;
262 size_t amount_loaded = 0;
263 size_t blocksize, len;
267 blocksize = nand_info[0].erasesize;
268 len = min(blocksize, CONFIG_ENV_SIZE);
270 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
271 if (nand_block_isbad(&nand_info[0], offset)) {
274 char_ptr = &buf[amount_loaded];
275 if (nand_read(&nand_info[0], offset, &len, char_ptr))
278 amount_loaded += len;
281 if (amount_loaded != CONFIG_ENV_SIZE)
287 #ifdef CONFIG_ENV_OFFSET_REDUND
288 void env_relocate_spec (void)
290 #if !defined(ENV_IS_EMBEDDED)
291 int crc1_ok = 0, crc2_ok = 0;
292 env_t *tmp_env1, *tmp_env2;
294 tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
295 tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
297 if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
298 puts("Can't allocate buffers for environment\n");
301 return use_default();
304 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
305 puts("No Valid Environment Area Found\n");
306 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
307 puts("No Valid Reundant Environment Area Found\n");
309 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
310 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
312 if(!crc1_ok && !crc2_ok) {
315 return use_default();
316 } else if(crc1_ok && !crc2_ok)
318 else if(!crc1_ok && crc2_ok)
321 /* both ok - check serial */
322 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
324 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
326 else if(tmp_env1->flags > tmp_env2->flags)
328 else if(tmp_env2->flags > tmp_env1->flags)
330 else /* flags are equal - almost impossible */
336 if(gd->env_valid == 1) {
344 #endif /* ! ENV_IS_EMBEDDED */
346 #else /* ! CONFIG_ENV_OFFSET_REDUND */
348 * The legacy NAND code saved the environment in the first NAND device i.e.,
349 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
351 void env_relocate_spec (void)
353 #if !defined(ENV_IS_EMBEDDED)
356 ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
358 return use_default();
360 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
361 return use_default();
362 #endif /* ! ENV_IS_EMBEDDED */
364 #endif /* CONFIG_ENV_OFFSET_REDUND */
366 #if !defined(ENV_IS_EMBEDDED)
367 static void use_default()
369 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");