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> |
| 35 | #include <cmd_nvedit.h> |
| 36 | #include <linux/stddef.h> |
| 37 | |
| 38 | #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH)) |
| 39 | #define CMD_SAVEENV |
| 40 | #elif defined(CFG_ENV_ADDR_REDUND) |
| 41 | #error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH |
| 42 | #endif |
| 43 | |
| 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 |
| 47 | #endif |
| 48 | |
| 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 |
| 51 | #endif |
| 52 | |
| 53 | #ifdef CONFIG_INFERNO |
| 54 | # ifdef CFG_ENV_ADDR_REDUND |
| 55 | #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO |
| 56 | # endif |
| 57 | #endif |
| 58 | |
| 59 | char * env_name_spec = "Flash"; |
| 60 | |
| 61 | #ifdef ENV_IS_EMBEDDED |
| 62 | |
| 63 | extern uchar environment[]; |
| 64 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 65 | |
| 66 | #ifdef CMD_SAVEENV |
| 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; |
| 69 | #endif |
| 70 | |
| 71 | #else /* ! ENV_IS_EMBEDDED */ |
| 72 | |
| 73 | env_t *env_ptr = (env_t *)CFG_ENV_ADDR; |
| 74 | #ifdef CMD_SAVEENV |
| 75 | static env_t *flash_addr = (env_t *)CFG_ENV_ADDR; |
| 76 | #endif |
| 77 | |
| 78 | #endif /* ENV_IS_EMBEDDED */ |
| 79 | |
| 80 | #ifdef CFG_ENV_ADDR_REDUND |
| 81 | static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND; |
| 82 | |
| 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; |
| 85 | |
| 86 | static uchar active_flag = 1; |
| 87 | static uchar obsolete_flag = 0; |
| 88 | #endif |
| 89 | |
| 90 | extern uchar default_environment[]; |
| 91 | extern int default_environment_size; |
| 92 | |
| 93 | |
| 94 | uchar env_get_char_spec (int index) |
| 95 | { |
| 96 | DECLARE_GLOBAL_DATA_PTR; |
| 97 | |
| 98 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 99 | } |
| 100 | |
| 101 | #ifdef CFG_ENV_ADDR_REDUND |
| 102 | |
| 103 | int env_init(void) |
| 104 | { |
| 105 | DECLARE_GLOBAL_DATA_PTR; |
| 106 | |
| 107 | int crc1_ok = |
| 108 | (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 109 | int crc2_ok = |
| 110 | (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 111 | |
| 112 | uchar flag1 = flash_addr->flags; |
| 113 | uchar flag2 = flash_addr_new->flags; |
| 114 | |
| 115 | ulong addr_default = (ulong)&default_environment[0]; |
| 116 | ulong addr1 = (ulong)&(flash_addr->data); |
| 117 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 118 | |
| 119 | if (crc1_ok && ! crc2_ok) |
| 120 | { |
| 121 | gd->env_addr = addr1; |
| 122 | gd->env_valid = 1; |
| 123 | } |
| 124 | else if (! crc1_ok && crc2_ok) |
| 125 | { |
| 126 | gd->env_addr = addr2; |
| 127 | gd->env_valid = 1; |
| 128 | } |
| 129 | else if (! crc1_ok && ! crc2_ok) |
| 130 | { |
| 131 | gd->env_addr = addr_default; |
| 132 | gd->env_valid = 0; |
| 133 | } |
| 134 | else if (flag1 == active_flag && flag2 == obsolete_flag) |
| 135 | { |
| 136 | gd->env_addr = addr1; |
| 137 | gd->env_valid = 1; |
| 138 | } |
| 139 | else if (flag1 == obsolete_flag && flag2 == active_flag) |
| 140 | { |
| 141 | gd->env_addr = addr2; |
| 142 | gd->env_valid = 1; |
| 143 | } |
| 144 | else if (flag1 == flag2) |
| 145 | { |
| 146 | gd->env_addr = addr1; |
| 147 | gd->env_valid = 2; |
| 148 | } |
| 149 | else if (flag1 == 0xFF) |
| 150 | { |
| 151 | gd->env_addr = addr1; |
| 152 | gd->env_valid = 2; |
| 153 | } |
| 154 | else if (flag2 == 0xFF) |
| 155 | { |
| 156 | gd->env_addr = addr2; |
| 157 | gd->env_valid = 2; |
| 158 | } |
| 159 | |
| 160 | return (0); |
| 161 | } |
| 162 | |
| 163 | #ifdef CMD_SAVEENV |
| 164 | int saveenv(void) |
| 165 | { |
| 166 | int rc = 1; |
| 167 | |
| 168 | debug ("Protect off %08lX ... %08lX\n", |
| 169 | (ulong)flash_addr, end_addr); |
| 170 | |
| 171 | if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) { |
| 172 | goto Done; |
| 173 | } |
| 174 | |
| 175 | debug ("Protect off %08lX ... %08lX\n", |
| 176 | (ulong)flash_addr_new, end_addr_new); |
| 177 | |
| 178 | if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) { |
| 179 | goto Done; |
| 180 | } |
| 181 | |
| 182 | puts ("Erasing Flash..."); |
| 183 | debug (" %08lX ... %08lX ...", |
| 184 | (ulong)flash_addr_new, end_addr_new); |
| 185 | |
| 186 | if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) { |
| 187 | goto Done; |
| 188 | } |
| 189 | |
| 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)) || |
| 197 | |
| 198 | flash_write((char *)&(env_ptr->crc), |
| 199 | (ulong)&(flash_addr_new->crc), |
| 200 | sizeof(env_ptr->crc)) || |
| 201 | |
| 202 | flash_write((char *)&obsolete_flag, |
| 203 | (ulong)&(flash_addr->flags), |
| 204 | sizeof(flash_addr->flags)) || |
| 205 | |
| 206 | flash_write((char *)&active_flag, |
| 207 | (ulong)&(flash_addr_new->flags), |
| 208 | sizeof(flash_addr_new->flags))) |
| 209 | { |
| 210 | flash_perror (rc); |
| 211 | goto Done; |
| 212 | } |
| 213 | puts ("done\n"); |
| 214 | |
| 215 | { |
| 216 | env_t * etmp = flash_addr; |
| 217 | ulong ltmp = end_addr; |
| 218 | |
| 219 | flash_addr = flash_addr_new; |
| 220 | flash_addr_new = etmp; |
| 221 | |
| 222 | end_addr = end_addr_new; |
| 223 | end_addr_new = ltmp; |
| 224 | } |
| 225 | |
| 226 | rc = 0; |
| 227 | Done: |
| 228 | |
| 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); |
| 232 | |
| 233 | return rc; |
| 234 | } |
| 235 | #endif /* CMD_SAVEENV */ |
| 236 | |
| 237 | #else /* ! CFG_ENV_ADDR_REDUND */ |
| 238 | |
| 239 | int env_init(void) |
| 240 | { |
| 241 | DECLARE_GLOBAL_DATA_PTR; |
| 242 | |
| 243 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 244 | gd->env_addr = (ulong)&(env_ptr->data); |
| 245 | gd->env_valid = 1; |
| 246 | } else { |
| 247 | gd->env_addr = (ulong)&default_environment[0]; |
| 248 | gd->env_valid = 0; |
| 249 | } |
| 250 | |
| 251 | return (0); |
| 252 | } |
| 253 | |
| 254 | #ifdef CMD_SAVEENV |
| 255 | |
| 256 | int saveenv(void) |
| 257 | { |
| 258 | int len, rc; |
| 259 | ulong end_addr; |
| 260 | ulong flash_sect_addr; |
| 261 | #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) |
| 262 | ulong flash_offset; |
| 263 | uchar env_buffer[CFG_ENV_SECT_SIZE]; |
| 264 | #else |
| 265 | uchar *env_buffer = (char *)env_ptr; |
| 266 | #endif /* CFG_ENV_SECT_SIZE */ |
| 267 | int rcode = 0; |
| 268 | |
| 269 | #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) |
| 270 | |
| 271 | flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1); |
| 272 | flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1); |
| 273 | |
| 274 | debug ( "copy old content: " |
| 275 | "sect_addr: %08lX env_addr: %08lX offset: %08lX\n", |
| 276 | flash_sect_addr, (ulong)flash_addr, flash_offset); |
| 277 | |
| 278 | /* copy old contents to temporary buffer */ |
| 279 | memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE); |
| 280 | |
| 281 | /* copy current environment to temporary buffer */ |
| 282 | memcpy ((uchar *)((unsigned long)env_buffer + flash_offset), |
| 283 | env_ptr, |
| 284 | CFG_ENV_SIZE); |
| 285 | |
| 286 | len = CFG_ENV_SECT_SIZE; |
| 287 | #else |
| 288 | flash_sect_addr = (ulong)flash_addr; |
| 289 | len = CFG_ENV_SIZE; |
| 290 | #endif /* CFG_ENV_SECT_SIZE */ |
| 291 | |
| 292 | #ifndef CONFIG_INFERNO |
| 293 | end_addr = flash_sect_addr + len - 1; |
| 294 | #else |
| 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; |
| 298 | #endif |
| 299 | |
| 300 | debug ("Protect off %08lX ... %08lX\n", |
| 301 | (ulong)flash_sect_addr, end_addr); |
| 302 | |
| 303 | if (flash_sect_protect (0, flash_sect_addr, end_addr)) |
| 304 | return 1; |
| 305 | |
| 306 | puts ("Erasing Flash..."); |
| 307 | if (flash_sect_erase (flash_sect_addr, end_addr)) |
| 308 | return 1; |
| 309 | |
| 310 | puts ("Writing to Flash... "); |
| 311 | rc = flash_write(env_buffer, flash_sect_addr, len); |
| 312 | if (rc != 0) { |
| 313 | flash_perror (rc); |
| 314 | rcode = 1; |
| 315 | } else { |
| 316 | puts ("done\n"); |
| 317 | } |
| 318 | |
| 319 | /* try to re-protect */ |
| 320 | (void) flash_sect_protect (1, flash_sect_addr, end_addr); |
| 321 | return rcode; |
| 322 | } |
| 323 | |
| 324 | #endif /* CMD_SAVEENV */ |
| 325 | |
| 326 | #endif /* CFG_ENV_ADDR_REDUND */ |
| 327 | |
| 328 | void env_relocate_spec (void) |
| 329 | { |
| 330 | #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND) |
| 331 | #ifdef CFG_ENV_ADDR_REDUND |
| 332 | DECLARE_GLOBAL_DATA_PTR; |
| 333 | |
| 334 | if (gd->env_addr != (ulong)&(flash_addr->data)) |
| 335 | { |
| 336 | env_t * etmp = flash_addr; |
| 337 | ulong ltmp = end_addr; |
| 338 | |
| 339 | flash_addr = flash_addr_new; |
| 340 | flash_addr_new = etmp; |
| 341 | |
| 342 | end_addr = end_addr_new; |
| 343 | end_addr_new = ltmp; |
| 344 | } |
| 345 | |
| 346 | if (flash_addr_new->flags != obsolete_flag && |
| 347 | crc32(0, flash_addr_new->data, ENV_SIZE) == |
| 348 | flash_addr_new->crc) |
| 349 | { |
| 350 | gd->env_valid = 2; |
| 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); |
| 356 | } |
| 357 | |
| 358 | if (flash_addr->flags != active_flag && |
| 359 | (flash_addr->flags & active_flag) == active_flag) |
| 360 | { |
| 361 | gd->env_valid = 2; |
| 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); |
| 367 | } |
| 368 | |
| 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 */ |
| 375 | } |
| 376 | |
wdenk | 4a5c8a7 | 2003-03-06 00:02:04 +0000 | [diff] [blame^] | 377 | #endif /* CFG_ENV_IS_IN_FLASH */ |