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 #if defined(CFG_ENV_IS_IN_FLASH) /* Environment is in Flash */
34 #include <environment.h>
35 #include <linux/stddef.h>
38 DECLARE_GLOBAL_DATA_PTR;
40 #if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)
42 #elif defined(CFG_ENV_ADDR_REDUND)
43 #error Cannot use CFG_ENV_ADDR_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_FLASH
46 #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
47 #error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
51 # ifdef CFG_ENV_ADDR_REDUND
52 #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
56 char * env_name_spec = "Flash";
58 #ifdef ENV_IS_EMBEDDED
60 extern uchar environment[];
61 env_t *env_ptr = (env_t *)(&environment[0]);
64 /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
65 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
68 #else /* ! ENV_IS_EMBEDDED */
70 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
72 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
75 #endif /* ENV_IS_EMBEDDED */
77 #ifdef CFG_ENV_ADDR_REDUND
78 static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
80 /* CFG_ENV_ADDR is supposed to be on sector boundary */
81 static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
82 static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
85 #define OBSOLETE_FLAG 0
86 #endif /* CFG_ENV_ADDR_REDUND */
88 extern uchar default_environment[];
89 extern int default_environment_size;
92 uchar env_get_char_spec (int index)
94 return ( *((uchar *)(gd->env_addr + index)) );
97 #ifdef CFG_ENV_ADDR_REDUND
101 int crc1_ok = 0, crc2_ok = 0;
103 uchar flag1 = flash_addr->flags;
104 uchar flag2 = flash_addr_new->flags;
106 ulong addr_default = (ulong)&default_environment[0];
107 ulong addr1 = (ulong)&(flash_addr->data);
108 ulong addr2 = (ulong)&(flash_addr_new->data);
110 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
111 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
113 if (crc1_ok && ! crc2_ok) {
114 gd->env_addr = addr1;
116 } else if (! crc1_ok && crc2_ok) {
117 gd->env_addr = addr2;
119 } else if (! crc1_ok && ! crc2_ok) {
120 gd->env_addr = addr_default;
122 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
123 gd->env_addr = addr1;
125 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
126 gd->env_addr = addr2;
128 } else if (flag1 == flag2) {
129 gd->env_addr = addr1;
131 } else if (flag1 == 0xFF) {
132 gd->env_addr = addr1;
134 } else if (flag2 == 0xFF) {
135 gd->env_addr = addr2;
145 char *saved_data = NULL;
147 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
148 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
152 debug ("Protect off %08lX ... %08lX\n",
153 (ulong)flash_addr, end_addr);
155 if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
159 debug ("Protect off %08lX ... %08lX\n",
160 (ulong)flash_addr_new, end_addr_new);
162 if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
166 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
167 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
168 debug ("Data to save 0x%x\n", up_data);
170 if ((saved_data = malloc(up_data)) == NULL) {
171 printf("Unable to save the rest of sector (%ld)\n",
176 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
177 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
178 (long)flash_addr_new + CFG_ENV_SIZE,
179 up_data, saved_data);
182 puts ("Erasing Flash...");
183 debug (" %08lX ... %08lX ...",
184 (ulong)flash_addr_new, end_addr_new);
186 if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
190 puts ("Writing to Flash... ");
191 debug (" %08lX ... %08lX ...",
192 (ulong)&(flash_addr_new->data),
193 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
194 if ((rc = flash_write((char *)env_ptr->data,
195 (ulong)&(flash_addr_new->data),
196 sizeof(env_ptr->data))) ||
197 (rc = flash_write((char *)&(env_ptr->crc),
198 (ulong)&(flash_addr_new->crc),
199 sizeof(env_ptr->crc))) ||
200 (rc = flash_write(&flag,
201 (ulong)&(flash_addr->flags),
202 sizeof(flash_addr->flags))) ||
203 (rc = flash_write(&new_flag,
204 (ulong)&(flash_addr_new->flags),
205 sizeof(flash_addr_new->flags))))
212 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
213 if (up_data) { /* restore the rest of sector */
214 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
215 (long)flash_addr_new + CFG_ENV_SIZE, up_data);
216 if (flash_write(saved_data,
217 (long)flash_addr_new + CFG_ENV_SIZE,
225 env_t * etmp = flash_addr;
226 ulong ltmp = end_addr;
228 flash_addr = flash_addr_new;
229 flash_addr_new = etmp;
231 end_addr = end_addr_new;
240 /* try to re-protect */
241 (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
242 (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
246 #endif /* CMD_SAVEENV */
248 #else /* ! CFG_ENV_ADDR_REDUND */
252 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
253 gd->env_addr = (ulong)&(env_ptr->data);
258 gd->env_addr = (ulong)&default_environment[0];
269 ulong flash_sect_addr;
270 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
272 uchar env_buffer[CFG_ENV_SECT_SIZE];
274 uchar *env_buffer = (uchar *)env_ptr;
275 #endif /* CFG_ENV_SECT_SIZE */
278 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
280 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
281 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
283 debug ( "copy old content: "
284 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
285 flash_sect_addr, (ulong)flash_addr, flash_offset);
287 /* copy old contents to temporary buffer */
288 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
290 /* copy current environment to temporary buffer */
291 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
295 len = CFG_ENV_SECT_SIZE;
297 flash_sect_addr = (ulong)flash_addr;
299 #endif /* CFG_ENV_SECT_SIZE */
301 #ifndef CONFIG_INFERNO
302 end_addr = flash_sect_addr + len - 1;
304 /* this is the last sector, and the size is hardcoded here */
305 /* otherwise we will get stack problems on loading 128 KB environment */
306 end_addr = flash_sect_addr + 0x20000 - 1;
309 debug ("Protect off %08lX ... %08lX\n",
310 (ulong)flash_sect_addr, end_addr);
312 if (flash_sect_protect (0, flash_sect_addr, end_addr))
315 puts ("Erasing Flash...");
316 if (flash_sect_erase (flash_sect_addr, end_addr))
319 puts ("Writing to Flash... ");
320 rc = flash_write((char *)env_buffer, flash_sect_addr, len);
328 /* try to re-protect */
329 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
333 #endif /* CMD_SAVEENV */
335 #endif /* CFG_ENV_ADDR_REDUND */
337 void env_relocate_spec (void)
339 #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
340 #ifdef CFG_ENV_ADDR_REDUND
341 if (gd->env_addr != (ulong)&(flash_addr->data)) {
342 env_t * etmp = flash_addr;
343 ulong ltmp = end_addr;
345 flash_addr = flash_addr_new;
346 flash_addr_new = etmp;
348 end_addr = end_addr_new;
352 if (flash_addr_new->flags != OBSOLETE_FLAG &&
353 crc32(0, flash_addr_new->data, ENV_SIZE) ==
354 flash_addr_new->crc) {
355 char flag = OBSOLETE_FLAG;
358 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
360 (ulong)&(flash_addr_new->flags),
361 sizeof(flash_addr_new->flags));
362 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
365 if (flash_addr->flags != ACTIVE_FLAG &&
366 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
367 char flag = ACTIVE_FLAG;
370 flash_sect_protect (0, (ulong)flash_addr, end_addr);
372 (ulong)&(flash_addr->flags),
373 sizeof(flash_addr->flags));
374 flash_sect_protect (1, (ulong)flash_addr, end_addr);
377 if (gd->env_valid == 2)
378 puts ("*** Warning - some problems detected "
379 "reading environment; recovered successfully\n\n");
380 #endif /* CFG_ENV_ADDR_REDUND */
381 memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
382 #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
385 #endif /* CFG_ENV_IS_IN_FLASH */