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 #if defined(CONFIG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
40 #include <environment.h>
41 #include <linux/stddef.h>
45 #if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)
47 #elif defined(CFG_ENV_OFFSET_REDUND)
48 #error Cannot use CFG_ENV_OFFSET_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_NAND
51 #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE)
52 #error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE
56 #error CONFIG_INFERNO not supported yet
60 #define CFG_ENV_RANGE CFG_ENV_SIZE
63 int nand_legacy_rw (struct nand_chip* nand, int cmd,
64 size_t start, size_t len,
65 size_t * retlen, u_char * buf);
67 /* references to names in env_common.c */
68 extern uchar default_environment[];
69 extern int default_environment_size;
71 char * env_name_spec = "NAND";
74 #ifdef ENV_IS_EMBEDDED
75 extern uchar environment[];
76 env_t *env_ptr = (env_t *)(&environment[0]);
77 #else /* ! ENV_IS_EMBEDDED */
79 #endif /* ENV_IS_EMBEDDED */
83 #if !defined(ENV_IS_EMBEDDED)
84 static void use_default(void);
87 DECLARE_GLOBAL_DATA_PTR;
89 uchar env_get_char_spec (int index)
91 return ( *((uchar *)(gd->env_addr + index)) );
95 /* this is called before nand_init()
96 * so we can't read Nand to validate env data.
97 * Mark it OK for now. env_relocate() in env_common.c
98 * will call our relocate function which does the real
101 * When using a NAND boot image (like sequoia_nand), the environment
102 * can be embedded or attached to the U-Boot image in NAND flash. This way
103 * the SPL loads not only the U-Boot image from NAND but also the
108 #if defined(ENV_IS_EMBEDDED)
110 int crc1_ok = 0, crc2_ok = 0;
111 env_t *tmp_env1, *tmp_env2;
113 total = CFG_ENV_SIZE;
116 tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
118 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
119 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
121 if (!crc1_ok && !crc2_ok)
123 else if(crc1_ok && !crc2_ok)
125 else if(!crc1_ok && crc2_ok)
128 /* both ok - check serial */
129 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
131 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
133 else if(tmp_env1->flags > tmp_env2->flags)
135 else if(tmp_env2->flags > tmp_env1->flags)
137 else /* flags are equal - almost impossible */
141 if (gd->env_valid == 1)
143 else if (gd->env_valid == 2)
145 #else /* ENV_IS_EMBEDDED */
146 gd->env_addr = (ulong)&default_environment[0];
148 #endif /* ENV_IS_EMBEDDED */
155 * The legacy NAND code saved the environment in the first NAND device i.e.,
156 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
158 int writeenv(size_t offset, u_char *buf)
160 size_t end = offset + CFG_ENV_RANGE;
161 size_t amount_saved = 0;
162 size_t blocksize, len;
166 blocksize = nand_info[0].erasesize;
167 len = min(blocksize, CFG_ENV_SIZE);
169 while (amount_saved < CFG_ENV_SIZE && offset < end) {
170 if (nand_block_isbad(&nand_info[0], offset)) {
173 char_ptr = &buf[amount_saved];
174 if (nand_write(&nand_info[0], offset, &len,
181 if (amount_saved != CFG_ENV_SIZE)
186 #ifdef CFG_ENV_OFFSET_REDUND
191 nand_erase_options_t nand_erase_options;
194 total = CFG_ENV_SIZE;
196 nand_erase_options.length = CFG_ENV_RANGE;
197 nand_erase_options.quiet = 0;
198 nand_erase_options.jffs2 = 0;
199 nand_erase_options.scrub = 0;
201 if (CFG_ENV_RANGE < CFG_ENV_SIZE)
203 if(gd->env_valid == 1) {
204 puts ("Erasing redundant Nand...\n");
205 nand_erase_options.offset = CFG_ENV_OFFSET_REDUND;
206 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
209 puts ("Writing to redundant Nand... ");
210 ret = writeenv(CFG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
212 puts ("Erasing Nand...\n");
213 nand_erase_options.offset = CFG_ENV_OFFSET;
214 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
217 puts ("Writing to Nand... ");
218 ret = writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
226 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
229 #else /* ! CFG_ENV_OFFSET_REDUND */
234 nand_erase_options_t nand_erase_options;
236 nand_erase_options.length = CFG_ENV_RANGE;
237 nand_erase_options.quiet = 0;
238 nand_erase_options.jffs2 = 0;
239 nand_erase_options.scrub = 0;
240 nand_erase_options.offset = CFG_ENV_OFFSET;
242 if (CFG_ENV_RANGE < CFG_ENV_SIZE)
244 puts ("Erasing Nand...\n");
245 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
248 puts ("Writing to Nand... ");
249 total = CFG_ENV_SIZE;
250 if (writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr)) {
258 #endif /* CFG_ENV_OFFSET_REDUND */
259 #endif /* CMD_SAVEENV */
261 int readenv (size_t offset, u_char * buf)
263 size_t end = offset + CFG_ENV_RANGE;
264 size_t amount_loaded = 0;
265 size_t blocksize, len;
269 blocksize = nand_info[0].erasesize;
270 len = min(blocksize, CFG_ENV_SIZE);
272 while (amount_loaded < CFG_ENV_SIZE && offset < end) {
273 if (nand_block_isbad(&nand_info[0], offset)) {
276 char_ptr = &buf[amount_loaded];
277 if (nand_read(&nand_info[0], offset, &len, char_ptr))
280 amount_loaded += len;
283 if (amount_loaded != CFG_ENV_SIZE)
289 #ifdef CFG_ENV_OFFSET_REDUND
290 void env_relocate_spec (void)
292 #if !defined(ENV_IS_EMBEDDED)
294 int crc1_ok = 0, crc2_ok = 0;
295 env_t *tmp_env1, *tmp_env2;
297 total = CFG_ENV_SIZE;
299 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
300 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
302 if (readenv(CFG_ENV_OFFSET, (u_char *) tmp_env1))
303 puts("No Valid Environment Area Found\n");
304 if (readenv(CFG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
305 puts("No Valid Reundant Environment Area Found\n");
307 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
308 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
310 if(!crc1_ok && !crc2_ok)
311 return use_default();
312 else if(crc1_ok && !crc2_ok)
314 else if(!crc1_ok && crc2_ok)
317 /* both ok - check serial */
318 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
320 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
322 else if(tmp_env1->flags > tmp_env2->flags)
324 else if(tmp_env2->flags > tmp_env1->flags)
326 else /* flags are equal - almost impossible */
332 if(gd->env_valid == 1) {
340 #endif /* ! ENV_IS_EMBEDDED */
342 #else /* ! CFG_ENV_OFFSET_REDUND */
344 * The legacy NAND code saved the environment in the first NAND device i.e.,
345 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
347 void env_relocate_spec (void)
349 #if !defined(ENV_IS_EMBEDDED)
352 ret = readenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
354 return use_default();
356 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
357 return use_default();
358 #endif /* ! ENV_IS_EMBEDDED */
360 #endif /* CFG_ENV_OFFSET_REDUND */
362 #if !defined(ENV_IS_EMBEDDED)
363 static void use_default()
365 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
370 #endif /* CONFIG_ENV_IS_IN_NAND */