]>
Commit | Line | Data |
---|---|---|
c609719b | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
c609719b WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
6 | * Andreas Heppel <[email protected]> | |
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> | |
c609719b WD |
30 | #include <command.h> |
31 | #include <environment.h> | |
c609719b | 32 | #include <linux/stddef.h> |
47cd00fa | 33 | #include <malloc.h> |
ea882baf WD |
34 | #include <search.h> |
35 | #include <errno.h> | |
c609719b | 36 | |
d87080b7 WD |
37 | DECLARE_GLOBAL_DATA_PTR; |
38 | ||
bdab39d3 | 39 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) |
c609719b | 40 | #define CMD_SAVEENV |
0e8d1586 | 41 | #elif defined(CONFIG_ENV_ADDR_REDUND) |
82b54b97 | 42 | #error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH |
c609719b WD |
43 | #endif |
44 | ||
82b54b97 IG |
45 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
46 | (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE) | |
0e8d1586 | 47 | #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE |
c609719b WD |
48 | #endif |
49 | ||
82b54b97 | 50 | char *env_name_spec = "Flash"; |
c609719b WD |
51 | |
52 | #ifdef ENV_IS_EMBEDDED | |
994bc671 | 53 | env_t *env_ptr = &environment; |
c609719b | 54 | |
0e8d1586 | 55 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
c609719b WD |
56 | |
57 | #else /* ! ENV_IS_EMBEDDED */ | |
58 | ||
0e8d1586 | 59 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
0e8d1586 | 60 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
c609719b WD |
61 | #endif /* ENV_IS_EMBEDDED */ |
62 | ||
ea882baf WD |
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 | ||
0e8d1586 JCPV |
68 | #ifdef CONFIG_ENV_ADDR_REDUND |
69 | static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; | |
c609719b | 70 | |
ea882baf | 71 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
0e8d1586 | 72 | static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; |
0e8d1586 | 73 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
c609719b | 74 | |
c609719b | 75 | |
0e8d1586 | 76 | #ifdef CONFIG_ENV_ADDR_REDUND |
82b54b97 | 77 | int env_init(void) |
c609719b | 78 | { |
8ed96046 | 79 | int crc1_ok = 0, crc2_ok = 0; |
c609719b WD |
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 | ||
82b54b97 IG |
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; | |
c3f9d493 | 101 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
82b54b97 IG |
102 | gd->env_addr = addr1; |
103 | gd->env_valid = 1; | |
c3f9d493 | 104 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
82b54b97 IG |
105 | gd->env_addr = addr2; |
106 | gd->env_valid = 1; | |
c3f9d493 | 107 | } else if (flag1 == flag2) { |
82b54b97 IG |
108 | gd->env_addr = addr1; |
109 | gd->env_valid = 2; | |
c3f9d493 | 110 | } else if (flag1 == 0xFF) { |
82b54b97 IG |
111 | gd->env_addr = addr1; |
112 | gd->env_valid = 2; | |
c3f9d493 | 113 | } else if (flag2 == 0xFF) { |
82b54b97 IG |
114 | gd->env_addr = addr2; |
115 | gd->env_valid = 2; | |
c609719b WD |
116 | } |
117 | ||
ea882baf | 118 | return 0; |
c609719b WD |
119 | } |
120 | ||
121 | #ifdef CMD_SAVEENV | |
122 | int saveenv(void) | |
123 | { | |
ea882baf WD |
124 | env_t env_new; |
125 | ssize_t len; | |
82b54b97 | 126 | char *res, *saved_data = NULL; |
ea882baf | 127 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
82b54b97 | 128 | int rc = 1; |
0e8d1586 | 129 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
ea882baf | 130 | ulong up_data = 0; |
500545cc | 131 | #endif |
c609719b | 132 | |
82b54b97 | 133 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
c609719b | 134 | |
82b54b97 | 135 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
ea882baf | 136 | goto done; |
c609719b | 137 | |
ea882baf | 138 | debug("Protect off %08lX ... %08lX\n", |
c609719b WD |
139 | (ulong)flash_addr_new, end_addr_new); |
140 | ||
82b54b97 | 141 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
ea882baf | 142 | goto done; |
ea882baf WD |
143 | |
144 | res = (char *)&env_new.data; | |
37f2fe74 | 145 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
146 | if (len < 0) { |
147 | error("Cannot export environment: errno = %d\n", errno); | |
148 | goto done; | |
c609719b | 149 | } |
82b54b97 IG |
150 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
151 | env_new.flags = new_flag; | |
c609719b | 152 | |
0e8d1586 | 153 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
82b54b97 | 154 | up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE); |
ea882baf | 155 | debug("Data to save 0x%lX\n", up_data); |
47cd00fa | 156 | if (up_data) { |
82b54b97 IG |
157 | saved_data = malloc(up_data); |
158 | if (saved_data == NULL) { | |
8bde7f77 | 159 | printf("Unable to save the rest of sector (%ld)\n", |
47cd00fa | 160 | up_data); |
ea882baf | 161 | goto done; |
47cd00fa | 162 | } |
8bde7f77 | 163 | memcpy(saved_data, |
82b54b97 IG |
164 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
165 | up_data); | |
ea882baf WD |
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); | |
47cd00fa WD |
169 | } |
170 | #endif | |
ea882baf | 171 | puts("Erasing Flash..."); |
82b54b97 | 172 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
c609719b | 173 | |
82b54b97 | 174 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
ea882baf | 175 | goto done; |
c609719b | 176 | |
ea882baf WD |
177 | puts("Writing to Flash... "); |
178 | debug(" %08lX ... %08lX ...", | |
c609719b | 179 | (ulong)&(flash_addr_new->data), |
82b54b97 IG |
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; | |
c609719b | 190 | |
0e8d1586 | 191 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
47cd00fa | 192 | if (up_data) { /* restore the rest of sector */ |
ea882baf WD |
193 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
194 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); | |
8bde7f77 | 195 | if (flash_write(saved_data, |
0e8d1586 | 196 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
82b54b97 IG |
197 | up_data)) |
198 | goto perror; | |
47cd00fa WD |
199 | } |
200 | #endif | |
ea882baf WD |
201 | puts("done\n"); |
202 | ||
c609719b | 203 | { |
82b54b97 | 204 | env_t *etmp = flash_addr; |
c609719b WD |
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; | |
82b54b97 IG |
215 | goto done; |
216 | perror: | |
217 | flash_perror(rc); | |
ea882baf | 218 | done: |
47cd00fa | 219 | if (saved_data) |
ea882baf | 220 | free(saved_data); |
c609719b | 221 | /* try to re-protect */ |
82b54b97 IG |
222 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
223 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); | |
c609719b WD |
224 | |
225 | return rc; | |
226 | } | |
227 | #endif /* CMD_SAVEENV */ | |
228 | ||
0e8d1586 | 229 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
c609719b | 230 | |
82b54b97 | 231 | int env_init(void) |
c609719b | 232 | { |
c609719b | 233 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
82b54b97 IG |
234 | gd->env_addr = (ulong)&(env_ptr->data); |
235 | gd->env_valid = 1; | |
236 | return 0; | |
c609719b | 237 | } |
9bb8b209 | 238 | |
82b54b97 IG |
239 | gd->env_addr = (ulong)&default_environment[0]; |
240 | gd->env_valid = 0; | |
ea882baf | 241 | return 0; |
c609719b WD |
242 | } |
243 | ||
244 | #ifdef CMD_SAVEENV | |
c609719b WD |
245 | int saveenv(void) |
246 | { | |
ea882baf WD |
247 | env_t env_new; |
248 | ssize_t len; | |
249 | int rc = 1; | |
82b54b97 | 250 | char *res, *saved_data = NULL; |
ea882baf WD |
251 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
252 | ulong up_data = 0; | |
c609719b | 253 | |
82b54b97 | 254 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
ea882baf WD |
255 | debug("Data to save 0x%lx\n", up_data); |
256 | if (up_data) { | |
82b54b97 IG |
257 | saved_data = malloc(up_data); |
258 | if (saved_data == NULL) { | |
ea882baf WD |
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 | } | |
0e8d1586 | 270 | #endif /* CONFIG_ENV_SECT_SIZE */ |
c609719b | 271 | |
82b54b97 | 272 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
c609719b | 273 | |
ea882baf WD |
274 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
275 | goto done; | |
c609719b | 276 | |
ea882baf | 277 | res = (char *)&env_new.data; |
37f2fe74 | 278 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
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); | |
c609719b | 284 | |
ea882baf WD |
285 | puts("Erasing Flash..."); |
286 | if (flash_sect_erase((long)flash_addr, end_addr)) | |
287 | goto done; | |
c609719b | 288 | |
ea882baf WD |
289 | puts("Writing to Flash... "); |
290 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); | |
82b54b97 IG |
291 | if (rc != 0) |
292 | goto perror; | |
293 | ||
ea882baf WD |
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, | |
82b54b97 IG |
300 | up_data)) |
301 | goto perror; | |
ea882baf WD |
302 | } |
303 | #endif | |
304 | puts("done\n"); | |
305 | rc = 0; | |
82b54b97 IG |
306 | goto done; |
307 | perror: | |
308 | flash_perror(rc); | |
ea882baf WD |
309 | done: |
310 | if (saved_data) | |
311 | free(saved_data); | |
c609719b | 312 | /* try to re-protect */ |
82b54b97 | 313 | flash_sect_protect(1, (long)flash_addr, end_addr); |
ea882baf | 314 | return rc; |
c609719b | 315 | } |
c609719b WD |
316 | #endif /* CMD_SAVEENV */ |
317 | ||
0e8d1586 | 318 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
c609719b | 319 | |
ea882baf | 320 | void env_relocate_spec(void) |
c609719b | 321 | { |
0e8d1586 | 322 | #ifdef CONFIG_ENV_ADDR_REDUND |
c3f9d493 | 323 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
ea882baf | 324 | env_t *etmp = flash_addr; |
c609719b WD |
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 | ||
c3f9d493 | 334 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
82b54b97 | 335 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
c3f9d493 WD |
336 | char flag = OBSOLETE_FLAG; |
337 | ||
c609719b | 338 | gd->env_valid = 2; |
ea882baf | 339 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
c3f9d493 | 340 | flash_write(&flag, |
8bde7f77 WD |
341 | (ulong)&(flash_addr_new->flags), |
342 | sizeof(flash_addr_new->flags)); | |
ea882baf | 343 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
c609719b WD |
344 | } |
345 | ||
c3f9d493 WD |
346 | if (flash_addr->flags != ACTIVE_FLAG && |
347 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { | |
348 | char flag = ACTIVE_FLAG; | |
349 | ||
c609719b | 350 | gd->env_valid = 2; |
ea882baf | 351 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
c3f9d493 | 352 | flash_write(&flag, |
8bde7f77 WD |
353 | (ulong)&(flash_addr->flags), |
354 | sizeof(flash_addr->flags)); | |
ea882baf | 355 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
c609719b WD |
356 | } |
357 | ||
358 | if (gd->env_valid == 2) | |
82b54b97 IG |
359 | puts("*** Warning - some problems detected " |
360 | "reading environment; recovered successfully\n\n"); | |
0e8d1586 | 361 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
ea882baf WD |
362 | |
363 | env_import((char *)flash_addr, 1); | |
c609719b | 364 | } |