]>
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 CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
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
50 char *env_name_spec
= "Flash";
52 #ifdef ENV_IS_EMBEDDED
53 env_t
*env_ptr
= &environment
;
55 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
57 #else /* ! ENV_IS_EMBEDDED */
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 */
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 #ifdef CONFIG_ENV_ADDR_REDUND
79 int crc1_ok
= 0, crc2_ok
= 0;
81 uchar flag1
= flash_addr
->flags
;
82 uchar flag2
= flash_addr_new
->flags
;
84 ulong addr_default
= (ulong
)&default_environment
[0];
85 ulong addr1
= (ulong
)&(flash_addr
->data
);
86 ulong addr2
= (ulong
)&(flash_addr_new
->data
);
88 crc1_ok
= crc32(0, flash_addr
->data
, ENV_SIZE
) == flash_addr
->crc
;
90 crc32(0, flash_addr_new
->data
, ENV_SIZE
) == flash_addr_new
->crc
;
92 if (crc1_ok
&& !crc2_ok
) {
95 } else if (!crc1_ok
&& crc2_ok
) {
98 } else if (!crc1_ok
&& !crc2_ok
) {
99 gd
->env_addr
= addr_default
;
101 } else if (flag1
== ACTIVE_FLAG
&& flag2
== OBSOLETE_FLAG
) {
102 gd
->env_addr
= addr1
;
104 } else if (flag1
== OBSOLETE_FLAG
&& flag2
== ACTIVE_FLAG
) {
105 gd
->env_addr
= addr2
;
107 } else if (flag1
== flag2
) {
108 gd
->env_addr
= addr1
;
110 } else if (flag1
== 0xFF) {
111 gd
->env_addr
= addr1
;
113 } else if (flag2
== 0xFF) {
114 gd
->env_addr
= addr2
;
126 char *res
, *saved_data
= NULL
;
127 char flag
= OBSOLETE_FLAG
, new_flag
= ACTIVE_FLAG
;
129 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
133 debug("Protect off %08lX ... %08lX\n", (ulong
)flash_addr
, end_addr
);
135 if (flash_sect_protect(0, (ulong
)flash_addr
, end_addr
))
138 debug("Protect off %08lX ... %08lX\n",
139 (ulong
)flash_addr_new
, end_addr_new
);
141 if (flash_sect_protect(0, (ulong
)flash_addr_new
, end_addr_new
))
144 res
= (char *)&env_new
.data
;
145 len
= hexport_r(&env_htab
, '\0', 0, &res
, ENV_SIZE
, 0, NULL
);
147 error("Cannot export environment: errno = %d\n", errno
);
150 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
151 env_new
.flags
= new_flag
;
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
);
157 saved_data
= malloc(up_data
);
158 if (saved_data
== NULL
) {
159 printf("Unable to save the rest of sector (%ld)\n",
164 (void *)((long)flash_addr_new
+ CONFIG_ENV_SIZE
),
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
);
171 puts("Erasing Flash...");
172 debug(" %08lX ... %08lX ...", (ulong
)flash_addr_new
, end_addr_new
);
174 if (flash_sect_erase((ulong
)flash_addr_new
, end_addr_new
))
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
,
186 rc
= flash_write(&flag
, (ulong
)&(flash_addr
->flags
),
187 sizeof(flash_addr
->flags
));
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
,
204 env_t
*etmp
= flash_addr
;
205 ulong ltmp
= end_addr
;
207 flash_addr
= flash_addr_new
;
208 flash_addr_new
= etmp
;
210 end_addr
= end_addr_new
;
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
);
227 #endif /* CMD_SAVEENV */
229 #else /* ! CONFIG_ENV_ADDR_REDUND */
233 if (crc32(0, env_ptr
->data
, ENV_SIZE
) == env_ptr
->crc
) {
234 gd
->env_addr
= (ulong
)&(env_ptr
->data
);
239 gd
->env_addr
= (ulong
)&default_environment
[0];
250 char *res
, *saved_data
= NULL
;
251 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
254 up_data
= end_addr
+ 1 - ((long)flash_addr
+ CONFIG_ENV_SIZE
);
255 debug("Data to save 0x%lx\n", up_data
);
257 saved_data
= malloc(up_data
);
258 if (saved_data
== NULL
) {
259 printf("Unable to save the rest of sector (%ld)\n",
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
,
270 #endif /* CONFIG_ENV_SECT_SIZE */
272 debug("Protect off %08lX ... %08lX\n", (ulong
)flash_addr
, end_addr
);
274 if (flash_sect_protect(0, (long)flash_addr
, end_addr
))
277 res
= (char *)&env_new
.data
;
278 len
= hexport_r(&env_htab
, '\0', 0, &res
, ENV_SIZE
, 0, NULL
);
280 error("Cannot export environment: errno = %d\n", errno
);
283 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
285 puts("Erasing Flash...");
286 if (flash_sect_erase((long)flash_addr
, end_addr
))
289 puts("Writing to Flash... ");
290 rc
= flash_write((char *)&env_new
, (long)flash_addr
, CONFIG_ENV_SIZE
);
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
,
312 /* try to re-protect */
313 flash_sect_protect(1, (long)flash_addr
, end_addr
);
316 #endif /* CMD_SAVEENV */
318 #endif /* CONFIG_ENV_ADDR_REDUND */
320 void env_relocate_spec(void)
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
;
327 flash_addr
= flash_addr_new
;
328 flash_addr_new
= etmp
;
330 end_addr
= end_addr_new
;
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
;
339 flash_sect_protect(0, (ulong
)flash_addr_new
, end_addr_new
);
341 (ulong
)&(flash_addr_new
->flags
),
342 sizeof(flash_addr_new
->flags
));
343 flash_sect_protect(1, (ulong
)flash_addr_new
, end_addr_new
);
346 if (flash_addr
->flags
!= ACTIVE_FLAG
&&
347 (flash_addr
->flags
& ACTIVE_FLAG
) == ACTIVE_FLAG
) {
348 char flag
= ACTIVE_FLAG
;
351 flash_sect_protect(0, (ulong
)flash_addr
, end_addr
);
353 (ulong
)&(flash_addr
->flags
),
354 sizeof(flash_addr
->flags
));
355 flash_sect_protect(1, (ulong
)flash_addr
, end_addr
);
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 */
363 env_import((char *)flash_addr
, 1);