wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | /* #define DEBUG */ |
| 28 | |
| 29 | #include <common.h> |
| 30 | |
| 31 | #if defined(CFG_ENV_IS_IN_FLASH) /* Environment is in Flash */ |
| 32 | |
| 33 | #include <command.h> |
| 34 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 35 | #include <linux/stddef.h> |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 36 | #include <malloc.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
Jon Loeliger | c3517f9 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 40 | #if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #define CMD_SAVEENV |
| 42 | #elif defined(CFG_ENV_ADDR_REDUND) |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 43 | #error Cannot use CFG_ENV_ADDR_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_FLASH |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 |
| 48 | #endif |
| 49 | |
| 50 | #ifdef CONFIG_INFERNO |
| 51 | # ifdef CFG_ENV_ADDR_REDUND |
| 52 | #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO |
| 53 | # endif |
| 54 | #endif |
| 55 | |
| 56 | char * env_name_spec = "Flash"; |
| 57 | |
| 58 | #ifdef ENV_IS_EMBEDDED |
| 59 | |
| 60 | extern uchar environment[]; |
| 61 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 62 | |
| 63 | #ifdef CMD_SAVEENV |
| 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; |
| 66 | #endif |
| 67 | |
| 68 | #else /* ! ENV_IS_EMBEDDED */ |
| 69 | |
| 70 | env_t *env_ptr = (env_t *)CFG_ENV_ADDR; |
| 71 | #ifdef CMD_SAVEENV |
| 72 | static env_t *flash_addr = (env_t *)CFG_ENV_ADDR; |
| 73 | #endif |
| 74 | |
| 75 | #endif /* ENV_IS_EMBEDDED */ |
| 76 | |
| 77 | #ifdef CFG_ENV_ADDR_REDUND |
| 78 | static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND; |
| 79 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 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; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 84 | #define ACTIVE_FLAG 1 |
| 85 | #define OBSOLETE_FLAG 0 |
| 86 | #endif /* CFG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 87 | |
| 88 | extern uchar default_environment[]; |
| 89 | extern int default_environment_size; |
| 90 | |
| 91 | |
| 92 | uchar env_get_char_spec (int index) |
| 93 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 94 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 95 | } |
| 96 | |
| 97 | #ifdef CFG_ENV_ADDR_REDUND |
| 98 | |
| 99 | int env_init(void) |
| 100 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 101 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | |
| 103 | uchar flag1 = flash_addr->flags; |
| 104 | uchar flag2 = flash_addr_new->flags; |
| 105 | |
| 106 | ulong addr_default = (ulong)&default_environment[0]; |
| 107 | ulong addr1 = (ulong)&(flash_addr->data); |
| 108 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 109 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 110 | #ifdef CONFIG_OMAP2420H4 |
| 111 | int flash_probe(void); |
| 112 | |
| 113 | if(flash_probe() == 0) |
| 114 | goto bad_flash; |
| 115 | #endif |
| 116 | |
| 117 | crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 118 | crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 119 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 120 | if (crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 121 | gd->env_addr = addr1; |
| 122 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 123 | } else if (! crc1_ok && crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 124 | gd->env_addr = addr2; |
| 125 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 126 | } else if (! crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 127 | gd->env_addr = addr_default; |
| 128 | gd->env_valid = 0; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 129 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 130 | gd->env_addr = addr1; |
| 131 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 132 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | gd->env_addr = addr2; |
| 134 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 135 | } else if (flag1 == flag2) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 136 | gd->env_addr = addr1; |
| 137 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 138 | } else if (flag1 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 139 | gd->env_addr = addr1; |
| 140 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 141 | } else if (flag2 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 142 | gd->env_addr = addr2; |
| 143 | gd->env_valid = 2; |
| 144 | } |
| 145 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 146 | #ifdef CONFIG_OMAP2420H4 |
| 147 | bad_flash: |
| 148 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 149 | return (0); |
| 150 | } |
| 151 | |
| 152 | #ifdef CMD_SAVEENV |
| 153 | int saveenv(void) |
| 154 | { |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 155 | char *saved_data = NULL; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 156 | int rc = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 157 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 158 | #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE |
| 159 | ulong up_data = 0; |
| 160 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | |
| 162 | debug ("Protect off %08lX ... %08lX\n", |
| 163 | (ulong)flash_addr, end_addr); |
| 164 | |
| 165 | if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) { |
| 166 | goto Done; |
| 167 | } |
| 168 | |
| 169 | debug ("Protect off %08lX ... %08lX\n", |
| 170 | (ulong)flash_addr_new, end_addr_new); |
| 171 | |
| 172 | if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) { |
| 173 | goto Done; |
| 174 | } |
| 175 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 176 | #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE |
| 177 | up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE)); |
| 178 | debug ("Data to save 0x%x\n", up_data); |
| 179 | if (up_data) { |
| 180 | if ((saved_data = malloc(up_data)) == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 181 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 182 | up_data); |
| 183 | goto Done; |
| 184 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 185 | memcpy(saved_data, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 186 | (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 187 | debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n", |
| 188 | (long)flash_addr_new + CFG_ENV_SIZE, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 189 | up_data, saved_data); |
| 190 | } |
| 191 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | puts ("Erasing Flash..."); |
| 193 | debug (" %08lX ... %08lX ...", |
| 194 | (ulong)flash_addr_new, end_addr_new); |
| 195 | |
| 196 | if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) { |
| 197 | goto Done; |
| 198 | } |
| 199 | |
| 200 | puts ("Writing to Flash... "); |
| 201 | debug (" %08lX ... %08lX ...", |
| 202 | (ulong)&(flash_addr_new->data), |
| 203 | sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data)); |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 204 | if ((rc = flash_write((char *)env_ptr->data, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 205 | (ulong)&(flash_addr_new->data), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 206 | sizeof(env_ptr->data))) || |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 207 | (rc = flash_write((char *)&(env_ptr->crc), |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 208 | (ulong)&(flash_addr_new->crc), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 209 | sizeof(env_ptr->crc))) || |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 210 | (rc = flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 211 | (ulong)&(flash_addr->flags), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 212 | sizeof(flash_addr->flags))) || |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 213 | (rc = flash_write(&new_flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 214 | (ulong)&(flash_addr_new->flags), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 215 | sizeof(flash_addr_new->flags)))) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 216 | { |
| 217 | flash_perror (rc); |
| 218 | goto Done; |
| 219 | } |
| 220 | puts ("done\n"); |
| 221 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 222 | #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE |
| 223 | if (up_data) { /* restore the rest of sector */ |
| 224 | debug ("Restoring the rest of data to 0x%x len 0x%x\n", |
| 225 | (long)flash_addr_new + CFG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 226 | if (flash_write(saved_data, |
| 227 | (long)flash_addr_new + CFG_ENV_SIZE, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 228 | up_data)) { |
| 229 | flash_perror(rc); |
| 230 | goto Done; |
| 231 | } |
| 232 | } |
| 233 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 234 | { |
| 235 | env_t * etmp = flash_addr; |
| 236 | ulong ltmp = end_addr; |
| 237 | |
| 238 | flash_addr = flash_addr_new; |
| 239 | flash_addr_new = etmp; |
| 240 | |
| 241 | end_addr = end_addr_new; |
| 242 | end_addr_new = ltmp; |
| 243 | } |
| 244 | |
| 245 | rc = 0; |
| 246 | Done: |
| 247 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 248 | if (saved_data) |
| 249 | free (saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 250 | /* try to re-protect */ |
| 251 | (void) flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 252 | (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 253 | |
| 254 | return rc; |
| 255 | } |
| 256 | #endif /* CMD_SAVEENV */ |
| 257 | |
| 258 | #else /* ! CFG_ENV_ADDR_REDUND */ |
| 259 | |
| 260 | int env_init(void) |
| 261 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 262 | #ifdef CONFIG_OMAP2420H4 |
| 263 | int flash_probe(void); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 264 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 265 | if(flash_probe() == 0) |
| 266 | goto bad_flash; |
| 267 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 269 | gd->env_addr = (ulong)&(env_ptr->data); |
| 270 | gd->env_valid = 1; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 271 | return(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | } |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 273 | #ifdef CONFIG_OMAP2420H4 |
| 274 | bad_flash: |
| 275 | #endif |
| 276 | gd->env_addr = (ulong)&default_environment[0]; |
| 277 | gd->env_valid = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 278 | return (0); |
| 279 | } |
| 280 | |
| 281 | #ifdef CMD_SAVEENV |
| 282 | |
| 283 | int saveenv(void) |
| 284 | { |
| 285 | int len, rc; |
| 286 | ulong end_addr; |
| 287 | ulong flash_sect_addr; |
| 288 | #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) |
| 289 | ulong flash_offset; |
| 290 | uchar env_buffer[CFG_ENV_SECT_SIZE]; |
| 291 | #else |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 292 | uchar *env_buffer = (uchar *)env_ptr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 293 | #endif /* CFG_ENV_SECT_SIZE */ |
| 294 | int rcode = 0; |
| 295 | |
| 296 | #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) |
| 297 | |
| 298 | flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1); |
| 299 | flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1); |
| 300 | |
| 301 | debug ( "copy old content: " |
| 302 | "sect_addr: %08lX env_addr: %08lX offset: %08lX\n", |
| 303 | flash_sect_addr, (ulong)flash_addr, flash_offset); |
| 304 | |
| 305 | /* copy old contents to temporary buffer */ |
| 306 | memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE); |
| 307 | |
| 308 | /* copy current environment to temporary buffer */ |
| 309 | memcpy ((uchar *)((unsigned long)env_buffer + flash_offset), |
| 310 | env_ptr, |
| 311 | CFG_ENV_SIZE); |
| 312 | |
| 313 | len = CFG_ENV_SECT_SIZE; |
| 314 | #else |
| 315 | flash_sect_addr = (ulong)flash_addr; |
| 316 | len = CFG_ENV_SIZE; |
| 317 | #endif /* CFG_ENV_SECT_SIZE */ |
| 318 | |
| 319 | #ifndef CONFIG_INFERNO |
| 320 | end_addr = flash_sect_addr + len - 1; |
| 321 | #else |
| 322 | /* this is the last sector, and the size is hardcoded here */ |
| 323 | /* otherwise we will get stack problems on loading 128 KB environment */ |
| 324 | end_addr = flash_sect_addr + 0x20000 - 1; |
| 325 | #endif |
| 326 | |
| 327 | debug ("Protect off %08lX ... %08lX\n", |
| 328 | (ulong)flash_sect_addr, end_addr); |
| 329 | |
| 330 | if (flash_sect_protect (0, flash_sect_addr, end_addr)) |
| 331 | return 1; |
| 332 | |
| 333 | puts ("Erasing Flash..."); |
| 334 | if (flash_sect_erase (flash_sect_addr, end_addr)) |
| 335 | return 1; |
| 336 | |
| 337 | puts ("Writing to Flash... "); |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 338 | rc = flash_write((char *)env_buffer, flash_sect_addr, len); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 339 | if (rc != 0) { |
| 340 | flash_perror (rc); |
| 341 | rcode = 1; |
| 342 | } else { |
| 343 | puts ("done\n"); |
| 344 | } |
| 345 | |
| 346 | /* try to re-protect */ |
| 347 | (void) flash_sect_protect (1, flash_sect_addr, end_addr); |
| 348 | return rcode; |
| 349 | } |
| 350 | |
| 351 | #endif /* CMD_SAVEENV */ |
| 352 | |
| 353 | #endif /* CFG_ENV_ADDR_REDUND */ |
| 354 | |
| 355 | void env_relocate_spec (void) |
| 356 | { |
| 357 | #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND) |
| 358 | #ifdef CFG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 359 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 360 | env_t * etmp = flash_addr; |
| 361 | ulong ltmp = end_addr; |
| 362 | |
| 363 | flash_addr = flash_addr_new; |
| 364 | flash_addr_new = etmp; |
| 365 | |
| 366 | end_addr = end_addr_new; |
| 367 | end_addr_new = ltmp; |
| 368 | } |
| 369 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 370 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 371 | crc32(0, flash_addr_new->data, ENV_SIZE) == |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 372 | flash_addr_new->crc) { |
| 373 | char flag = OBSOLETE_FLAG; |
| 374 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 375 | gd->env_valid = 2; |
| 376 | flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 377 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 378 | (ulong)&(flash_addr_new->flags), |
| 379 | sizeof(flash_addr_new->flags)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 380 | flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 381 | } |
| 382 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 383 | if (flash_addr->flags != ACTIVE_FLAG && |
| 384 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 385 | char flag = ACTIVE_FLAG; |
| 386 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | gd->env_valid = 2; |
| 388 | flash_sect_protect (0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 389 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 390 | (ulong)&(flash_addr->flags), |
| 391 | sizeof(flash_addr->flags)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 392 | flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 393 | } |
| 394 | |
| 395 | if (gd->env_valid == 2) |
| 396 | puts ("*** Warning - some problems detected " |
| 397 | "reading environment; recovered successfully\n\n"); |
| 398 | #endif /* CFG_ENV_ADDR_REDUND */ |
| 399 | memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE); |
| 400 | #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */ |
| 401 | } |
| 402 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 403 | #endif /* CFG_ENV_IS_IN_FLASH */ |