3 * Jian Zhang, Texas Instruments, jzhang@ti.com.
5 * (C) Copyright 2000-2006
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Andreas Heppel <aheppel@sysgo.de>
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
37 #include <environment.h>
38 #include <linux/stddef.h>
42 #if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)
44 #elif defined(CFG_ENV_OFFSET_REDUND)
45 #error Cannot use CFG_ENV_OFFSET_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_NAND
48 #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE)
49 #error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE
53 #error CONFIG_INFERNO not supported yet
56 int nand_legacy_rw (struct nand_chip* nand, int cmd,
57 size_t start, size_t len,
58 size_t * retlen, u_char * buf);
60 /* references to names in env_common.c */
61 extern uchar default_environment[];
62 extern int default_environment_size;
64 char * env_name_spec = "NAND";
67 #ifdef ENV_IS_EMBEDDED
68 extern uchar environment[];
69 env_t *env_ptr = (env_t *)(&environment[0]);
70 #else /* ! ENV_IS_EMBEDDED */
72 #endif /* ENV_IS_EMBEDDED */
76 #if !defined(ENV_IS_EMBEDDED)
77 static void use_default(void);
80 DECLARE_GLOBAL_DATA_PTR;
82 uchar env_get_char_spec (int index)
84 return ( *((uchar *)(gd->env_addr + index)) );
88 /* this is called before nand_init()
89 * so we can't read Nand to validate env data.
90 * Mark it OK for now. env_relocate() in env_common.c
91 * will call our relocate function which will does
92 * the real validation.
94 * When using a NAND boot image (like sequoia_nand), the environment
95 * can be embedded or attached to the U-Boot image in NAND flash. This way
96 * the SPL loads not only the U-Boot image from NAND but also the
101 #if defined(ENV_IS_EMBEDDED)
103 int crc1_ok = 0, crc2_ok = 0;
104 env_t *tmp_env1, *tmp_env2;
106 total = CFG_ENV_SIZE;
109 tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
111 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
112 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
114 if (!crc1_ok && !crc2_ok)
116 else if(crc1_ok && !crc2_ok)
118 else if(!crc1_ok && crc2_ok)
121 /* both ok - check serial */
122 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
124 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
126 else if(tmp_env1->flags > tmp_env2->flags)
128 else if(tmp_env2->flags > tmp_env1->flags)
130 else /* flags are equal - almost impossible */
134 if (gd->env_valid == 1)
136 else if (gd->env_valid == 2)
138 #else /* ENV_IS_EMBEDDED */
139 gd->env_addr = (ulong)&default_environment[0];
141 #endif /* ENV_IS_EMBEDDED */
148 * The legacy NAND code saved the environment in the first NAND device i.e.,
149 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
151 #ifdef CFG_ENV_OFFSET_REDUND
158 total = CFG_ENV_SIZE;
160 if(gd->env_valid == 1) {
161 puts ("Erasing redundant Nand...");
162 if (nand_erase(&nand_info[0],
163 CFG_ENV_OFFSET_REDUND, CFG_ENV_SIZE))
165 puts ("Writing to redundant Nand... ");
166 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
169 puts ("Erasing Nand...");
170 if (nand_erase(&nand_info[0],
171 CFG_ENV_OFFSET, CFG_ENV_SIZE))
174 puts ("Writing to Nand... ");
175 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
178 if (ret || total != CFG_ENV_SIZE)
182 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
185 #else /* ! CFG_ENV_OFFSET_REDUND */
191 puts ("Erasing Nand...");
192 if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
195 puts ("Writing to Nand... ");
196 total = CFG_ENV_SIZE;
197 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
198 if (ret || total != CFG_ENV_SIZE)
204 #endif /* CFG_ENV_OFFSET_REDUND */
205 #endif /* CMD_SAVEENV */
207 #ifdef CFG_ENV_OFFSET_REDUND
208 void env_relocate_spec (void)
210 #if !defined(ENV_IS_EMBEDDED)
212 int crc1_ok = 0, crc2_ok = 0;
213 env_t *tmp_env1, *tmp_env2;
215 total = CFG_ENV_SIZE;
217 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
218 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
220 nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
222 nand_read(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
225 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
226 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
228 if(!crc1_ok && !crc2_ok)
229 return use_default();
230 else if(crc1_ok && !crc2_ok)
232 else if(!crc1_ok && crc2_ok)
235 /* both ok - check serial */
236 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
238 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
240 else if(tmp_env1->flags > tmp_env2->flags)
242 else if(tmp_env2->flags > tmp_env1->flags)
244 else /* flags are equal - almost impossible */
250 if(gd->env_valid == 1) {
258 #endif /* ! ENV_IS_EMBEDDED */
260 #else /* ! CFG_ENV_OFFSET_REDUND */
262 * The legacy NAND code saved the environment in the first NAND device i.e.,
263 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
265 void env_relocate_spec (void)
267 #if !defined(ENV_IS_EMBEDDED)
271 total = CFG_ENV_SIZE;
272 ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
273 if (ret || total != CFG_ENV_SIZE)
274 return use_default();
276 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
277 return use_default();
278 #endif /* ! ENV_IS_EMBEDDED */
280 #endif /* CFG_ENV_OFFSET_REDUND */
282 #if !defined(ENV_IS_EMBEDDED)
283 static void use_default()
285 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
287 if (default_environment_size > CFG_ENV_SIZE){
288 puts ("*** Error - default environment is too large\n\n");
292 memset (env_ptr, 0, sizeof(env_t));
293 memcpy (env_ptr->data,
295 default_environment_size);
296 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
302 #endif /* CFG_ENV_IS_IN_NAND */