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 #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_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
45 #error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
49 # ifdef CFG_ENV_ADDR_REDUND
50 #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
54 char * env_name_spec = "Flash";
56 #ifdef ENV_IS_EMBEDDED
58 extern uchar environment[];
59 env_t *env_ptr = (env_t *)(&environment[0]);
62 /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
63 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
66 #else /* ! ENV_IS_EMBEDDED */
68 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
70 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
73 #endif /* ENV_IS_EMBEDDED */
75 #ifdef CFG_ENV_ADDR_REDUND
76 static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
78 /* CFG_ENV_ADDR is supposed to be on sector boundary */
79 static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
80 static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
82 static uchar active_flag = 1;
83 static uchar obsolete_flag = 0;
86 extern uchar default_environment[];
87 extern int default_environment_size;
90 uchar env_get_char_spec (int index)
92 DECLARE_GLOBAL_DATA_PTR;
94 return ( *((uchar *)(gd->env_addr + index)) );
97 #ifdef CFG_ENV_ADDR_REDUND
101 DECLARE_GLOBAL_DATA_PTR;
104 (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
106 (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
108 uchar flag1 = flash_addr->flags;
109 uchar flag2 = flash_addr_new->flags;
111 ulong addr_default = (ulong)&default_environment[0];
112 ulong addr1 = (ulong)&(flash_addr->data);
113 ulong addr2 = (ulong)&(flash_addr_new->data);
115 if (crc1_ok && ! crc2_ok)
117 gd->env_addr = addr1;
120 else if (! crc1_ok && crc2_ok)
122 gd->env_addr = addr2;
125 else if (! crc1_ok && ! crc2_ok)
127 gd->env_addr = addr_default;
130 else if (flag1 == active_flag && flag2 == obsolete_flag)
132 gd->env_addr = addr1;
135 else if (flag1 == obsolete_flag && flag2 == active_flag)
137 gd->env_addr = addr2;
140 else if (flag1 == flag2)
142 gd->env_addr = addr1;
145 else if (flag1 == 0xFF)
147 gd->env_addr = addr1;
150 else if (flag2 == 0xFF)
152 gd->env_addr = addr2;
162 char *saved_data = NULL;
164 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
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 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
183 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
184 debug ("Data to save 0x%x\n", up_data);
186 if ((saved_data = malloc(up_data)) == NULL) {
187 printf("Unable to save the rest of sector (%ld)\n",
192 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
193 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
194 (long)flash_addr_new + CFG_ENV_SIZE,
195 up_data, saved_data);
198 puts ("Erasing Flash...");
199 debug (" %08lX ... %08lX ...",
200 (ulong)flash_addr_new, end_addr_new);
202 if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
206 puts ("Writing to Flash... ");
207 debug (" %08lX ... %08lX ...",
208 (ulong)&(flash_addr_new->data),
209 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
210 if (flash_write(env_ptr->data,
211 (ulong)&(flash_addr_new->data),
212 sizeof(env_ptr->data)) ||
214 flash_write((char *)&(env_ptr->crc),
215 (ulong)&(flash_addr_new->crc),
216 sizeof(env_ptr->crc)) ||
218 flash_write((char *)&obsolete_flag,
219 (ulong)&(flash_addr->flags),
220 sizeof(flash_addr->flags)) ||
222 flash_write((char *)&active_flag,
223 (ulong)&(flash_addr_new->flags),
224 sizeof(flash_addr_new->flags)))
231 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
232 if (up_data) { /* restore the rest of sector */
233 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
234 (long)flash_addr_new + CFG_ENV_SIZE, up_data);
235 if (flash_write(saved_data,
236 (long)flash_addr_new + CFG_ENV_SIZE,
244 env_t * etmp = flash_addr;
245 ulong ltmp = end_addr;
247 flash_addr = flash_addr_new;
248 flash_addr_new = etmp;
250 end_addr = end_addr_new;
259 /* try to re-protect */
260 (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
261 (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
265 #endif /* CMD_SAVEENV */
267 #else /* ! CFG_ENV_ADDR_REDUND */
271 DECLARE_GLOBAL_DATA_PTR;
273 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
274 gd->env_addr = (ulong)&(env_ptr->data);
277 gd->env_addr = (ulong)&default_environment[0];
290 ulong flash_sect_addr;
291 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
293 uchar env_buffer[CFG_ENV_SECT_SIZE];
295 uchar *env_buffer = (char *)env_ptr;
296 #endif /* CFG_ENV_SECT_SIZE */
299 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
301 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
302 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
304 debug ( "copy old content: "
305 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
306 flash_sect_addr, (ulong)flash_addr, flash_offset);
308 /* copy old contents to temporary buffer */
309 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
311 /* copy current environment to temporary buffer */
312 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
316 len = CFG_ENV_SECT_SIZE;
318 flash_sect_addr = (ulong)flash_addr;
320 #endif /* CFG_ENV_SECT_SIZE */
322 #ifndef CONFIG_INFERNO
323 end_addr = flash_sect_addr + len - 1;
325 /* this is the last sector, and the size is hardcoded here */
326 /* otherwise we will get stack problems on loading 128 KB environment */
327 end_addr = flash_sect_addr + 0x20000 - 1;
330 debug ("Protect off %08lX ... %08lX\n",
331 (ulong)flash_sect_addr, end_addr);
333 if (flash_sect_protect (0, flash_sect_addr, end_addr))
336 puts ("Erasing Flash...");
337 if (flash_sect_erase (flash_sect_addr, end_addr))
340 puts ("Writing to Flash... ");
341 rc = flash_write(env_buffer, flash_sect_addr, len);
349 /* try to re-protect */
350 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
354 #endif /* CMD_SAVEENV */
356 #endif /* CFG_ENV_ADDR_REDUND */
358 void env_relocate_spec (void)
360 #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
361 #ifdef CFG_ENV_ADDR_REDUND
362 DECLARE_GLOBAL_DATA_PTR;
364 if (gd->env_addr != (ulong)&(flash_addr->data))
366 env_t * etmp = flash_addr;
367 ulong ltmp = end_addr;
369 flash_addr = flash_addr_new;
370 flash_addr_new = etmp;
372 end_addr = end_addr_new;
376 if (flash_addr_new->flags != obsolete_flag &&
377 crc32(0, flash_addr_new->data, ENV_SIZE) ==
381 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
382 flash_write((char *)&obsolete_flag,
383 (ulong)&(flash_addr_new->flags),
384 sizeof(flash_addr_new->flags));
385 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
388 if (flash_addr->flags != active_flag &&
389 (flash_addr->flags & active_flag) == active_flag)
392 flash_sect_protect (0, (ulong)flash_addr, end_addr);
393 flash_write((char *)&active_flag,
394 (ulong)&(flash_addr->flags),
395 sizeof(flash_addr->flags));
396 flash_sect_protect (1, (ulong)flash_addr, end_addr);
399 if (gd->env_valid == 2)
400 puts ("*** Warning - some problems detected "
401 "reading environment; recovered successfully\n\n");
402 #endif /* CFG_ENV_ADDR_REDUND */
403 memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
404 #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
407 #endif /* CFG_ENV_IS_IN_FLASH */