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 int nand_legacy_rw (struct nand_chip* nand, int cmd,
61 size_t start, size_t len,
62 size_t * retlen, u_char * buf);
64 /* references to names in env_common.c */
65 extern uchar default_environment[];
66 extern int default_environment_size;
68 char * env_name_spec = "NAND";
71 #ifdef ENV_IS_EMBEDDED
72 extern uchar environment[];
73 env_t *env_ptr = (env_t *)(&environment[0]);
74 #else /* ! ENV_IS_EMBEDDED */
76 #endif /* ENV_IS_EMBEDDED */
80 #if !defined(ENV_IS_EMBEDDED)
81 static void use_default(void);
84 DECLARE_GLOBAL_DATA_PTR;
86 uchar env_get_char_spec (int index)
88 return ( *((uchar *)(gd->env_addr + index)) );
92 /* this is called before nand_init()
93 * so we can't read Nand to validate env data.
94 * Mark it OK for now. env_relocate() in env_common.c
95 * will call our relocate function which does the real
98 * When using a NAND boot image (like sequoia_nand), the environment
99 * can be embedded or attached to the U-Boot image in NAND flash. This way
100 * the SPL loads not only the U-Boot image from NAND but also the
105 #if defined(ENV_IS_EMBEDDED)
107 int crc1_ok = 0, crc2_ok = 0;
108 env_t *tmp_env1, *tmp_env2;
110 total = CONFIG_ENV_SIZE;
113 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
115 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
116 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
118 if (!crc1_ok && !crc2_ok)
120 else if(crc1_ok && !crc2_ok)
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 == 1)
140 else if (gd->env_valid == 2)
142 #else /* ENV_IS_EMBEDDED */
143 gd->env_addr = (ulong)&default_environment[0];
145 #endif /* ENV_IS_EMBEDDED */
152 * The legacy NAND code saved the environment in the first NAND device i.e.,
153 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
155 int writeenv(size_t offset, u_char *buf)
157 size_t end = offset + CONFIG_ENV_RANGE;
158 size_t amount_saved = 0;
159 size_t blocksize, len;
163 blocksize = nand_info[0].erasesize;
164 len = min(blocksize, CONFIG_ENV_SIZE);
166 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
167 if (nand_block_isbad(&nand_info[0], offset)) {
170 char_ptr = &buf[amount_saved];
171 if (nand_write(&nand_info[0], offset, &len,
178 if (amount_saved != CONFIG_ENV_SIZE)
183 #ifdef CONFIG_ENV_OFFSET_REDUND
188 nand_erase_options_t nand_erase_options;
191 total = CONFIG_ENV_SIZE;
193 nand_erase_options.length = CONFIG_ENV_RANGE;
194 nand_erase_options.quiet = 0;
195 nand_erase_options.jffs2 = 0;
196 nand_erase_options.scrub = 0;
198 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
200 if(gd->env_valid == 1) {
201 puts ("Erasing redundant Nand...\n");
202 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
203 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
206 puts ("Writing to redundant Nand... ");
207 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
209 puts ("Erasing Nand...\n");
210 nand_erase_options.offset = CONFIG_ENV_OFFSET;
211 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
214 puts ("Writing to Nand... ");
215 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
223 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
226 #else /* ! CONFIG_ENV_OFFSET_REDUND */
231 nand_erase_options_t nand_erase_options;
233 nand_erase_options.length = CONFIG_ENV_RANGE;
234 nand_erase_options.quiet = 0;
235 nand_erase_options.jffs2 = 0;
236 nand_erase_options.scrub = 0;
237 nand_erase_options.offset = CONFIG_ENV_OFFSET;
239 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
241 puts ("Erasing Nand...\n");
242 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
245 puts ("Writing to Nand... ");
246 total = CONFIG_ENV_SIZE;
247 if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
255 #endif /* CONFIG_ENV_OFFSET_REDUND */
256 #endif /* CMD_SAVEENV */
258 int readenv (size_t offset, u_char * buf)
260 size_t end = offset + CONFIG_ENV_RANGE;
261 size_t amount_loaded = 0;
262 size_t blocksize, len;
266 blocksize = nand_info[0].erasesize;
267 len = min(blocksize, CONFIG_ENV_SIZE);
269 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
270 if (nand_block_isbad(&nand_info[0], offset)) {
273 char_ptr = &buf[amount_loaded];
274 if (nand_read(&nand_info[0], offset, &len, char_ptr))
277 amount_loaded += len;
280 if (amount_loaded != CONFIG_ENV_SIZE)
286 #ifdef CONFIG_ENV_OFFSET_REDUND
287 void env_relocate_spec (void)
289 #if !defined(ENV_IS_EMBEDDED)
291 int crc1_ok = 0, crc2_ok = 0;
292 env_t *tmp_env1, *tmp_env2;
294 total = CONFIG_ENV_SIZE;
296 tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
297 tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
299 if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
300 puts("No Valid Environment Area Found\n");
301 if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
302 puts("No Valid Reundant Environment Area Found\n");
304 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
305 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
307 if(!crc1_ok && !crc2_ok) {
310 return use_default();
311 } else if(crc1_ok && !crc2_ok)
313 else if(!crc1_ok && crc2_ok)
316 /* both ok - check serial */
317 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
319 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
321 else if(tmp_env1->flags > tmp_env2->flags)
323 else if(tmp_env2->flags > tmp_env1->flags)
325 else /* flags are equal - almost impossible */
331 if(gd->env_valid == 1) {
339 #endif /* ! ENV_IS_EMBEDDED */
341 #else /* ! CONFIG_ENV_OFFSET_REDUND */
343 * The legacy NAND code saved the environment in the first NAND device i.e.,
344 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
346 void env_relocate_spec (void)
348 #if !defined(ENV_IS_EMBEDDED)
351 ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
353 return use_default();
355 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
356 return use_default();
357 #endif /* ! ENV_IS_EMBEDDED */
359 #endif /* CONFIG_ENV_OFFSET_REDUND */
361 #if !defined(ENV_IS_EMBEDDED)
362 static void use_default()
364 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");