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