Merge samsung, imx, tegra into u-boot-arm/master
[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 CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
43 #endif
44
45 #if defined(CONFIG_ENV_SIZE_REDUND) &&  \
46         (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
47 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
48 #endif
49
50 char *env_name_spec = "Flash";
51
52 #ifdef ENV_IS_EMBEDDED
53 env_t *env_ptr = &environment;
54
55 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
56
57 #else /* ! ENV_IS_EMBEDDED */
58
59 env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
60 static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
61 #endif /* ENV_IS_EMBEDDED */
62
63 #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
64 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
65 static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
66 #endif
67
68 #ifdef CONFIG_ENV_ADDR_REDUND
69 static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
70
71 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
72 static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
73 #endif /* CONFIG_ENV_ADDR_REDUND */
74
75
76 #ifdef CONFIG_ENV_ADDR_REDUND
77 int env_init(void)
78 {
79         int crc1_ok = 0, crc2_ok = 0;
80
81         uchar flag1 = flash_addr->flags;
82         uchar flag2 = flash_addr_new->flags;
83
84         ulong addr_default = (ulong)&default_environment[0];
85         ulong addr1 = (ulong)&(flash_addr->data);
86         ulong addr2 = (ulong)&(flash_addr_new->data);
87
88         crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
89         crc2_ok =
90                 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
91
92         if (crc1_ok && !crc2_ok) {
93                 gd->env_addr    = addr1;
94                 gd->env_valid   = 1;
95         } else if (!crc1_ok && crc2_ok) {
96                 gd->env_addr    = addr2;
97                 gd->env_valid   = 1;
98         } else if (!crc1_ok && !crc2_ok) {
99                 gd->env_addr    = addr_default;
100                 gd->env_valid   = 0;
101         } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
102                 gd->env_addr    = addr1;
103                 gd->env_valid   = 1;
104         } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
105                 gd->env_addr    = addr2;
106                 gd->env_valid   = 1;
107         } else if (flag1 == flag2) {
108                 gd->env_addr    = addr1;
109                 gd->env_valid   = 2;
110         } else if (flag1 == 0xFF) {
111                 gd->env_addr    = addr1;
112                 gd->env_valid   = 2;
113         } else if (flag2 == 0xFF) {
114                 gd->env_addr    = addr2;
115                 gd->env_valid   = 2;
116         }
117
118         return 0;
119 }
120
121 #ifdef CMD_SAVEENV
122 int saveenv(void)
123 {
124         env_t   env_new;
125         ssize_t len;
126         char    *res, *saved_data = NULL;
127         char    flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
128         int     rc = 1;
129 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
130         ulong   up_data = 0;
131 #endif
132
133         debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
134
135         if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
136                 goto done;
137
138         debug("Protect off %08lX ... %08lX\n",
139                 (ulong)flash_addr_new, end_addr_new);
140
141         if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
142                 goto done;
143
144         res = (char *)&env_new.data;
145         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
146         if (len < 0) {
147                 error("Cannot export environment: errno = %d\n", errno);
148                 goto done;
149         }
150         env_new.crc     = crc32(0, env_new.data, ENV_SIZE);
151         env_new.flags   = new_flag;
152
153 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
154         up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
155         debug("Data to save 0x%lX\n", up_data);
156         if (up_data) {
157                 saved_data = malloc(up_data);
158                 if (saved_data == NULL) {
159                         printf("Unable to save the rest of sector (%ld)\n",
160                                 up_data);
161                         goto done;
162                 }
163                 memcpy(saved_data,
164                         (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
165                         up_data);
166                 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
167                         (long)flash_addr_new + CONFIG_ENV_SIZE,
168                         up_data, saved_data);
169         }
170 #endif
171         puts("Erasing Flash...");
172         debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
173
174         if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
175                 goto done;
176
177         puts("Writing to Flash... ");
178         debug(" %08lX ... %08lX ...",
179                 (ulong)&(flash_addr_new->data),
180                 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
181         rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
182                          sizeof(env_new));
183         if (rc)
184                 goto perror;
185
186         rc = flash_write(&flag, (ulong)&(flash_addr->flags),
187                          sizeof(flash_addr->flags));
188         if (rc)
189                 goto perror;
190
191 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
192         if (up_data) { /* restore the rest of sector */
193                 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
194                         (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
195                 if (flash_write(saved_data,
196                                 (long)flash_addr_new + CONFIG_ENV_SIZE,
197                                 up_data))
198                         goto perror;
199         }
200 #endif
201         puts("done\n");
202
203         {
204                 env_t *etmp = flash_addr;
205                 ulong ltmp = end_addr;
206
207                 flash_addr = flash_addr_new;
208                 flash_addr_new = etmp;
209
210                 end_addr = end_addr_new;
211                 end_addr_new = ltmp;
212         }
213
214         rc = 0;
215         goto done;
216 perror:
217         flash_perror(rc);
218 done:
219         if (saved_data)
220                 free(saved_data);
221         /* try to re-protect */
222         flash_sect_protect(1, (ulong)flash_addr, end_addr);
223         flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
224
225         return rc;
226 }
227 #endif /* CMD_SAVEENV */
228
229 #else /* ! CONFIG_ENV_ADDR_REDUND */
230
231 int env_init(void)
232 {
233         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
234                 gd->env_addr    = (ulong)&(env_ptr->data);
235                 gd->env_valid   = 1;
236                 return 0;
237         }
238
239         gd->env_addr    = (ulong)&default_environment[0];
240         gd->env_valid   = 0;
241         return 0;
242 }
243
244 #ifdef CMD_SAVEENV
245 int saveenv(void)
246 {
247         env_t   env_new;
248         ssize_t len;
249         int     rc = 1;
250         char    *res, *saved_data = NULL;
251 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
252         ulong   up_data = 0;
253
254         up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
255         debug("Data to save 0x%lx\n", up_data);
256         if (up_data) {
257                 saved_data = malloc(up_data);
258                 if (saved_data == NULL) {
259                         printf("Unable to save the rest of sector (%ld)\n",
260                                 up_data);
261                         goto done;
262                 }
263                 memcpy(saved_data,
264                         (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
265                 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
266                         (ulong)flash_addr + CONFIG_ENV_SIZE,
267                         up_data,
268                         (ulong)saved_data);
269         }
270 #endif  /* CONFIG_ENV_SECT_SIZE */
271
272         debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
273
274         if (flash_sect_protect(0, (long)flash_addr, end_addr))
275                 goto done;
276
277         res = (char *)&env_new.data;
278         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
279         if (len < 0) {
280                 error("Cannot export environment: errno = %d\n", errno);
281                 goto done;
282         }
283         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
284
285         puts("Erasing Flash...");
286         if (flash_sect_erase((long)flash_addr, end_addr))
287                 goto done;
288
289         puts("Writing to Flash... ");
290         rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
291         if (rc != 0)
292                 goto perror;
293
294 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
295         if (up_data) {  /* restore the rest of sector */
296                 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
297                         (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
298                 if (flash_write(saved_data,
299                                 (long)flash_addr + CONFIG_ENV_SIZE,
300                                 up_data))
301                         goto perror;
302         }
303 #endif
304         puts("done\n");
305         rc = 0;
306         goto done;
307 perror:
308         flash_perror(rc);
309 done:
310         if (saved_data)
311                 free(saved_data);
312         /* try to re-protect */
313         flash_sect_protect(1, (long)flash_addr, end_addr);
314         return rc;
315 }
316 #endif /* CMD_SAVEENV */
317
318 #endif /* CONFIG_ENV_ADDR_REDUND */
319
320 void env_relocate_spec(void)
321 {
322 #ifdef CONFIG_ENV_ADDR_REDUND
323         if (gd->env_addr != (ulong)&(flash_addr->data)) {
324                 env_t *etmp = flash_addr;
325                 ulong ltmp = end_addr;
326
327                 flash_addr = flash_addr_new;
328                 flash_addr_new = etmp;
329
330                 end_addr = end_addr_new;
331                 end_addr_new = ltmp;
332         }
333
334         if (flash_addr_new->flags != OBSOLETE_FLAG &&
335             crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
336                 char flag = OBSOLETE_FLAG;
337
338                 gd->env_valid = 2;
339                 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
340                 flash_write(&flag,
341                             (ulong)&(flash_addr_new->flags),
342                             sizeof(flash_addr_new->flags));
343                 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
344         }
345
346         if (flash_addr->flags != ACTIVE_FLAG &&
347             (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
348                 char flag = ACTIVE_FLAG;
349
350                 gd->env_valid = 2;
351                 flash_sect_protect(0, (ulong)flash_addr, end_addr);
352                 flash_write(&flag,
353                             (ulong)&(flash_addr->flags),
354                             sizeof(flash_addr->flags));
355                 flash_sect_protect(1, (ulong)flash_addr, end_addr);
356         }
357
358         if (gd->env_valid == 2)
359                 puts("*** Warning - some problems detected "
360                      "reading environment; recovered successfully\n\n");
361 #endif /* CONFIG_ENV_ADDR_REDUND */
362
363         env_import((char *)flash_addr, 1);
364 }