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;
83 #define OBSOLETE_FLAG 0
84 #endif /* CFG_ENV_ADDR_REDUND */
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;
102 int crc1_ok = 0, crc2_ok = 0;
104 uchar flag1 = flash_addr->flags;
105 uchar flag2 = flash_addr_new->flags;
107 ulong addr_default = (ulong)&default_environment[0];
108 ulong addr1 = (ulong)&(flash_addr->data);
109 ulong addr2 = (ulong)&(flash_addr_new->data);
111 #ifdef CONFIG_OMAP2420H4
112 int flash_probe(void);
114 if(flash_probe() == 0)
118 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
119 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
121 if (crc1_ok && ! crc2_ok) {
122 gd->env_addr = addr1;
124 } else if (! crc1_ok && crc2_ok) {
125 gd->env_addr = addr2;
127 } else if (! crc1_ok && ! crc2_ok) {
128 gd->env_addr = addr_default;
130 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
131 gd->env_addr = addr1;
133 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
134 gd->env_addr = addr2;
136 } else if (flag1 == flag2) {
137 gd->env_addr = addr1;
139 } else if (flag1 == 0xFF) {
140 gd->env_addr = addr1;
142 } else if (flag2 == 0xFF) {
143 gd->env_addr = addr2;
147 #ifdef CONFIG_OMAP2420H4
156 char *saved_data = NULL;
158 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
159 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
163 debug ("Protect off %08lX ... %08lX\n",
164 (ulong)flash_addr, end_addr);
166 if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
170 debug ("Protect off %08lX ... %08lX\n",
171 (ulong)flash_addr_new, end_addr_new);
173 if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
177 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
178 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
179 debug ("Data to save 0x%x\n", up_data);
181 if ((saved_data = malloc(up_data)) == NULL) {
182 printf("Unable to save the rest of sector (%ld)\n",
187 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
188 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
189 (long)flash_addr_new + CFG_ENV_SIZE,
190 up_data, saved_data);
193 puts ("Erasing Flash...");
194 debug (" %08lX ... %08lX ...",
195 (ulong)flash_addr_new, end_addr_new);
197 if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
201 puts ("Writing to Flash... ");
202 debug (" %08lX ... %08lX ...",
203 (ulong)&(flash_addr_new->data),
204 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
205 if ((rc = flash_write((char *)env_ptr->data,
206 (ulong)&(flash_addr_new->data),
207 sizeof(env_ptr->data))) ||
208 (rc = flash_write((char *)&(env_ptr->crc),
209 (ulong)&(flash_addr_new->crc),
210 sizeof(env_ptr->crc))) ||
211 (rc = flash_write(&flag,
212 (ulong)&(flash_addr->flags),
213 sizeof(flash_addr->flags))) ||
214 (rc = flash_write(&new_flag,
215 (ulong)&(flash_addr_new->flags),
216 sizeof(flash_addr_new->flags))))
223 #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
224 if (up_data) { /* restore the rest of sector */
225 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
226 (long)flash_addr_new + CFG_ENV_SIZE, up_data);
227 if (flash_write(saved_data,
228 (long)flash_addr_new + CFG_ENV_SIZE,
236 env_t * etmp = flash_addr;
237 ulong ltmp = end_addr;
239 flash_addr = flash_addr_new;
240 flash_addr_new = etmp;
242 end_addr = end_addr_new;
251 /* try to re-protect */
252 (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
253 (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
257 #endif /* CMD_SAVEENV */
259 #else /* ! CFG_ENV_ADDR_REDUND */
263 DECLARE_GLOBAL_DATA_PTR;
264 #ifdef CONFIG_OMAP2420H4
265 int flash_probe(void);
267 if(flash_probe() == 0)
270 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
271 gd->env_addr = (ulong)&(env_ptr->data);
275 #ifdef CONFIG_OMAP2420H4
278 gd->env_addr = (ulong)&default_environment[0];
289 ulong flash_sect_addr;
290 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
292 uchar env_buffer[CFG_ENV_SECT_SIZE];
294 uchar *env_buffer = (uchar *)env_ptr;
295 #endif /* CFG_ENV_SECT_SIZE */
298 #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
300 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
301 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
303 debug ( "copy old content: "
304 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
305 flash_sect_addr, (ulong)flash_addr, flash_offset);
307 /* copy old contents to temporary buffer */
308 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
310 /* copy current environment to temporary buffer */
311 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
315 len = CFG_ENV_SECT_SIZE;
317 flash_sect_addr = (ulong)flash_addr;
319 #endif /* CFG_ENV_SECT_SIZE */
321 #ifndef CONFIG_INFERNO
322 end_addr = flash_sect_addr + len - 1;
324 /* this is the last sector, and the size is hardcoded here */
325 /* otherwise we will get stack problems on loading 128 KB environment */
326 end_addr = flash_sect_addr + 0x20000 - 1;
329 debug ("Protect off %08lX ... %08lX\n",
330 (ulong)flash_sect_addr, end_addr);
332 if (flash_sect_protect (0, flash_sect_addr, end_addr))
335 puts ("Erasing Flash...");
336 if (flash_sect_erase (flash_sect_addr, end_addr))
339 puts ("Writing to Flash... ");
340 rc = flash_write((char *)env_buffer, flash_sect_addr, len);
348 /* try to re-protect */
349 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
353 #endif /* CMD_SAVEENV */
355 #endif /* CFG_ENV_ADDR_REDUND */
357 void env_relocate_spec (void)
359 #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
360 #ifdef CFG_ENV_ADDR_REDUND
361 DECLARE_GLOBAL_DATA_PTR;
363 if (gd->env_addr != (ulong)&(flash_addr->data)) {
364 env_t * etmp = flash_addr;
365 ulong ltmp = end_addr;
367 flash_addr = flash_addr_new;
368 flash_addr_new = etmp;
370 end_addr = end_addr_new;
374 if (flash_addr_new->flags != OBSOLETE_FLAG &&
375 crc32(0, flash_addr_new->data, ENV_SIZE) ==
376 flash_addr_new->crc) {
377 char flag = OBSOLETE_FLAG;
380 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
382 (ulong)&(flash_addr_new->flags),
383 sizeof(flash_addr_new->flags));
384 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
387 if (flash_addr->flags != ACTIVE_FLAG &&
388 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
389 char flag = ACTIVE_FLAG;
392 flash_sect_protect (0, (ulong)flash_addr, end_addr);
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 */