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[];
62 extern int default_environment_size;
64 char * env_name_spec = "NAND";
67 #if defined(ENV_IS_EMBEDDED)
68 extern uchar environment[];
69 env_t *env_ptr = (env_t *)(&environment[0]);
70 #elif defined(CONFIG_NAND_ENV_DST)
71 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
72 #else /* ! ENV_IS_EMBEDDED */
74 #endif /* ENV_IS_EMBEDDED */
78 #if !defined(ENV_IS_EMBEDDED)
79 static void use_default(void);
82 DECLARE_GLOBAL_DATA_PTR;
84 uchar env_get_char_spec (int index)
86 return ( *((uchar *)(gd->env_addr + index)) );
90 /* this is called before nand_init()
91 * so we can't read Nand to validate env data.
92 * Mark it OK for now. env_relocate() in env_common.c
93 * will call our relocate function which does the real
96 * When using a NAND boot image (like sequoia_nand), the environment
97 * can be embedded or attached to the U-Boot image in NAND flash. This way
98 * the SPL loads not only the U-Boot image from NAND but also the
103 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
104 int crc1_ok = 0, crc2_ok = 0;
107 #ifdef CONFIG_ENV_OFFSET_REDUND
110 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
111 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
116 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
118 if (!crc1_ok && !crc2_ok) {
123 } else if (crc1_ok && !crc2_ok) {
126 #ifdef CONFIG_ENV_OFFSET_REDUND
127 else if (!crc1_ok && crc2_ok) {
130 /* both ok - check serial */
131 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
133 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
135 else if(tmp_env1->flags > tmp_env2->flags)
137 else if(tmp_env2->flags > tmp_env1->flags)
139 else /* flags are equal - almost impossible */
143 if (gd->env_valid == 2)
147 if (gd->env_valid == 1)
150 gd->env_addr = (ulong)env_ptr->data;
152 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
153 gd->env_addr = (ulong)&default_environment[0];
155 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
162 * The legacy NAND code saved the environment in the first NAND device i.e.,
163 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
165 int writeenv(size_t offset, u_char *buf)
167 size_t end = offset + CONFIG_ENV_RANGE;
168 size_t amount_saved = 0;
169 size_t blocksize, len;
173 blocksize = nand_info[0].erasesize;
174 len = min(blocksize, CONFIG_ENV_SIZE);
176 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
177 if (nand_block_isbad(&nand_info[0], offset)) {
180 char_ptr = &buf[amount_saved];
181 if (nand_write(&nand_info[0], offset, &len,
188 if (amount_saved != CONFIG_ENV_SIZE)
193 #ifdef CONFIG_ENV_OFFSET_REDUND
197 nand_erase_options_t nand_erase_options;
201 nand_erase_options.length = CONFIG_ENV_RANGE;
202 nand_erase_options.quiet = 0;
203 nand_erase_options.jffs2 = 0;
204 nand_erase_options.scrub = 0;
206 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
208 if(gd->env_valid == 1) {
209 puts ("Erasing redundant Nand...\n");
210 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
211 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
214 puts ("Writing to redundant Nand... ");
215 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
217 puts ("Erasing Nand...\n");
218 nand_erase_options.offset = CONFIG_ENV_OFFSET;
219 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
222 puts ("Writing to Nand... ");
223 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
231 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
234 #else /* ! CONFIG_ENV_OFFSET_REDUND */
238 nand_erase_options_t nand_erase_options;
240 nand_erase_options.length = CONFIG_ENV_RANGE;
241 nand_erase_options.quiet = 0;
242 nand_erase_options.jffs2 = 0;
243 nand_erase_options.scrub = 0;
244 nand_erase_options.offset = CONFIG_ENV_OFFSET;
246 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
248 puts ("Erasing Nand...\n");
249 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
252 puts ("Writing to Nand... ");
253 if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
261 #endif /* CONFIG_ENV_OFFSET_REDUND */
262 #endif /* CMD_SAVEENV */
264 int readenv (size_t offset, u_char * buf)
266 size_t end = offset + CONFIG_ENV_RANGE;
267 size_t amount_loaded = 0;
268 size_t blocksize, len;
272 blocksize = nand_info[0].erasesize;
273 len = min(blocksize, CONFIG_ENV_SIZE);
275 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
276 if (nand_block_isbad(&nand_info[0], offset)) {
279 char_ptr = &buf[amount_loaded];
280 if (nand_read(&nand_info[0], offset, &len, char_ptr))
283 amount_loaded += len;
286 if (amount_loaded != CONFIG_ENV_SIZE)
292 #ifdef CONFIG_ENV_OFFSET_REDUND
293 void env_relocate_spec (void)
295 #if !defined(ENV_IS_EMBEDDED)
296 int crc1_ok = 0, crc2_ok = 0;
297 env_t *tmp_env1, *tmp_env2;
299 tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
300 tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
302 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
303 puts("No Valid Environment Area Found\n");
304 if (readenv(CONFIG_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) {
313 return use_default();
314 } else if(crc1_ok && !crc2_ok)
316 else if(!crc1_ok && crc2_ok)
319 /* both ok - check serial */
320 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
322 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
324 else if(tmp_env1->flags > tmp_env2->flags)
326 else if(tmp_env2->flags > tmp_env1->flags)
328 else /* flags are equal - almost impossible */
334 if(gd->env_valid == 1) {
342 #endif /* ! ENV_IS_EMBEDDED */
344 #else /* ! CONFIG_ENV_OFFSET_REDUND */
346 * The legacy NAND code saved the environment in the first NAND device i.e.,
347 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
349 void env_relocate_spec (void)
351 #if !defined(ENV_IS_EMBEDDED)
354 ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
356 return use_default();
358 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
359 return use_default();
360 #endif /* ! ENV_IS_EMBEDDED */
362 #endif /* CONFIG_ENV_OFFSET_REDUND */
364 #if !defined(ENV_IS_EMBEDDED)
365 static void use_default()
367 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");