]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_flash.c
Merge branch 'next' of /home/wd/git/u-boot/next
[karo-tx-uboot.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 #undef debug
86 #define debug printf
87
88 #ifdef CONFIG_ENV_ADDR_REDUND
89
90 int  env_init(void)
91 {
92         int crc1_ok = 0, crc2_ok = 0;
93
94         uchar flag1 = flash_addr->flags;
95         uchar flag2 = flash_addr_new->flags;
96
97         ulong addr_default = (ulong)&default_environment[0];
98         ulong addr1 = (ulong)&(flash_addr->data);
99         ulong addr2 = (ulong)&(flash_addr_new->data);
100
101         crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
102         crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
103
104         if (crc1_ok && ! crc2_ok) {
105                 gd->env_addr  = addr1;
106                 gd->env_valid = 1;
107         } else if (! crc1_ok && crc2_ok) {
108                 gd->env_addr  = addr2;
109                 gd->env_valid = 1;
110         } else if (! crc1_ok && ! crc2_ok) {
111                 gd->env_addr  = addr_default;
112                 gd->env_valid = 0;
113         } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
114                 gd->env_addr  = addr1;
115                 gd->env_valid = 1;
116         } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
117                 gd->env_addr  = addr2;
118                 gd->env_valid = 1;
119         } else if (flag1 == flag2) {
120                 gd->env_addr  = addr1;
121                 gd->env_valid = 2;
122         } else if (flag1 == 0xFF) {
123                 gd->env_addr  = addr1;
124                 gd->env_valid = 2;
125         } else if (flag2 == 0xFF) {
126                 gd->env_addr  = addr2;
127                 gd->env_valid = 2;
128         }
129
130         return 0;
131 }
132
133 #ifdef CMD_SAVEENV
134 int saveenv(void)
135 {
136         env_t   env_new;
137         ssize_t len;
138         char    *saved_data = NULL;
139         char    *res;
140         int     rc = 1;
141         char    flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
142 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
143         ulong   up_data = 0;
144 #endif
145
146         debug("Protect off %08lX ... %08lX\n",
147                 (ulong)flash_addr, end_addr);
148
149         if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
150                 goto done;
151         }
152
153         debug("Protect off %08lX ... %08lX\n",
154                 (ulong)flash_addr_new, end_addr_new);
155
156         if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
157                 goto done;
158         }
159
160         res = (char *)&env_new.data;
161         len = hexport('\0', &res, ENV_SIZE);
162         if (len < 0) {
163                 error("Cannot export environment: errno = %d\n", errno);
164                 goto done;
165         }
166         env_new.crc   = crc32(0, env_new.data, ENV_SIZE);
167         env_new.flags = new_flag;
168
169 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
170         up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
171         debug("Data to save 0x%lX\n", up_data);
172         if (up_data) {
173                 if ((saved_data = malloc(up_data)) == NULL) {
174                         printf("Unable to save the rest of sector (%ld)\n",
175                                 up_data);
176                         goto done;
177                 }
178                 memcpy(saved_data,
179                         (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
180                 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
181                         (long)flash_addr_new + CONFIG_ENV_SIZE,
182                         up_data, saved_data);
183         }
184 #endif
185         puts("Erasing Flash...");
186         debug(" %08lX ... %08lX ...",
187                 (ulong)flash_addr_new, end_addr_new);
188
189         if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
190                 goto done;
191         }
192
193         puts("Writing to Flash... ");
194         debug(" %08lX ... %08lX ...",
195                 (ulong)&(flash_addr_new->data),
196                 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
197         if ((rc = flash_write((char *)&env_new,
198                         (ulong)flash_addr_new,
199                         sizeof(env_new))) ||
200             (rc = flash_write(&flag,
201                         (ulong)&(flash_addr->flags),
202                         sizeof(flash_addr->flags))) ) {
203                 flash_perror(rc);
204                 goto done;
205         }
206
207 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
208         if (up_data) { /* restore the rest of sector */
209                 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
210                         (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
211                 if (flash_write(saved_data,
212                                 (long)flash_addr_new + CONFIG_ENV_SIZE,
213                                 up_data)) {
214                         flash_perror(rc);
215                         goto done;
216                 }
217         }
218 #endif
219         puts("done\n");
220
221         {
222                 env_t * etmp = flash_addr;
223                 ulong ltmp = end_addr;
224
225                 flash_addr = flash_addr_new;
226                 flash_addr_new = etmp;
227
228                 end_addr = end_addr_new;
229                 end_addr_new = ltmp;
230         }
231
232         rc = 0;
233 done:
234         if (saved_data)
235                 free(saved_data);
236         /* try to re-protect */
237         (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
238         (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
239
240         return rc;
241 }
242 #endif /* CMD_SAVEENV */
243
244 #else /* ! CONFIG_ENV_ADDR_REDUND */
245
246 int  env_init(void)
247 {
248         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
249                 gd->env_addr  = (ulong)&(env_ptr->data);
250                 gd->env_valid = 1;
251                 return(0);
252         }
253
254         gd->env_addr  = (ulong)&default_environment[0];
255         gd->env_valid = 0;
256         return 0;
257 }
258
259 #ifdef CMD_SAVEENV
260
261 int saveenv(void)
262 {
263         env_t   env_new;
264         ssize_t len;
265         int     rc = 1;
266         char    *res;
267         char    *saved_data = NULL;
268 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
269         ulong   up_data = 0;
270
271         up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
272         debug("Data to save 0x%lx\n", up_data);
273         if (up_data) {
274                 if ((saved_data = malloc(up_data)) == NULL) {
275                         printf("Unable to save the rest of sector (%ld)\n",
276                                 up_data);
277                         goto done;
278                 }
279                 memcpy(saved_data,
280                         (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
281                 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
282                         (ulong)flash_addr + CONFIG_ENV_SIZE,
283                         up_data,
284                         (ulong)saved_data);
285         }
286 #endif  /* CONFIG_ENV_SECT_SIZE */
287
288         debug("Protect off %08lX ... %08lX\n",
289                 (ulong)flash_addr, end_addr);
290
291         if (flash_sect_protect(0, (long)flash_addr, end_addr))
292                 goto done;
293
294         res = (char *)&env_new.data;
295         len = hexport('\0', &res, ENV_SIZE);
296         if (len < 0) {
297                 error("Cannot export environment: errno = %d\n", errno);
298                 goto done;
299         }
300         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
301
302         puts("Erasing Flash...");
303         if (flash_sect_erase((long)flash_addr, end_addr))
304                 goto done;
305
306         puts("Writing to Flash... ");
307         rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
308         if (rc != 0) {
309                 flash_perror(rc);
310                 goto done;
311         }
312 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
313         if (up_data) {  /* restore the rest of sector */
314                 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
315                         (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
316                 if (flash_write(saved_data,
317                                 (long)flash_addr + CONFIG_ENV_SIZE,
318                                 up_data)) {
319                         flash_perror(rc);
320                         goto done;
321                 }
322         }
323 #endif
324         puts("done\n");
325         rc = 0;
326 done:
327         if (saved_data)
328                 free(saved_data);
329         /* try to re-protect */
330         (void) flash_sect_protect(1, (long)flash_addr, end_addr);
331         return rc;
332 }
333
334 #endif /* CMD_SAVEENV */
335
336 #endif /* CONFIG_ENV_ADDR_REDUND */
337
338 void env_relocate_spec(void)
339 {
340 #ifdef CONFIG_ENV_ADDR_REDUND
341         if (gd->env_addr != (ulong)&(flash_addr->data)) {
342                 env_t *etmp = flash_addr;
343                 ulong ltmp = end_addr;
344
345                 flash_addr = flash_addr_new;
346                 flash_addr_new = etmp;
347
348                 end_addr = end_addr_new;
349                 end_addr_new = ltmp;
350         }
351
352         if (flash_addr_new->flags != OBSOLETE_FLAG &&
353             crc32(0, flash_addr_new->data, ENV_SIZE) ==
354             flash_addr_new->crc) {
355                 char flag = OBSOLETE_FLAG;
356
357                 gd->env_valid = 2;
358                 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
359                 flash_write(&flag,
360                             (ulong)&(flash_addr_new->flags),
361                             sizeof(flash_addr_new->flags));
362                 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
363         }
364
365         if (flash_addr->flags != ACTIVE_FLAG &&
366             (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
367                 char flag = ACTIVE_FLAG;
368
369                 gd->env_valid = 2;
370                 flash_sect_protect(0, (ulong)flash_addr, end_addr);
371                 flash_write(&flag,
372                             (ulong)&(flash_addr->flags),
373                             sizeof(flash_addr->flags));
374                 flash_sect_protect(1, (ulong)flash_addr, end_addr);
375         }
376
377         if (gd->env_valid == 2)
378                 puts ("*** Warning - some problems detected "
379                       "reading environment; recovered successfully\n\n");
380 #endif /* CONFIG_ENV_ADDR_REDUND */
381
382         env_import((char *)flash_addr, 1);
383 }