]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/env_flash.c
2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 #include <environment.h>
32 #include <linux/stddef.h>
37 DECLARE_GLOBAL_DATA_PTR
;
39 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
41 #elif defined(CONFIG_ENV_ADDR_REDUND)
42 #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
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
49 char * env_name_spec
= "Flash";
51 #ifdef ENV_IS_EMBEDDED
52 env_t
*env_ptr
= &environment
;
54 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
56 #else /* ! ENV_IS_EMBEDDED */
58 env_t
*env_ptr
= (env_t
*)CONFIG_ENV_ADDR
;
59 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
61 #endif /* ENV_IS_EMBEDDED */
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;
68 #ifdef CONFIG_ENV_ADDR_REDUND
69 static env_t
*flash_addr_new
= (env_t
*)CONFIG_ENV_ADDR_REDUND
;
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 */
76 uchar
env_get_char_spec(int index
)
78 return (*((uchar
*)(gd
->env_addr
+ index
)));
81 #ifdef CONFIG_ENV_ADDR_REDUND
85 int crc1_ok
= 0, crc2_ok
= 0;
87 uchar flag1
= flash_addr
->flags
;
88 uchar flag2
= flash_addr_new
->flags
;
90 ulong addr_default
= (ulong
)&default_environment
[0];
91 ulong addr1
= (ulong
)&(flash_addr
->data
);
92 ulong addr2
= (ulong
)&(flash_addr_new
->data
);
94 crc1_ok
= (crc32(0, flash_addr
->data
, ENV_SIZE
) == flash_addr
->crc
);
95 crc2_ok
= (crc32(0, flash_addr_new
->data
, ENV_SIZE
) == flash_addr_new
->crc
);
97 if (crc1_ok
&& ! crc2_ok
) {
100 } else if (! crc1_ok
&& crc2_ok
) {
101 gd
->env_addr
= addr2
;
103 } else if (! crc1_ok
&& ! crc2_ok
) {
104 gd
->env_addr
= addr_default
;
106 } else if (flag1
== ACTIVE_FLAG
&& flag2
== OBSOLETE_FLAG
) {
107 gd
->env_addr
= addr1
;
109 } else if (flag1
== OBSOLETE_FLAG
&& flag2
== ACTIVE_FLAG
) {
110 gd
->env_addr
= addr2
;
112 } else if (flag1
== flag2
) {
113 gd
->env_addr
= addr1
;
115 } else if (flag1
== 0xFF) {
116 gd
->env_addr
= addr1
;
118 } else if (flag2
== 0xFF) {
119 gd
->env_addr
= addr2
;
131 char *saved_data
= NULL
;
134 char flag
= OBSOLETE_FLAG
, new_flag
= ACTIVE_FLAG
;
135 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
139 debug("Protect off %08lX ... %08lX\n",
140 (ulong
)flash_addr
, end_addr
);
142 if (flash_sect_protect(0, (ulong
)flash_addr
, end_addr
)) {
146 debug("Protect off %08lX ... %08lX\n",
147 (ulong
)flash_addr_new
, end_addr_new
);
149 if (flash_sect_protect(0, (ulong
)flash_addr_new
, end_addr_new
)) {
153 res
= (char *)&env_new
.data
;
154 len
= hexport_r(&env_htab
, '\0', &res
, ENV_SIZE
, 0, NULL
);
156 error("Cannot export environment: errno = %d\n", errno
);
159 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
160 env_new
.flags
= new_flag
;
162 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
163 up_data
= (end_addr_new
+ 1 - ((long)flash_addr_new
+ CONFIG_ENV_SIZE
));
164 debug("Data to save 0x%lX\n", up_data
);
166 if ((saved_data
= malloc(up_data
)) == NULL
) {
167 printf("Unable to save the rest of sector (%ld)\n",
172 (void *)((long)flash_addr_new
+ CONFIG_ENV_SIZE
), up_data
);
173 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
174 (long)flash_addr_new
+ CONFIG_ENV_SIZE
,
175 up_data
, saved_data
);
178 puts("Erasing Flash...");
179 debug(" %08lX ... %08lX ...",
180 (ulong
)flash_addr_new
, end_addr_new
);
182 if (flash_sect_erase((ulong
)flash_addr_new
, end_addr_new
)) {
186 puts("Writing to Flash... ");
187 debug(" %08lX ... %08lX ...",
188 (ulong
)&(flash_addr_new
->data
),
189 sizeof(env_ptr
->data
)+(ulong
)&(flash_addr_new
->data
));
190 if ((rc
= flash_write((char *)&env_new
,
191 (ulong
)flash_addr_new
,
193 (rc
= flash_write(&flag
,
194 (ulong
)&(flash_addr
->flags
),
195 sizeof(flash_addr
->flags
))) ) {
200 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
201 if (up_data
) { /* restore the rest of sector */
202 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
203 (long)flash_addr_new
+ CONFIG_ENV_SIZE
, up_data
);
204 if (flash_write(saved_data
,
205 (long)flash_addr_new
+ CONFIG_ENV_SIZE
,
215 env_t
* etmp
= flash_addr
;
216 ulong ltmp
= end_addr
;
218 flash_addr
= flash_addr_new
;
219 flash_addr_new
= etmp
;
221 end_addr
= end_addr_new
;
229 /* try to re-protect */
230 (void) flash_sect_protect(1, (ulong
)flash_addr
, end_addr
);
231 (void) flash_sect_protect(1, (ulong
)flash_addr_new
, end_addr_new
);
235 #endif /* CMD_SAVEENV */
237 #else /* ! CONFIG_ENV_ADDR_REDUND */
241 if (crc32(0, env_ptr
->data
, ENV_SIZE
) == env_ptr
->crc
) {
242 gd
->env_addr
= (ulong
)&(env_ptr
->data
);
247 gd
->env_addr
= (ulong
)&default_environment
[0];
260 char *saved_data
= NULL
;
261 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
264 up_data
= (end_addr
+ 1 - ((long)flash_addr
+ CONFIG_ENV_SIZE
));
265 debug("Data to save 0x%lx\n", up_data
);
267 if ((saved_data
= malloc(up_data
)) == NULL
) {
268 printf("Unable to save the rest of sector (%ld)\n",
273 (void *)((long)flash_addr
+ CONFIG_ENV_SIZE
), up_data
);
274 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
275 (ulong
)flash_addr
+ CONFIG_ENV_SIZE
,
279 #endif /* CONFIG_ENV_SECT_SIZE */
281 debug("Protect off %08lX ... %08lX\n",
282 (ulong
)flash_addr
, end_addr
);
284 if (flash_sect_protect(0, (long)flash_addr
, end_addr
))
287 res
= (char *)&env_new
.data
;
288 len
= hexport_r(&env_htab
, '\0', &res
, ENV_SIZE
, 0, NULL
);
290 error("Cannot export environment: errno = %d\n", errno
);
293 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
295 puts("Erasing Flash...");
296 if (flash_sect_erase((long)flash_addr
, end_addr
))
299 puts("Writing to Flash... ");
300 rc
= flash_write((char *)&env_new
, (long)flash_addr
, CONFIG_ENV_SIZE
);
305 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
306 if (up_data
) { /* restore the rest of sector */
307 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
308 (ulong
)flash_addr
+ CONFIG_ENV_SIZE
, up_data
);
309 if (flash_write(saved_data
,
310 (long)flash_addr
+ CONFIG_ENV_SIZE
,
322 /* try to re-protect */
323 (void) flash_sect_protect(1, (long)flash_addr
, end_addr
);
327 #endif /* CMD_SAVEENV */
329 #endif /* CONFIG_ENV_ADDR_REDUND */
331 void env_relocate_spec(void)
333 #ifdef CONFIG_ENV_ADDR_REDUND
334 if (gd
->env_addr
!= (ulong
)&(flash_addr
->data
)) {
335 env_t
*etmp
= flash_addr
;
336 ulong ltmp
= end_addr
;
338 flash_addr
= flash_addr_new
;
339 flash_addr_new
= etmp
;
341 end_addr
= end_addr_new
;
345 if (flash_addr_new
->flags
!= OBSOLETE_FLAG
&&
346 crc32(0, flash_addr_new
->data
, ENV_SIZE
) ==
347 flash_addr_new
->crc
) {
348 char flag
= OBSOLETE_FLAG
;
351 flash_sect_protect(0, (ulong
)flash_addr_new
, end_addr_new
);
353 (ulong
)&(flash_addr_new
->flags
),
354 sizeof(flash_addr_new
->flags
));
355 flash_sect_protect(1, (ulong
)flash_addr_new
, end_addr_new
);
358 if (flash_addr
->flags
!= ACTIVE_FLAG
&&
359 (flash_addr
->flags
& ACTIVE_FLAG
) == ACTIVE_FLAG
) {
360 char flag
= ACTIVE_FLAG
;
363 flash_sect_protect(0, (ulong
)flash_addr
, end_addr
);
365 (ulong
)&(flash_addr
->flags
),
366 sizeof(flash_addr
->flags
));
367 flash_sect_protect(1, (ulong
)flash_addr
, end_addr
);
370 if (gd
->env_valid
== 2)
371 puts ("*** Warning - some problems detected "
372 "reading environment; recovered successfully\n\n");
373 #endif /* CONFIG_ENV_ADDR_REDUND */
375 env_import((char *)flash_addr
, 1);