blob: 2e1bfa666596b2531852ee46cc1ac69712404f2e [file] [log] [blame]
wdenk13a56952004-06-09 14:58:14 +00001/*
2 * (C) Copyright 2004
3 * Jian Zhang, Texas Instruments, jzhang@ti.com.
4
5 * (C) Copyright 2000-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Andreas Heppel <aheppel@sysgo.de>
10
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
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.
18 *
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.
23 *
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
28 */
29
30/* #define DEBUG */
31
32#include <common.h>
33
34#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
35
36#include <command.h>
37#include <environment.h>
38#include <linux/stddef.h>
Markus Klotzbuechere443c942006-03-20 18:02:44 +010039#include <malloc.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010040#include <nand.h>
wdenk13a56952004-06-09 14:58:14 +000041
42#if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
43#define CMD_SAVEENV
Markus Klotzbuechere443c942006-03-20 18:02:44 +010044#elif defined(CFG_ENV_OFFSET_REDUND)
45#error Cannot use CFG_ENV_OFFSET_REDUND without CFG_CMD_ENV & CFG_CMD_NAND
wdenk13a56952004-06-09 14:58:14 +000046#endif
47
Markus Klotzbuechere443c942006-03-20 18:02:44 +010048#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
wdenk13a56952004-06-09 14:58:14 +000050#endif
51
wdenk13a56952004-06-09 14:58:14 +000052#ifdef CONFIG_INFERNO
53#error CONFIG_INFERNO not supported yet
54#endif
55
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010056int nand_legacy_rw (struct nand_chip* nand, int cmd,
wdenk13a56952004-06-09 14:58:14 +000057 size_t start, size_t len,
58 size_t * retlen, u_char * buf);
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010059
60/* info for NAND chips, defined in drivers/nand/nand.c */
61extern nand_info_t nand_info[];
wdenk13a56952004-06-09 14:58:14 +000062
63/* references to names in env_common.c */
64extern uchar default_environment[];
65extern int default_environment_size;
66
67char * env_name_spec = "NAND";
68
69
70#ifdef ENV_IS_EMBEDDED
71extern uchar environment[];
72env_t *env_ptr = (env_t *)(&environment[0]);
73#else /* ! ENV_IS_EMBEDDED */
wdenk49822e22004-06-19 21:19:10 +000074env_t *env_ptr = 0;
wdenk13a56952004-06-09 14:58:14 +000075#endif /* ENV_IS_EMBEDDED */
76
77
78/* local functions */
79static void use_default(void);
80
81
82uchar env_get_char_spec (int index)
83{
84 DECLARE_GLOBAL_DATA_PTR;
85
86 return ( *((uchar *)(gd->env_addr + index)) );
87}
88
89
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 will does
94 * the real validation.
95 */
96int env_init(void)
97{
98 DECLARE_GLOBAL_DATA_PTR;
99
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100100 gd->env_addr = (ulong)&default_environment[0];
wdenk13a56952004-06-09 14:58:14 +0000101 gd->env_valid = 1;
102
103 return (0);
104}
105
106#ifdef CMD_SAVEENV
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100107/*
108 * The legacy NAND code saved the environment in the first NAND device i.e.,
109 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
110 */
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100111#ifdef CFG_ENV_OFFSET_REDUND
wdenk13a56952004-06-09 14:58:14 +0000112int saveenv(void)
113{
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100114 int total, ret = 0;
115
116 DECLARE_GLOBAL_DATA_PTR;
117
118 env_ptr->flags++;
119 total = CFG_ENV_SIZE;
120
121 if(gd->env_valid == 1) {
122 puts ("Erasing redundant Nand...");
123 if (nand_erase(&nand_info[0],
124 CFG_ENV_OFFSET_REDUND, CFG_ENV_SIZE))
125 return 1;
126 puts ("Writing to redundant Nand... ");
127 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
128 (u_char*) env_ptr);
129 } else {
130 puts ("Erasing Nand...");
131 if (nand_erase(&nand_info[0],
132 CFG_ENV_OFFSET, CFG_ENV_SIZE))
133 return 1;
134
135 puts ("Writing to Nand... ");
136 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
137 (u_char*) env_ptr);
138 }
139 if (ret || total != CFG_ENV_SIZE)
140 return 1;
141
142 puts ("done\n");
143 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
144 return ret;
145}
146#else /* ! CFG_ENV_OFFSET_REDUND */
147int saveenv(void)
148{
149 int total, ret = 0;
wdenk13a56952004-06-09 14:58:14 +0000150
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100151 puts ("Erasing Nand...");
Markus Klotzbuecher3c4eb082006-03-08 00:04:04 +0100152 if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
wdenk13a56952004-06-09 14:58:14 +0000153 return 1;
154
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100155 puts ("Writing to Nand... ");
156 total = CFG_ENV_SIZE;
157 ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
158 (u_char*) env_ptr);
159 if (ret || total != CFG_ENV_SIZE)
160 return 1;
161
162 puts ("done\n");
163 return ret;
wdenk13a56952004-06-09 14:58:14 +0000164}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100165#endif /* CFG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000166#endif /* CMD_SAVEENV */
167
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100168#ifdef CFG_ENV_OFFSET_REDUND
169void env_relocate_spec (void)
170{
171#if !defined(ENV_IS_EMBEDDED)
172 int crc1_ok = 0, crc2_ok = 0, total;
173 env_t *tmp_env1, *tmp_env2;
wdenk13a56952004-06-09 14:58:14 +0000174
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100175 DECLARE_GLOBAL_DATA_PTR;
176
177 total = CFG_ENV_SIZE;
178
179 tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
180 tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
181
182 nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
183 (u_char*) tmp_env1);
184 nand_read(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
185 (u_char*) tmp_env2);
186
187 crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
188 crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
189
190 if(!crc1_ok && !crc2_ok)
191 return use_default();
192 else if(crc1_ok && !crc2_ok)
193 gd->env_valid = 1;
194 else if(!crc1_ok && crc2_ok)
195 gd->env_valid = 2;
196 else {
197 /* both ok - check serial */
198 if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
199 gd->env_valid = 2;
200 else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
201 gd->env_valid = 1;
202 else if(tmp_env1->flags > tmp_env2->flags)
203 gd->env_valid = 1;
204 else if(tmp_env2->flags > tmp_env1->flags)
205 gd->env_valid = 2;
206 else /* flags are equal - almost impossible */
207 gd->env_valid = 1;
208
209 }
210
211 free(env_ptr);
212 if(gd->env_valid == 1) {
213 env_ptr = tmp_env1;
214 free(tmp_env2);
215 } else {
216 env_ptr = tmp_env2;
217 free(tmp_env1);
218 }
219
220#endif /* ! ENV_IS_EMBEDDED */
221}
222#else /* ! CFG_ENV_OFFSET_REDUND */
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100223/*
224 * The legacy NAND code saved the environment in the first NAND device i.e.,
225 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
226 */
wdenk13a56952004-06-09 14:58:14 +0000227void env_relocate_spec (void)
228{
229#if !defined(ENV_IS_EMBEDDED)
230 int ret, total;
231
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100232 total = CFG_ENV_SIZE;
233 ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
234 (u_char*) env_ptr);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100235 if (ret || total != CFG_ENV_SIZE)
wdenk13a56952004-06-09 14:58:14 +0000236 return use_default();
237
238 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
239 return use_default();
240#endif /* ! ENV_IS_EMBEDDED */
wdenk13a56952004-06-09 14:58:14 +0000241}
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100242#endif /* CFG_ENV_OFFSET_REDUND */
wdenk13a56952004-06-09 14:58:14 +0000243
244static void use_default()
245{
246 DECLARE_GLOBAL_DATA_PTR;
247
248 puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
249
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100250 if (default_environment_size > CFG_ENV_SIZE){
wdenk13a56952004-06-09 14:58:14 +0000251 puts ("*** Error - default environment is too large\n\n");
252 return;
253 }
254
255 memset (env_ptr, 0, sizeof(env_t));
256 memcpy (env_ptr->data,
257 default_environment,
258 default_environment_size);
259 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
Markus Klotzbuechere443c942006-03-20 18:02:44 +0100260 gd->env_valid = 1;
wdenk13a56952004-06-09 14:58:14 +0000261
262}
263
264#endif /* CFG_ENV_IS_IN_NAND */