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 <cmd_nvedit.h>
36 #include <linux/stddef.h>
38 #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
40 #elif defined(CFG_ENV_ADDR_REDUND)
41 #error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH
44 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) && \
45 defined(CFG_ENV_ADDR_REDUND)
46 #error CFG_ENV_ADDR_REDUND should not be used when CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
49 #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
50 #error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
54 # ifdef CFG_ENV_ADDR_REDUND
55 #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
59 char * env_name_spec = "Flash";
61 #ifdef ENV_IS_EMBEDDED
63 extern uchar environment[];
64 env_t *env_ptr = (env_t *)(&environment[0]);
67 /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
68 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
71 #else /* ! ENV_IS_EMBEDDED */
73 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
75 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
78 #endif /* ENV_IS_EMBEDDED */
80 #ifdef CFG_ENV_ADDR_REDUND
81 static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
83 static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SIZE - 1;
84 static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1;
86 static uchar active_flag = 1;
87 static uchar obsolete_flag = 0;
90 extern uchar default_environment[];
91 extern int default_environment_size;
94 uchar env_get_char_spec (int index)
96 DECLARE_GLOBAL_DATA_PTR;
98 return ( *((uchar *)(gd->env_addr + index)) );
101 #ifdef CFG_ENV_ADDR_REDUND
105 DECLARE_GLOBAL_DATA_PTR;
108 (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
110 (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
112 uchar flag1 = flash_addr->flags;
113 uchar flag2 = flash_addr_new->flags;
115 ulong addr_default = (ulong)&default_environment[0];
116 ulong addr1 = (ulong)&(flash_addr->data);
117 ulong addr2 = (ulong)&(flash_addr_new->data);
119 if (crc1_ok && ! crc2_ok)
121 gd->env_addr = addr1;
124 else if (! crc1_ok && crc2_ok)
126 gd->env_addr = addr2;
129 else if (! crc1_ok && ! crc2_ok)
131 gd->env_addr = addr_default;
134 else if (flag1 == active_flag && flag2 == obsolete_flag)
136 gd->env_addr = addr1;
139 else if (flag1 == obsolete_flag && flag2 == active_flag)
141 gd->env_addr = addr2;
144 else if (flag1 == flag2)
146 gd->env_addr = addr1;
149 else if (flag1 == 0xFF)
151 gd->env_addr = addr1;
154 else if (flag2 == 0xFF)
156 gd->env_addr = addr2;
168 debug ("Protect off %08lX ... %08lX\n",
169 (ulong)flash_addr, end_addr);
171 if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
175 debug ("Protect off %08lX ... %08lX\n",
176 (ulong)flash_addr_new, end_addr_new);
178 if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
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 (flash_write(env_ptr->data,
195 (ulong)&(flash_addr_new->data),
196 sizeof(env_ptr->data)) ||
198 flash_write((char *)&(env_ptr->crc),
199 (ulong)&(flash_addr_new->crc),
200 sizeof(env_ptr->crc)) ||
202 flash_write((char *)&obsolete_flag,
203 (ulong)&(flash_addr->flags),
204 sizeof(flash_addr->flags)) ||
206 flash_write((char *)&active_flag,
207 (ulong)&(flash_addr_new->flags),
208 sizeof(flash_addr_new->flags)))
216 env_t * etmp = flash_addr;
217 ulong ltmp = end_addr;
219 flash_addr = flash_addr_new;
220 flash_addr_new = etmp;
222 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 /* ! CFG_ENV_ADDR_REDUND */
241 DECLARE_GLOBAL_DATA_PTR;
243 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
244 gd->env_addr = (ulong)&(env_ptr->data);
247 gd->env_addr = (ulong)&default_environment[0];
260 ulong flash_sect_addr;
261 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
263 uchar env_buffer[CFG_ENV_SECT_SIZE];
265 uchar *env_buffer = (char *)env_ptr;
266 #endif /* CFG_ENV_SECT_SIZE */
269 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
271 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
272 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
274 debug ( "copy old content: "
275 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
276 flash_sect_addr, (ulong)flash_addr, flash_offset);
278 /* copy old contents to temporary buffer */
279 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
281 /* copy current environment to temporary buffer */
282 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
286 len = CFG_ENV_SECT_SIZE;
288 flash_sect_addr = (ulong)flash_addr;
290 #endif /* CFG_ENV_SECT_SIZE */
292 #ifndef CONFIG_INFERNO
293 end_addr = flash_sect_addr + len - 1;
295 /* this is the last sector, and the size is hardcoded here */
296 /* otherwise we will get stack problems on loading 128 KB environment */
297 end_addr = flash_sect_addr + 0x20000 - 1;
300 debug ("Protect off %08lX ... %08lX\n",
301 (ulong)flash_sect_addr, end_addr);
303 if (flash_sect_protect (0, flash_sect_addr, end_addr))
306 puts ("Erasing Flash...");
307 if (flash_sect_erase (flash_sect_addr, end_addr))
310 puts ("Writing to Flash... ");
311 rc = flash_write(env_buffer, flash_sect_addr, len);
319 /* try to re-protect */
320 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
324 #endif /* CMD_SAVEENV */
326 #endif /* CFG_ENV_ADDR_REDUND */
328 void env_relocate_spec (void)
330 #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
331 #ifdef CFG_ENV_ADDR_REDUND
332 DECLARE_GLOBAL_DATA_PTR;
334 if (gd->env_addr != (ulong)&(flash_addr->data))
336 env_t * etmp = flash_addr;
337 ulong ltmp = end_addr;
339 flash_addr = flash_addr_new;
340 flash_addr_new = etmp;
342 end_addr = end_addr_new;
346 if (flash_addr_new->flags != obsolete_flag &&
347 crc32(0, flash_addr_new->data, ENV_SIZE) ==
351 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
352 flash_write((char *)&obsolete_flag,
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)
362 flash_sect_protect (0, (ulong)flash_addr, end_addr);
363 flash_write((char *)&active_flag,
364 (ulong)&(flash_addr->flags),
365 sizeof(flash_addr->flags));
366 flash_sect_protect (1, (ulong)flash_addr, end_addr);
369 if (gd->env_valid == 2)
370 puts ("*** Warning - some problems detected "
371 "reading environment; recovered successfully\n\n");
372 #endif /* CFG_ENV_ADDR_REDUND */
373 memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
374 #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
377 #endif /* CFG_ENV_IS_IN_FLASH) */