2 * (C) Copyright 2000-2002
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>
35 DECLARE_GLOBAL_DATA_PTR
;
37 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
39 #elif defined(CONFIG_ENV_ADDR_REDUND)
40 #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
43 #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
44 #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
48 # ifdef CONFIG_ENV_ADDR_REDUND
49 #error CONFIG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
53 char * env_name_spec
= "Flash";
55 #ifdef ENV_IS_EMBEDDED
57 extern uchar environment
[];
58 env_t
*env_ptr
= (env_t
*)(&environment
[0]);
61 /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
62 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
65 #else /* ! ENV_IS_EMBEDDED */
67 env_t
*env_ptr
= (env_t
*)CONFIG_ENV_ADDR
;
69 static env_t
*flash_addr
= (env_t
*)CONFIG_ENV_ADDR
;
72 #endif /* ENV_IS_EMBEDDED */
74 #ifdef CONFIG_ENV_ADDR_REDUND
75 static env_t
*flash_addr_new
= (env_t
*)CONFIG_ENV_ADDR_REDUND
;
77 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
78 static ulong end_addr
= CONFIG_ENV_ADDR
+ CONFIG_ENV_SECT_SIZE
- 1;
79 static ulong end_addr_new
= CONFIG_ENV_ADDR_REDUND
+ CONFIG_ENV_SECT_SIZE
- 1;
82 #define OBSOLETE_FLAG 0
83 #endif /* CONFIG_ENV_ADDR_REDUND */
85 extern uchar default_environment
[];
86 extern int default_environment_size
;
89 uchar
env_get_char_spec (int index
)
91 return ( *((uchar
*)(gd
->env_addr
+ index
)) );
94 #ifdef CONFIG_ENV_ADDR_REDUND
98 int crc1_ok
= 0, crc2_ok
= 0;
100 uchar flag1
= flash_addr
->flags
;
101 uchar flag2
= flash_addr_new
->flags
;
103 ulong addr_default
= (ulong
)&default_environment
[0];
104 ulong addr1
= (ulong
)&(flash_addr
->data
);
105 ulong addr2
= (ulong
)&(flash_addr_new
->data
);
107 crc1_ok
= (crc32(0, flash_addr
->data
, ENV_SIZE
) == flash_addr
->crc
);
108 crc2_ok
= (crc32(0, flash_addr_new
->data
, ENV_SIZE
) == flash_addr_new
->crc
);
110 if (crc1_ok
&& ! crc2_ok
) {
111 gd
->env_addr
= addr1
;
113 } else if (! crc1_ok
&& crc2_ok
) {
114 gd
->env_addr
= addr2
;
116 } else if (! crc1_ok
&& ! crc2_ok
) {
117 gd
->env_addr
= addr_default
;
119 } else if (flag1
== ACTIVE_FLAG
&& flag2
== OBSOLETE_FLAG
) {
120 gd
->env_addr
= addr1
;
122 } else if (flag1
== OBSOLETE_FLAG
&& flag2
== ACTIVE_FLAG
) {
123 gd
->env_addr
= addr2
;
125 } else if (flag1
== flag2
) {
126 gd
->env_addr
= addr1
;
128 } else if (flag1
== 0xFF) {
129 gd
->env_addr
= addr1
;
131 } else if (flag2
== 0xFF) {
132 gd
->env_addr
= addr2
;
142 char *saved_data
= NULL
;
144 char flag
= OBSOLETE_FLAG
, new_flag
= ACTIVE_FLAG
;
145 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
149 debug ("Protect off %08lX ... %08lX\n",
150 (ulong
)flash_addr
, end_addr
);
152 if (flash_sect_protect (0, (ulong
)flash_addr
, end_addr
)) {
156 debug ("Protect off %08lX ... %08lX\n",
157 (ulong
)flash_addr_new
, end_addr_new
);
159 if (flash_sect_protect (0, (ulong
)flash_addr_new
, end_addr_new
)) {
163 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
164 up_data
= (end_addr_new
+ 1 - ((long)flash_addr_new
+ CONFIG_ENV_SIZE
));
165 debug ("Data to save 0x%x\n", up_data
);
167 if ((saved_data
= malloc(up_data
)) == NULL
) {
168 printf("Unable to save the rest of sector (%ld)\n",
173 (void *)((long)flash_addr_new
+ CONFIG_ENV_SIZE
), up_data
);
174 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
175 (long)flash_addr_new
+ CONFIG_ENV_SIZE
,
176 up_data
, saved_data
);
179 puts ("Erasing Flash...");
180 debug (" %08lX ... %08lX ...",
181 (ulong
)flash_addr_new
, end_addr_new
);
183 if (flash_sect_erase ((ulong
)flash_addr_new
, end_addr_new
)) {
187 puts ("Writing to Flash... ");
188 debug (" %08lX ... %08lX ...",
189 (ulong
)&(flash_addr_new
->data
),
190 sizeof(env_ptr
->data
)+(ulong
)&(flash_addr_new
->data
));
191 if ((rc
= flash_write((char *)env_ptr
->data
,
192 (ulong
)&(flash_addr_new
->data
),
193 sizeof(env_ptr
->data
))) ||
194 (rc
= flash_write((char *)&(env_ptr
->crc
),
195 (ulong
)&(flash_addr_new
->crc
),
196 sizeof(env_ptr
->crc
))) ||
197 (rc
= flash_write(&flag
,
198 (ulong
)&(flash_addr
->flags
),
199 sizeof(flash_addr
->flags
))) ||
200 (rc
= flash_write(&new_flag
,
201 (ulong
)&(flash_addr_new
->flags
),
202 sizeof(flash_addr_new
->flags
))))
209 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
210 if (up_data
) { /* restore the rest of sector */
211 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
212 (long)flash_addr_new
+ CONFIG_ENV_SIZE
, up_data
);
213 if (flash_write(saved_data
,
214 (long)flash_addr_new
+ CONFIG_ENV_SIZE
,
222 env_t
* etmp
= flash_addr
;
223 ulong ltmp
= end_addr
;
225 flash_addr
= flash_addr_new
;
226 flash_addr_new
= etmp
;
228 end_addr
= end_addr_new
;
237 /* try to re-protect */
238 (void) flash_sect_protect (1, (ulong
)flash_addr
, end_addr
);
239 (void) flash_sect_protect (1, (ulong
)flash_addr_new
, end_addr_new
);
243 #endif /* CMD_SAVEENV */
245 #else /* ! CONFIG_ENV_ADDR_REDUND */
249 if (crc32(0, env_ptr
->data
, ENV_SIZE
) == env_ptr
->crc
) {
250 gd
->env_addr
= (ulong
)&(env_ptr
->data
);
255 gd
->env_addr
= (ulong
)&default_environment
[0];
266 ulong flash_sect_addr
;
267 #if defined(CONFIG_ENV_SECT_SIZE) && (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE)
269 uchar env_buffer
[CONFIG_ENV_SECT_SIZE
];
271 uchar
*env_buffer
= (uchar
*)env_ptr
;
272 #endif /* CONFIG_ENV_SECT_SIZE */
275 #if defined(CONFIG_ENV_SECT_SIZE) && (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE)
277 flash_offset
= ((ulong
)flash_addr
) & (CONFIG_ENV_SECT_SIZE
-1);
278 flash_sect_addr
= ((ulong
)flash_addr
) & ~(CONFIG_ENV_SECT_SIZE
-1);
280 debug ( "copy old content: "
281 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
282 flash_sect_addr
, (ulong
)flash_addr
, flash_offset
);
284 /* copy old contents to temporary buffer */
285 memcpy (env_buffer
, (void *)flash_sect_addr
, CONFIG_ENV_SECT_SIZE
);
287 /* copy current environment to temporary buffer */
288 memcpy ((uchar
*)((unsigned long)env_buffer
+ flash_offset
),
292 len
= CONFIG_ENV_SECT_SIZE
;
294 flash_sect_addr
= (ulong
)flash_addr
;
295 len
= CONFIG_ENV_SIZE
;
296 #endif /* CONFIG_ENV_SECT_SIZE */
298 #ifndef CONFIG_INFERNO
299 end_addr
= flash_sect_addr
+ len
- 1;
301 /* this is the last sector, and the size is hardcoded here */
302 /* otherwise we will get stack problems on loading 128 KB environment */
303 end_addr
= flash_sect_addr
+ 0x20000 - 1;
306 debug ("Protect off %08lX ... %08lX\n",
307 (ulong
)flash_sect_addr
, end_addr
);
309 if (flash_sect_protect (0, flash_sect_addr
, end_addr
))
312 puts ("Erasing Flash...");
313 if (flash_sect_erase (flash_sect_addr
, end_addr
))
316 puts ("Writing to Flash... ");
317 rc
= flash_write((char *)env_buffer
, flash_sect_addr
, len
);
325 /* try to re-protect */
326 (void) flash_sect_protect (1, flash_sect_addr
, end_addr
);
330 #endif /* CMD_SAVEENV */
332 #endif /* CONFIG_ENV_ADDR_REDUND */
334 void env_relocate_spec (void)
336 #if !defined(ENV_IS_EMBEDDED) || defined(CONFIG_ENV_ADDR_REDUND)
337 #ifdef CONFIG_ENV_ADDR_REDUND
338 if (gd
->env_addr
!= (ulong
)&(flash_addr
->data
)) {
339 env_t
* etmp
= flash_addr
;
340 ulong ltmp
= end_addr
;
342 flash_addr
= flash_addr_new
;
343 flash_addr_new
= etmp
;
345 end_addr
= end_addr_new
;
349 if (flash_addr_new
->flags
!= OBSOLETE_FLAG
&&
350 crc32(0, flash_addr_new
->data
, ENV_SIZE
) ==
351 flash_addr_new
->crc
) {
352 char flag
= OBSOLETE_FLAG
;
355 flash_sect_protect (0, (ulong
)flash_addr_new
, end_addr_new
);
357 (ulong
)&(flash_addr_new
->flags
),
358 sizeof(flash_addr_new
->flags
));
359 flash_sect_protect (1, (ulong
)flash_addr_new
, end_addr_new
);
362 if (flash_addr
->flags
!= ACTIVE_FLAG
&&
363 (flash_addr
->flags
& ACTIVE_FLAG
) == ACTIVE_FLAG
) {
364 char flag
= ACTIVE_FLAG
;
367 flash_sect_protect (0, (ulong
)flash_addr
, end_addr
);
369 (ulong
)&(flash_addr
->flags
),
370 sizeof(flash_addr
->flags
));
371 flash_sect_protect (1, (ulong
)flash_addr
, end_addr
);
374 if (gd
->env_valid
== 2)
375 puts ("*** Warning - some problems detected "
376 "reading environment; recovered successfully\n\n");
377 #endif /* CONFIG_ENV_ADDR_REDUND */
379 memcpy (env_ptr
, (void*)flash_addr
, CONFIG_ENV_SIZE
);
381 #endif /* ! ENV_IS_EMBEDDED || CONFIG_ENV_ADDR_REDUND */