]>
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 * SPDX-License-Identifier: GPL-2.0+
15 #include <environment.h>
16 #include <linux/stddef.h>
21 DECLARE_GLOBAL_DATA_PTR
;
23 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
25 #elif defined(CONFIG_ENV_ADDR_REDUND)
26 #error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
29 #if defined(CONFIG_ENV_SIZE_REDUND) && \
30 (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
31 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
34 char *env_name_spec
= "Flash";
36 #ifdef ENV_IS_EMBEDDED
37 env_t
*env_ptr
= &environment
;
39 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
41 #else /* ! ENV_IS_EMBEDDED */
43 env_t
*env_ptr
= (env_t
*)CONFIG_ENV_ADDR
;
44 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
45 #endif /* ENV_IS_EMBEDDED */
47 #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
48 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
49 static ulong end_addr
= CONFIG_ENV_ADDR
+ CONFIG_ENV_SECT_SIZE
- 1;
52 #ifdef CONFIG_ENV_ADDR_REDUND
53 static env_t
*flash_addr_new
= (env_t
*)CONFIG_ENV_ADDR_REDUND
;
55 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
56 static ulong end_addr_new
= CONFIG_ENV_ADDR_REDUND
+ CONFIG_ENV_SECT_SIZE
- 1;
57 #endif /* CONFIG_ENV_ADDR_REDUND */
60 #ifdef CONFIG_ENV_ADDR_REDUND
63 int crc1_ok
= 0, crc2_ok
= 0;
65 uchar flag1
= flash_addr
->flags
;
66 uchar flag2
= flash_addr_new
->flags
;
68 ulong addr_default
= (ulong
)&default_environment
[0];
69 ulong addr1
= (ulong
)&(flash_addr
->data
);
70 ulong addr2
= (ulong
)&(flash_addr_new
->data
);
72 crc1_ok
= crc32(0, flash_addr
->data
, ENV_SIZE
) == flash_addr
->crc
;
74 crc32(0, flash_addr_new
->data
, ENV_SIZE
) == flash_addr_new
->crc
;
76 if (crc1_ok
&& !crc2_ok
) {
79 } else if (!crc1_ok
&& crc2_ok
) {
82 } else if (!crc1_ok
&& !crc2_ok
) {
83 gd
->env_addr
= addr_default
;
85 } else if (flag1
== ACTIVE_FLAG
&& flag2
== OBSOLETE_FLAG
) {
88 } else if (flag1
== OBSOLETE_FLAG
&& flag2
== ACTIVE_FLAG
) {
91 } else if (flag1
== flag2
) {
94 } else if (flag1
== 0xFF) {
97 } else if (flag2
== 0xFF) {
110 char *res
, *saved_data
= NULL
;
111 char flag
= OBSOLETE_FLAG
, new_flag
= ACTIVE_FLAG
;
113 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
117 debug("Protect off %08lX ... %08lX\n", (ulong
)flash_addr
, end_addr
);
119 if (flash_sect_protect(0, (ulong
)flash_addr
, end_addr
))
122 debug("Protect off %08lX ... %08lX\n",
123 (ulong
)flash_addr_new
, end_addr_new
);
125 if (flash_sect_protect(0, (ulong
)flash_addr_new
, end_addr_new
))
128 res
= (char *)&env_new
.data
;
129 len
= hexport_r(&env_htab
, '\0', 0, &res
, ENV_SIZE
, 0, NULL
);
131 error("Cannot export environment: errno = %d\n", errno
);
134 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
135 env_new
.flags
= new_flag
;
137 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
138 up_data
= end_addr_new
+ 1 - ((long)flash_addr_new
+ CONFIG_ENV_SIZE
);
139 debug("Data to save 0x%lX\n", up_data
);
141 saved_data
= malloc(up_data
);
142 if (saved_data
== NULL
) {
143 printf("Unable to save the rest of sector (%ld)\n",
148 (void *)((long)flash_addr_new
+ CONFIG_ENV_SIZE
),
150 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
151 (long)flash_addr_new
+ CONFIG_ENV_SIZE
,
152 up_data
, saved_data
);
155 puts("Erasing Flash...");
156 debug(" %08lX ... %08lX ...", (ulong
)flash_addr_new
, end_addr_new
);
158 if (flash_sect_erase((ulong
)flash_addr_new
, end_addr_new
))
161 puts("Writing to Flash... ");
162 debug(" %08lX ... %08lX ...",
163 (ulong
)&(flash_addr_new
->data
),
164 sizeof(env_ptr
->data
) + (ulong
)&(flash_addr_new
->data
));
165 rc
= flash_write((char *)&env_new
, (ulong
)flash_addr_new
,
170 rc
= flash_write(&flag
, (ulong
)&(flash_addr
->flags
),
171 sizeof(flash_addr
->flags
));
175 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
176 if (up_data
) { /* restore the rest of sector */
177 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
178 (long)flash_addr_new
+ CONFIG_ENV_SIZE
, up_data
);
179 if (flash_write(saved_data
,
180 (long)flash_addr_new
+ CONFIG_ENV_SIZE
,
188 env_t
*etmp
= flash_addr
;
189 ulong ltmp
= end_addr
;
191 flash_addr
= flash_addr_new
;
192 flash_addr_new
= etmp
;
194 end_addr
= end_addr_new
;
205 /* try to re-protect */
206 flash_sect_protect(1, (ulong
)flash_addr
, end_addr
);
207 flash_sect_protect(1, (ulong
)flash_addr_new
, end_addr_new
);
211 #endif /* CMD_SAVEENV */
213 #else /* ! CONFIG_ENV_ADDR_REDUND */
217 if (crc32(0, env_ptr
->data
, ENV_SIZE
) == env_ptr
->crc
) {
218 gd
->env_addr
= (ulong
)&(env_ptr
->data
);
223 gd
->env_addr
= (ulong
)&default_environment
[0];
234 char *res
, *saved_data
= NULL
;
235 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
238 up_data
= end_addr
+ 1 - ((long)flash_addr
+ CONFIG_ENV_SIZE
);
239 debug("Data to save 0x%lx\n", up_data
);
241 saved_data
= malloc(up_data
);
242 if (saved_data
== NULL
) {
243 printf("Unable to save the rest of sector (%ld)\n",
248 (void *)((long)flash_addr
+ CONFIG_ENV_SIZE
), up_data
);
249 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
250 (ulong
)flash_addr
+ CONFIG_ENV_SIZE
,
254 #endif /* CONFIG_ENV_SECT_SIZE */
256 debug("Protect off %08lX ... %08lX\n", (ulong
)flash_addr
, end_addr
);
258 if (flash_sect_protect(0, (long)flash_addr
, end_addr
))
261 res
= (char *)&env_new
.data
;
262 len
= hexport_r(&env_htab
, '\0', 0, &res
, ENV_SIZE
, 0, NULL
);
264 error("Cannot export environment: errno = %d\n", errno
);
267 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
269 puts("Erasing Flash...");
270 if (flash_sect_erase((long)flash_addr
, end_addr
))
273 puts("Writing to Flash... ");
274 rc
= flash_write((char *)&env_new
, (long)flash_addr
, CONFIG_ENV_SIZE
);
278 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
279 if (up_data
) { /* restore the rest of sector */
280 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
281 (ulong
)flash_addr
+ CONFIG_ENV_SIZE
, up_data
);
282 if (flash_write(saved_data
,
283 (long)flash_addr
+ CONFIG_ENV_SIZE
,
296 /* try to re-protect */
297 flash_sect_protect(1, (long)flash_addr
, end_addr
);
300 #endif /* CMD_SAVEENV */
302 #endif /* CONFIG_ENV_ADDR_REDUND */
304 void env_relocate_spec(void)
306 #ifdef CONFIG_ENV_ADDR_REDUND
307 if (gd
->env_addr
!= (ulong
)&(flash_addr
->data
)) {
308 env_t
*etmp
= flash_addr
;
309 ulong ltmp
= end_addr
;
311 flash_addr
= flash_addr_new
;
312 flash_addr_new
= etmp
;
314 end_addr
= end_addr_new
;
318 if (flash_addr_new
->flags
!= OBSOLETE_FLAG
&&
319 crc32(0, flash_addr_new
->data
, ENV_SIZE
) == flash_addr_new
->crc
) {
320 char flag
= OBSOLETE_FLAG
;
323 flash_sect_protect(0, (ulong
)flash_addr_new
, end_addr_new
);
325 (ulong
)&(flash_addr_new
->flags
),
326 sizeof(flash_addr_new
->flags
));
327 flash_sect_protect(1, (ulong
)flash_addr_new
, end_addr_new
);
330 if (flash_addr
->flags
!= ACTIVE_FLAG
&&
331 (flash_addr
->flags
& ACTIVE_FLAG
) == ACTIVE_FLAG
) {
332 char flag
= ACTIVE_FLAG
;
335 flash_sect_protect(0, (ulong
)flash_addr
, end_addr
);
337 (ulong
)&(flash_addr
->flags
),
338 sizeof(flash_addr
->flags
));
339 flash_sect_protect(1, (ulong
)flash_addr
, end_addr
);
342 if (gd
->env_valid
== 2)
343 puts("*** Warning - some problems detected "
344 "reading environment; recovered successfully\n\n");
345 #endif /* CONFIG_ENV_ADDR_REDUND */
347 env_import((char *)flash_addr
, 1);