* Code cleanup:
[platform/kernel/u-boot.git] / common / env_flash.c
1 /*
2  * (C) Copyright 2000-2002
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
31 #if defined(CFG_ENV_IS_IN_FLASH) /* Environment is in Flash */
32
33 #include <command.h>
34 #include <environment.h>
35 #include <linux/stddef.h>
36 #include <malloc.h>
37
38 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
39 #define CMD_SAVEENV
40 #elif defined(CFG_ENV_ADDR_REDUND)
41 #error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH
42 #endif
43
44 #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
45 #error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
46 #endif
47
48 #ifdef CONFIG_INFERNO
49 # ifdef CFG_ENV_ADDR_REDUND
50 #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
51 # endif
52 #endif
53
54 char * env_name_spec = "Flash";
55
56 #ifdef ENV_IS_EMBEDDED
57
58 extern uchar environment[];
59 env_t *env_ptr = (env_t *)(&environment[0]);
60
61 #ifdef CMD_SAVEENV
62 /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
63 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
64 #endif
65
66 #else /* ! ENV_IS_EMBEDDED */
67
68 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
69 #ifdef CMD_SAVEENV
70 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
71 #endif
72
73 #endif /* ENV_IS_EMBEDDED */
74
75 #ifdef CFG_ENV_ADDR_REDUND
76 static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
77
78 /* CFG_ENV_ADDR is supposed to be on sector boundary */
79 static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
80 static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
81
82 static uchar active_flag = 1;
83 static uchar obsolete_flag = 0;
84 #endif
85
86 extern uchar default_environment[];
87 extern int default_environment_size;
88
89
90 uchar env_get_char_spec (int index)
91 {
92         DECLARE_GLOBAL_DATA_PTR;
93
94         return ( *((uchar *)(gd->env_addr + index)) );
95 }
96
97 #ifdef CFG_ENV_ADDR_REDUND
98
99 int  env_init(void)
100 {
101         DECLARE_GLOBAL_DATA_PTR;
102
103         int crc1_ok =
104                 (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
105         int crc2_ok =
106                 (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
107
108         uchar flag1 = flash_addr->flags;
109         uchar flag2 = flash_addr_new->flags;
110
111         ulong addr_default = (ulong)&default_environment[0];
112         ulong addr1 = (ulong)&(flash_addr->data);
113         ulong addr2 = (ulong)&(flash_addr_new->data);
114
115         if (crc1_ok && ! crc2_ok)
116         {
117                 gd->env_addr  = addr1;
118                 gd->env_valid = 1;
119         }
120         else if (! crc1_ok && crc2_ok)
121         {
122                 gd->env_addr  = addr2;
123                 gd->env_valid = 1;
124         }
125         else if (! crc1_ok && ! crc2_ok)
126         {
127                 gd->env_addr  = addr_default;
128                 gd->env_valid = 0;
129         }
130         else if (flag1 == active_flag && flag2 == obsolete_flag)
131         {
132                 gd->env_addr  = addr1;
133                 gd->env_valid = 1;
134         }
135         else if (flag1 == obsolete_flag && flag2 == active_flag)
136         {
137                 gd->env_addr  = addr2;
138                 gd->env_valid = 1;
139         }
140         else if (flag1 == flag2)
141         {
142                 gd->env_addr  = addr1;
143                 gd->env_valid = 2;
144         }
145         else if (flag1 == 0xFF)
146         {
147                 gd->env_addr  = addr1;
148                 gd->env_valid = 2;
149         }
150         else if (flag2 == 0xFF)
151         {
152                 gd->env_addr  = addr2;
153                 gd->env_valid = 2;
154         }
155
156         return (0);
157 }
158
159 #ifdef CMD_SAVEENV
160 int saveenv(void)
161 {
162         char *saved_data = NULL;
163         int rc = 1;
164 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
165         ulong up_data = 0;
166 #endif
167
168         debug ("Protect off %08lX ... %08lX\n",
169                 (ulong)flash_addr, end_addr);
170
171         if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
172                 goto Done;
173         }
174
175         debug ("Protect off %08lX ... %08lX\n",
176                 (ulong)flash_addr_new, end_addr_new);
177
178         if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
179                 goto Done;
180         }
181
182 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
183         up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
184         debug ("Data to save 0x%x\n", up_data);
185         if (up_data) {
186                 if ((saved_data = malloc(up_data)) == NULL) {
187                         printf("Unable to save the rest of sector (%ld)\n",
188                                 up_data);
189                         goto Done;
190                 }
191                 memcpy(saved_data,
192                         (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
193                 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
194                            (long)flash_addr_new + CFG_ENV_SIZE,
195                                 up_data, saved_data);
196         }
197 #endif
198         puts ("Erasing Flash...");
199         debug (" %08lX ... %08lX ...",
200                 (ulong)flash_addr_new, end_addr_new);
201
202         if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
203                 goto Done;
204         }
205
206         puts ("Writing to Flash... ");
207         debug (" %08lX ... %08lX ...",
208                 (ulong)&(flash_addr_new->data),
209                 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
210         if (flash_write(env_ptr->data,
211                         (ulong)&(flash_addr_new->data),
212                         sizeof(env_ptr->data)) ||
213
214             flash_write((char *)&(env_ptr->crc),
215                         (ulong)&(flash_addr_new->crc),
216                         sizeof(env_ptr->crc)) ||
217
218             flash_write((char *)&obsolete_flag,
219                         (ulong)&(flash_addr->flags),
220                         sizeof(flash_addr->flags)) ||
221
222             flash_write((char *)&active_flag,
223                         (ulong)&(flash_addr_new->flags),
224                         sizeof(flash_addr_new->flags)))
225         {
226                 flash_perror (rc);
227                 goto Done;
228         }
229         puts ("done\n");
230
231 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
232         if (up_data) { /* restore the rest of sector */
233                 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
234                            (long)flash_addr_new + CFG_ENV_SIZE, up_data);
235                 if (flash_write(saved_data,
236                                 (long)flash_addr_new + CFG_ENV_SIZE,
237                                 up_data)) {
238                         flash_perror(rc);
239                         goto Done;
240                 }
241         }
242 #endif
243         {
244                 env_t * etmp = flash_addr;
245                 ulong ltmp = end_addr;
246
247                 flash_addr = flash_addr_new;
248                 flash_addr_new = etmp;
249
250                 end_addr = end_addr_new;
251                 end_addr_new = ltmp;
252         }
253
254         rc = 0;
255 Done:
256
257         if (saved_data)
258                 free (saved_data);
259         /* try to re-protect */
260         (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
261         (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
262
263         return rc;
264 }
265 #endif /* CMD_SAVEENV */
266
267 #else /* ! CFG_ENV_ADDR_REDUND */
268
269 int  env_init(void)
270 {
271         DECLARE_GLOBAL_DATA_PTR;
272
273         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
274                 gd->env_addr  = (ulong)&(env_ptr->data);
275                 gd->env_valid = 1;
276         } else {
277                 gd->env_addr  = (ulong)&default_environment[0];
278                 gd->env_valid = 0;
279         }
280
281         return (0);
282 }
283
284 #ifdef CMD_SAVEENV
285
286 int saveenv(void)
287 {
288         int     len, rc;
289         ulong   end_addr;
290         ulong   flash_sect_addr;
291 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
292         ulong   flash_offset;
293         uchar   env_buffer[CFG_ENV_SECT_SIZE];
294 #else
295         uchar *env_buffer = (char *)env_ptr;
296 #endif  /* CFG_ENV_SECT_SIZE */
297         int rcode = 0;
298
299 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
300
301         flash_offset    = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
302         flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
303
304         debug ( "copy old content: "
305                 "sect_addr: %08lX  env_addr: %08lX  offset: %08lX\n",
306                 flash_sect_addr, (ulong)flash_addr, flash_offset);
307
308         /* copy old contents to temporary buffer */
309         memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
310
311         /* copy current environment to temporary buffer */
312         memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
313                 env_ptr,
314                 CFG_ENV_SIZE);
315
316         len      = CFG_ENV_SECT_SIZE;
317 #else
318         flash_sect_addr = (ulong)flash_addr;
319         len      = CFG_ENV_SIZE;
320 #endif  /* CFG_ENV_SECT_SIZE */
321
322 #ifndef CONFIG_INFERNO
323         end_addr = flash_sect_addr + len - 1;
324 #else
325         /* this is the last sector, and the size is hardcoded here */
326         /* otherwise we will get stack problems on loading 128 KB environment */
327         end_addr = flash_sect_addr + 0x20000 - 1;
328 #endif
329
330         debug ("Protect off %08lX ... %08lX\n",
331                 (ulong)flash_sect_addr, end_addr);
332
333         if (flash_sect_protect (0, flash_sect_addr, end_addr))
334                 return 1;
335
336         puts ("Erasing Flash...");
337         if (flash_sect_erase (flash_sect_addr, end_addr))
338                 return 1;
339
340         puts ("Writing to Flash... ");
341         rc = flash_write(env_buffer, flash_sect_addr, len);
342         if (rc != 0) {
343                 flash_perror (rc);
344                 rcode = 1;
345         } else {
346                 puts ("done\n");
347         }
348
349         /* try to re-protect */
350         (void) flash_sect_protect (1, flash_sect_addr, end_addr);
351         return rcode;
352 }
353
354 #endif /* CMD_SAVEENV */
355
356 #endif /* CFG_ENV_ADDR_REDUND */
357
358 void env_relocate_spec (void)
359 {
360 #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
361 #ifdef CFG_ENV_ADDR_REDUND
362         DECLARE_GLOBAL_DATA_PTR;
363
364         if (gd->env_addr != (ulong)&(flash_addr->data))
365         {
366                 env_t * etmp = flash_addr;
367                 ulong ltmp = end_addr;
368
369                 flash_addr = flash_addr_new;
370                 flash_addr_new = etmp;
371
372                 end_addr = end_addr_new;
373                 end_addr_new = ltmp;
374         }
375
376         if (flash_addr_new->flags != obsolete_flag &&
377             crc32(0, flash_addr_new->data, ENV_SIZE) ==
378             flash_addr_new->crc)
379         {
380                 gd->env_valid = 2;
381                 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
382                 flash_write((char *)&obsolete_flag,
383                             (ulong)&(flash_addr_new->flags),
384                             sizeof(flash_addr_new->flags));
385                 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
386         }
387
388         if (flash_addr->flags != active_flag &&
389             (flash_addr->flags & active_flag) == active_flag)
390         {
391                 gd->env_valid = 2;
392                 flash_sect_protect (0, (ulong)flash_addr, end_addr);
393                 flash_write((char *)&active_flag,
394                             (ulong)&(flash_addr->flags),
395                             sizeof(flash_addr->flags));
396                 flash_sect_protect (1, (ulong)flash_addr, end_addr);
397         }
398
399         if (gd->env_valid == 2)
400                 puts ("*** Warning - some problems detected "
401                       "reading environment; recovered successfully\n\n");
402 #endif /* CFG_ENV_ADDR_REDUND */
403         memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
404 #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
405 }
406
407 #endif /* CFG_ENV_IS_IN_FLASH */