NAND: add the ability to directly write yaffs image
[platform/kernel/u-boot.git] / common / env_flash.c
1 /*
2  * (C) Copyright 2000-2010
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6  * Andreas Heppel <aheppel@sysgo.de>
7
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 /* #define DEBUG */
28
29 #include <common.h>
30 #include <command.h>
31 #include <environment.h>
32 #include <linux/stddef.h>
33 #include <malloc.h>
34 #include <search.h>
35 #include <errno.h>
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
40 #define CMD_SAVEENV
41 #elif defined(CONFIG_ENV_ADDR_REDUND)
42 #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
43 #endif
44
45 #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
46 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
47 #endif
48
49 char * env_name_spec = "Flash";
50
51 #ifdef ENV_IS_EMBEDDED
52
53 extern uchar environment[];
54 env_t *env_ptr = (env_t *)(&environment[0]);
55
56 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
57
58 #else /* ! ENV_IS_EMBEDDED */
59
60 env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
61 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
62
63 #endif /* ENV_IS_EMBEDDED */
64
65 #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
66 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
67 static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
68 #endif
69
70 #ifdef CONFIG_ENV_ADDR_REDUND
71 static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
72
73 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
74 static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
75 #endif /* CONFIG_ENV_ADDR_REDUND */
76
77 extern uchar default_environment[];
78
79
80 uchar env_get_char_spec(int index)
81 {
82         return (*((uchar *)(gd->env_addr + index)));
83 }
84
85 #ifdef CONFIG_ENV_ADDR_REDUND
86
87 int  env_init(void)
88 {
89         int crc1_ok = 0, crc2_ok = 0;
90
91         uchar flag1 = flash_addr->flags;
92         uchar flag2 = flash_addr_new->flags;
93
94         ulong addr_default = (ulong)&default_environment[0];
95         ulong addr1 = (ulong)&(flash_addr->data);
96         ulong addr2 = (ulong)&(flash_addr_new->data);
97
98         crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
99         crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
100
101         if (crc1_ok && ! crc2_ok) {
102                 gd->env_addr  = addr1;
103                 gd->env_valid = 1;
104         } else if (! crc1_ok && crc2_ok) {
105                 gd->env_addr  = addr2;
106                 gd->env_valid = 1;
107         } else if (! crc1_ok && ! crc2_ok) {
108                 gd->env_addr  = addr_default;
109                 gd->env_valid = 0;
110         } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
111                 gd->env_addr  = addr1;
112                 gd->env_valid = 1;
113         } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
114                 gd->env_addr  = addr2;
115                 gd->env_valid = 1;
116         } else if (flag1 == flag2) {
117                 gd->env_addr  = addr1;
118                 gd->env_valid = 2;
119         } else if (flag1 == 0xFF) {
120                 gd->env_addr  = addr1;
121                 gd->env_valid = 2;
122         } else if (flag2 == 0xFF) {
123                 gd->env_addr  = addr2;
124                 gd->env_valid = 2;
125         }
126
127         return 0;
128 }
129
130 #ifdef CMD_SAVEENV
131 int saveenv(void)
132 {
133         env_t   env_new;
134         ssize_t len;
135         char    *saved_data = NULL;
136         char    *res;
137         int     rc = 1;
138         char    flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
139 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
140         ulong   up_data = 0;
141 #endif
142
143         debug("Protect off %08lX ... %08lX\n",
144                 (ulong)flash_addr, end_addr);
145
146         if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
147                 goto done;
148         }
149
150         debug("Protect off %08lX ... %08lX\n",
151                 (ulong)flash_addr_new, end_addr_new);
152
153         if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
154                 goto done;
155         }
156
157         res = (char *)&env_new.data;
158         len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
159         if (len < 0) {
160                 error("Cannot export environment: errno = %d\n", errno);
161                 goto done;
162         }
163         env_new.crc   = crc32(0, env_new.data, ENV_SIZE);
164         env_new.flags = new_flag;
165
166 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
167         up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
168         debug("Data to save 0x%lX\n", up_data);
169         if (up_data) {
170                 if ((saved_data = malloc(up_data)) == NULL) {
171                         printf("Unable to save the rest of sector (%ld)\n",
172                                 up_data);
173                         goto done;
174                 }
175                 memcpy(saved_data,
176                         (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
177                 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
178                         (long)flash_addr_new + CONFIG_ENV_SIZE,
179                         up_data, saved_data);
180         }
181 #endif
182         puts("Erasing Flash...");
183         debug(" %08lX ... %08lX ...",
184                 (ulong)flash_addr_new, end_addr_new);
185
186         if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
187                 goto done;
188         }
189
190         puts("Writing to Flash... ");
191         debug(" %08lX ... %08lX ...",
192                 (ulong)&(flash_addr_new->data),
193                 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
194         if ((rc = flash_write((char *)&env_new,
195                         (ulong)flash_addr_new,
196                         sizeof(env_new))) ||
197             (rc = flash_write(&flag,
198                         (ulong)&(flash_addr->flags),
199                         sizeof(flash_addr->flags))) ) {
200                 flash_perror(rc);
201                 goto done;
202         }
203
204 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
205         if (up_data) { /* restore the rest of sector */
206                 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
207                         (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
208                 if (flash_write(saved_data,
209                                 (long)flash_addr_new + CONFIG_ENV_SIZE,
210                                 up_data)) {
211                         flash_perror(rc);
212                         goto done;
213                 }
214         }
215 #endif
216         puts("done\n");
217
218         {
219                 env_t * etmp = flash_addr;
220                 ulong ltmp = end_addr;
221
222                 flash_addr = flash_addr_new;
223                 flash_addr_new = etmp;
224
225                 end_addr = end_addr_new;
226                 end_addr_new = ltmp;
227         }
228
229         rc = 0;
230 done:
231         if (saved_data)
232                 free(saved_data);
233         /* try to re-protect */
234         (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
235         (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
236
237         return rc;
238 }
239 #endif /* CMD_SAVEENV */
240
241 #else /* ! CONFIG_ENV_ADDR_REDUND */
242
243 int  env_init(void)
244 {
245         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
246                 gd->env_addr  = (ulong)&(env_ptr->data);
247                 gd->env_valid = 1;
248                 return(0);
249         }
250
251         gd->env_addr  = (ulong)&default_environment[0];
252         gd->env_valid = 0;
253         return 0;
254 }
255
256 #ifdef CMD_SAVEENV
257
258 int saveenv(void)
259 {
260         env_t   env_new;
261         ssize_t len;
262         int     rc = 1;
263         char    *res;
264         char    *saved_data = NULL;
265 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
266         ulong   up_data = 0;
267
268         up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
269         debug("Data to save 0x%lx\n", up_data);
270         if (up_data) {
271                 if ((saved_data = malloc(up_data)) == NULL) {
272                         printf("Unable to save the rest of sector (%ld)\n",
273                                 up_data);
274                         goto done;
275                 }
276                 memcpy(saved_data,
277                         (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
278                 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
279                         (ulong)flash_addr + CONFIG_ENV_SIZE,
280                         up_data,
281                         (ulong)saved_data);
282         }
283 #endif  /* CONFIG_ENV_SECT_SIZE */
284
285         debug("Protect off %08lX ... %08lX\n",
286                 (ulong)flash_addr, end_addr);
287
288         if (flash_sect_protect(0, (long)flash_addr, end_addr))
289                 goto done;
290
291         res = (char *)&env_new.data;
292         len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
293         if (len < 0) {
294                 error("Cannot export environment: errno = %d\n", errno);
295                 goto done;
296         }
297         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
298
299         puts("Erasing Flash...");
300         if (flash_sect_erase((long)flash_addr, end_addr))
301                 goto done;
302
303         puts("Writing to Flash... ");
304         rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
305         if (rc != 0) {
306                 flash_perror(rc);
307                 goto done;
308         }
309 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
310         if (up_data) {  /* restore the rest of sector */
311                 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
312                         (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
313                 if (flash_write(saved_data,
314                                 (long)flash_addr + CONFIG_ENV_SIZE,
315                                 up_data)) {
316                         flash_perror(rc);
317                         goto done;
318                 }
319         }
320 #endif
321         puts("done\n");
322         rc = 0;
323 done:
324         if (saved_data)
325                 free(saved_data);
326         /* try to re-protect */
327         (void) flash_sect_protect(1, (long)flash_addr, end_addr);
328         return rc;
329 }
330
331 #endif /* CMD_SAVEENV */
332
333 #endif /* CONFIG_ENV_ADDR_REDUND */
334
335 void env_relocate_spec(void)
336 {
337 #ifdef CONFIG_ENV_ADDR_REDUND
338         if (gd->env_addr != (ulong)&(flash_addr->data)) {
339                 env_t *etmp = flash_addr;
340                 ulong ltmp = end_addr;
341
342                 flash_addr = flash_addr_new;
343                 flash_addr_new = etmp;
344
345                 end_addr = end_addr_new;
346                 end_addr_new = ltmp;
347         }
348
349         if (flash_addr_new->flags != OBSOLETE_FLAG &&
350             crc32(0, flash_addr_new->data, ENV_SIZE) ==
351             flash_addr_new->crc) {
352                 char flag = OBSOLETE_FLAG;
353
354                 gd->env_valid = 2;
355                 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
356                 flash_write(&flag,
357                             (ulong)&(flash_addr_new->flags),
358                             sizeof(flash_addr_new->flags));
359                 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
360         }
361
362         if (flash_addr->flags != ACTIVE_FLAG &&
363             (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
364                 char flag = ACTIVE_FLAG;
365
366                 gd->env_valid = 2;
367                 flash_sect_protect(0, (ulong)flash_addr, end_addr);
368                 flash_write(&flag,
369                             (ulong)&(flash_addr->flags),
370                             sizeof(flash_addr->flags));
371                 flash_sect_protect(1, (ulong)flash_addr, end_addr);
372         }
373
374         if (gd->env_valid == 2)
375                 puts ("*** Warning - some problems detected "
376                       "reading environment; recovered successfully\n\n");
377 #endif /* CONFIG_ENV_ADDR_REDUND */
378
379         env_import((char *)flash_addr, 1);
380 }