to make u-boot work for fat32 filesystem
[jz_uboot.git] / common / env_nand.c
blobdd4fb911a61013b356b461e9e8067986ec265ad3
1 /*
2 * (C) Copyright 2004
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
12 * project.
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,
27 * MA 02111-1307 USA
30 /* #define DEBUG */
32 #include <common.h>
34 #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
36 #include <command.h>
37 #include <environment.h>
38 #include <linux/stddef.h>
39 #include <malloc.h>
40 #include <nand.h>
42 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
43 #define CMD_SAVEENV
44 #elif defined(CFG_ENV_OFFSET_REDUND)
45 #error Cannot use CFG_ENV_OFFSET_REDUND without CFG_CMD_ENV & CFG_CMD_NAND
46 #endif
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
50 #endif
52 #ifdef CONFIG_INFERNO
53 #error CONFIG_INFERNO not supported yet
54 #endif
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 /* info for NAND chips, defined in drivers/nand/nand.c */
61 extern nand_info_t nand_info[];
63 /* references to names in env_common.c */
64 extern uchar default_environment[];
65 extern int default_environment_size;
67 char * env_name_spec = "NAND";
69 #undef ENV_IS_EMBEDDED
70 #ifdef ENV_IS_EMBEDDED
71 extern uchar environment[];
72 env_t *env_ptr = (env_t *)(&environment[0]);
73 #else /* ! ENV_IS_EMBEDDED */
74 env_t *env_ptr = 0;
75 #endif /* ENV_IS_EMBEDDED */
78 /* local functions */
79 #if !defined(ENV_IS_EMBEDDED)
80 static void use_default(void);
81 #endif
83 DECLARE_GLOBAL_DATA_PTR;
85 uchar env_get_char_spec (int index)
87 return ( *((uchar *)(gd->env_addr + index)) );
91 /* this is called before nand_init()
92 * so we can't read Nand to validate env data.
93 * Mark it OK for now. env_relocate() in env_common.c
94 * will call our relocate function which will does
95 * the real validation.
97 * When using a NAND boot image (like sequoia_nand), the environment
98 * can be embedded or attached to the U-Boot image in NAND flash. This way
99 * the SPL loads not only the U-Boot image from NAND but also the
100 * environment.
102 int env_init(void)
104 #if (defined(ENV_IS_EMBEDDED) && (!defined(CONFIG_OMIT_ENV_NAND)))
105 ulong total;
106 int crc1_ok = 0, crc2_ok = 0;
107 env_t *tmp_env1, *tmp_env2;
109 total = CFG_ENV_SIZE;
111 tmp_env1 = env_ptr;
112 tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
114 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
115 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
117 if (!crc1_ok && !crc2_ok)
118 gd->env_valid = 0;
119 else if(crc1_ok && !crc2_ok)
120 gd->env_valid = 1;
121 else if(!crc1_ok && crc2_ok)
122 gd->env_valid = 2;
123 else {
124 /* both ok - check serial */
125 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
126 gd->env_valid = 2;
127 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
128 gd->env_valid = 1;
129 else if(tmp_env1->flags > tmp_env2->flags)
130 gd->env_valid = 1;
131 else if(tmp_env2->flags > tmp_env1->flags)
132 gd->env_valid = 2;
133 else /* flags are equal - almost impossible */
134 gd->env_valid = 1;
137 if (gd->env_valid == 1)
138 env_ptr = tmp_env1;
139 else if (gd->env_valid == 2)
140 env_ptr = tmp_env2;
141 #else /* ENV_IS_EMBEDDED */
142 printf("useing defalut \n");
143 gd->env_addr = (ulong)&default_environment[0];
144 gd->env_valid = 1;
145 #if defined(CONFIG_OMIT_ENV_NAND)
146 gd->env_valid = 0;
147 #endif
148 #endif /* ENV_IS_EMBEDDED */
150 return (0);
153 #ifdef CMD_SAVEENV
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 #ifdef CFG_ENV_OFFSET_REDUND
159 int saveenv(void)
161 ulong total;
162 int ret = 0;
164 env_ptr->flags++;
165 total = CFG_ENV_SIZE;
167 if(gd->env_valid == 1) {
168 puts ("Erasing redundant Nand...");
169 if (nand_erase(&nand_info[0],
170 CFG_ENV_OFFSET_REDUND, CFG_ENV_SIZE))
171 return 1;
172 puts ("Writing to redundant Nand... ");
173 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
174 (u_char*) env_ptr);
175 } else {
176 puts ("Erasing Nand...");
177 if (nand_erase(&nand_info[0],
178 CFG_ENV_OFFSET, CFG_ENV_SIZE))
179 return 1;
181 puts ("Writing to Nand... ");
182 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
183 (u_char*) env_ptr);
185 if (ret || total != CFG_ENV_SIZE)
186 return 1;
188 puts ("done\n");
189 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
190 return ret;
192 #else /* ! CFG_ENV_OFFSET_REDUND */
193 int saveenv(void)
195 ulong total;
196 int ret = 0;
198 puts ("Erasing Nand...");
199 if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
200 return 1;
202 puts ("Writing to Nand... ");
203 total = CFG_ENV_SIZE;
204 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
205 if (ret || total != CFG_ENV_SIZE)
206 return 1;
208 puts ("done\n");
209 return ret;
211 #endif /* CFG_ENV_OFFSET_REDUND */
212 #endif /* CMD_SAVEENV */
214 #ifdef CFG_ENV_OFFSET_REDUND
215 void env_relocate_spec (void)
217 #if !defined(ENV_IS_EMBEDDED)
218 ulong total;
219 int crc1_ok = 0, crc2_ok = 0;
220 env_t *tmp_env1, *tmp_env2;
222 total = CFG_ENV_SIZE;
224 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
225 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
227 nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
228 (u_char*) tmp_env1);
229 nand_read(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
230 (u_char*) tmp_env2);
232 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
233 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
235 if(!crc1_ok && !crc2_ok)
236 return use_default();
237 else if(crc1_ok && !crc2_ok)
238 gd->env_valid = 1;
239 else if(!crc1_ok && crc2_ok)
240 gd->env_valid = 2;
241 else {
242 /* both ok - check serial */
243 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
244 gd->env_valid = 2;
245 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
246 gd->env_valid = 1;
247 else if(tmp_env1->flags > tmp_env2->flags)
248 gd->env_valid = 1;
249 else if(tmp_env2->flags > tmp_env1->flags)
250 gd->env_valid = 2;
251 else /* flags are equal - almost impossible */
252 gd->env_valid = 1;
255 #if 0
256 free(env_ptr);
257 #endif
258 if(gd->env_valid == 1) {
259 env_ptr = tmp_env1;
260 free(tmp_env2);
261 } else {
262 env_ptr = tmp_env2;
263 free(tmp_env1);
266 #endif /* ! ENV_IS_EMBEDDED */
268 #else /* ! CFG_ENV_OFFSET_REDUND */
270 * The legacy NAND code saved the environment in the first NAND device i.e.,
271 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
273 void env_relocate_spec (void)
275 #if !defined(ENV_IS_EMBEDDED)
276 ulong total;
277 int ret;
279 total = CFG_ENV_SIZE;
280 ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
281 if (ret || total != CFG_ENV_SIZE)
282 return use_default();
284 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
285 return use_default();
286 #endif /* ! ENV_IS_EMBEDDED */
288 #endif /* CFG_ENV_OFFSET_REDUND */
290 #if !defined(ENV_IS_EMBEDDED)
291 static void use_default()
293 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
295 if (default_environment_size > CFG_ENV_SIZE){
296 puts ("*** Error - default environment is too large\n\n");
297 return;
300 memset (env_ptr, 0, sizeof(env_t));
301 memcpy (env_ptr->data,
302 default_environment,
303 default_environment_size);
304 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
305 gd->env_valid = 1;
308 #endif
310 #endif /* CFG_ENV_IS_IN_NAND */