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> |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame^] | 37 | #include <malloc.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | |
| 39 | #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH)) |
| 40 | #define CMD_SAVEENV |
| 41 | #elif defined(CFG_ENV_ADDR_REDUND) |
| 42 | #error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH |
| 43 | #endif |
| 44 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE) |
| 46 | #error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE |
| 47 | #endif |
| 48 | |
| 49 | #ifdef CONFIG_INFERNO |
| 50 | # ifdef CFG_ENV_ADDR_REDUND |
| 51 | #error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO |
| 52 | # endif |
| 53 | #endif |
| 54 | |
| 55 | char * env_name_spec = "Flash"; |
| 56 | |
| 57 | #ifdef ENV_IS_EMBEDDED |
| 58 | |
| 59 | extern uchar environment[]; |
| 60 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 61 | |
| 62 | #ifdef CMD_SAVEENV |
| 63 | /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/ |
| 64 | static env_t *flash_addr = (env_t *)CFG_ENV_ADDR; |
| 65 | #endif |
| 66 | |
| 67 | #else /* ! ENV_IS_EMBEDDED */ |
| 68 | |
| 69 | env_t *env_ptr = (env_t *)CFG_ENV_ADDR; |
| 70 | #ifdef CMD_SAVEENV |
| 71 | static env_t *flash_addr = (env_t *)CFG_ENV_ADDR; |
| 72 | #endif |
| 73 | |
| 74 | #endif /* ENV_IS_EMBEDDED */ |
| 75 | |
| 76 | #ifdef CFG_ENV_ADDR_REDUND |
| 77 | static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND; |
| 78 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame^] | 79 | /* CFG_ENV_ADDR is supposed to be on sector boundary */ |
| 80 | static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1; |
| 81 | 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] | 82 | |
| 83 | static uchar active_flag = 1; |
| 84 | static uchar obsolete_flag = 0; |
| 85 | #endif |
| 86 | |
| 87 | extern uchar default_environment[]; |
| 88 | extern int default_environment_size; |
| 89 | |
| 90 | |
| 91 | uchar env_get_char_spec (int index) |
| 92 | { |
| 93 | DECLARE_GLOBAL_DATA_PTR; |
| 94 | |
| 95 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 96 | } |
| 97 | |
| 98 | #ifdef CFG_ENV_ADDR_REDUND |
| 99 | |
| 100 | int env_init(void) |
| 101 | { |
| 102 | DECLARE_GLOBAL_DATA_PTR; |
| 103 | |
| 104 | int crc1_ok = |
| 105 | (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 106 | int crc2_ok = |
| 107 | (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 108 | |
| 109 | uchar flag1 = flash_addr->flags; |
| 110 | uchar flag2 = flash_addr_new->flags; |
| 111 | |
| 112 | ulong addr_default = (ulong)&default_environment[0]; |
| 113 | ulong addr1 = (ulong)&(flash_addr->data); |
| 114 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 115 | |
| 116 | if (crc1_ok && ! crc2_ok) |
| 117 | { |
| 118 | gd->env_addr = addr1; |
| 119 | gd->env_valid = 1; |
| 120 | } |
| 121 | else if (! crc1_ok && crc2_ok) |
| 122 | { |
| 123 | gd->env_addr = addr2; |
| 124 | gd->env_valid = 1; |
| 125 | } |
| 126 | else if (! crc1_ok && ! crc2_ok) |
| 127 | { |
| 128 | gd->env_addr = addr_default; |
| 129 | gd->env_valid = 0; |
| 130 | } |
| 131 | else if (flag1 == active_flag && flag2 == obsolete_flag) |
| 132 | { |
| 133 | gd->env_addr = addr1; |
| 134 | gd->env_valid = 1; |
| 135 | } |
| 136 | else if (flag1 == obsolete_flag && flag2 == active_flag) |
| 137 | { |
| 138 | gd->env_addr = addr2; |
| 139 | gd->env_valid = 1; |
| 140 | } |
| 141 | else if (flag1 == flag2) |
| 142 | { |
| 143 | gd->env_addr = addr1; |
| 144 | gd->env_valid = 2; |
| 145 | } |
| 146 | else if (flag1 == 0xFF) |
| 147 | { |
| 148 | gd->env_addr = addr1; |
| 149 | gd->env_valid = 2; |
| 150 | } |
| 151 | else if (flag2 == 0xFF) |
| 152 | { |
| 153 | gd->env_addr = addr2; |
| 154 | gd->env_valid = 2; |
| 155 | } |
| 156 | |
| 157 | return (0); |
| 158 | } |
| 159 | |
| 160 | #ifdef CMD_SAVEENV |
| 161 | int saveenv(void) |
| 162 | { |
| 163 | int rc = 1; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame^] | 164 | ulong up_data = 0; |
| 165 | char *saved_data = NULL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | |
| 167 | debug ("Protect off %08lX ... %08lX\n", |
| 168 | (ulong)flash_addr, end_addr); |
| 169 | |
| 170 | if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) { |
| 171 | goto Done; |
| 172 | } |
| 173 | |
| 174 | debug ("Protect off %08lX ... %08lX\n", |
| 175 | (ulong)flash_addr_new, end_addr_new); |
| 176 | |
| 177 | if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) { |
| 178 | goto Done; |
| 179 | } |
| 180 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame^] | 181 | #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE |
| 182 | up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE)); |
| 183 | debug ("Data to save 0x%x\n", up_data); |
| 184 | if (up_data) { |
| 185 | if ((saved_data = malloc(up_data)) == NULL) { |
| 186 | printf("Unable to save the rest of sector (%ld)\n", |
| 187 | up_data); |
| 188 | goto Done; |
| 189 | } |
| 190 | memcpy(saved_data, |
| 191 | (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data); |
| 192 | debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n", |
| 193 | (long)flash_addr_new + CFG_ENV_SIZE, |
| 194 | up_data, saved_data); |
| 195 | } |
| 196 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | puts ("Erasing Flash..."); |
| 198 | debug (" %08lX ... %08lX ...", |
| 199 | (ulong)flash_addr_new, end_addr_new); |
| 200 | |
| 201 | if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) { |
| 202 | goto Done; |
| 203 | } |
| 204 | |
| 205 | puts ("Writing to Flash... "); |
| 206 | debug (" %08lX ... %08lX ...", |
| 207 | (ulong)&(flash_addr_new->data), |
| 208 | sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data)); |
| 209 | if (flash_write(env_ptr->data, |
| 210 | (ulong)&(flash_addr_new->data), |
| 211 | sizeof(env_ptr->data)) || |
| 212 | |
| 213 | flash_write((char *)&(env_ptr->crc), |
| 214 | (ulong)&(flash_addr_new->crc), |
| 215 | sizeof(env_ptr->crc)) || |
| 216 | |
| 217 | flash_write((char *)&obsolete_flag, |
| 218 | (ulong)&(flash_addr->flags), |
| 219 | sizeof(flash_addr->flags)) || |
| 220 | |
| 221 | flash_write((char *)&active_flag, |
| 222 | (ulong)&(flash_addr_new->flags), |
| 223 | sizeof(flash_addr_new->flags))) |
| 224 | { |
| 225 | flash_perror (rc); |
| 226 | goto Done; |
| 227 | } |
| 228 | puts ("done\n"); |
| 229 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame^] | 230 | #if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE |
| 231 | if (up_data) { /* restore the rest of sector */ |
| 232 | debug ("Restoring the rest of data to 0x%x len 0x%x\n", |
| 233 | (long)flash_addr_new + CFG_ENV_SIZE, up_data); |
| 234 | if (flash_write(saved_data, |
| 235 | (long)flash_addr_new + CFG_ENV_SIZE, |
| 236 | up_data)) { |
| 237 | flash_perror(rc); |
| 238 | goto Done; |
| 239 | } |
| 240 | } |
| 241 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 242 | { |
| 243 | env_t * etmp = flash_addr; |
| 244 | ulong ltmp = end_addr; |
| 245 | |
| 246 | flash_addr = flash_addr_new; |
| 247 | flash_addr_new = etmp; |
| 248 | |
| 249 | end_addr = end_addr_new; |
| 250 | end_addr_new = ltmp; |
| 251 | } |
| 252 | |
| 253 | rc = 0; |
| 254 | Done: |
| 255 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame^] | 256 | if (saved_data) |
| 257 | free (saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 258 | /* try to re-protect */ |
| 259 | (void) flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 260 | (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 261 | |
| 262 | return rc; |
| 263 | } |
| 264 | #endif /* CMD_SAVEENV */ |
| 265 | |
| 266 | #else /* ! CFG_ENV_ADDR_REDUND */ |
| 267 | |
| 268 | int env_init(void) |
| 269 | { |
| 270 | DECLARE_GLOBAL_DATA_PTR; |
| 271 | |
| 272 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 273 | gd->env_addr = (ulong)&(env_ptr->data); |
| 274 | gd->env_valid = 1; |
| 275 | } else { |
| 276 | gd->env_addr = (ulong)&default_environment[0]; |
| 277 | gd->env_valid = 0; |
| 278 | } |
| 279 | |
| 280 | return (0); |
| 281 | } |
| 282 | |
| 283 | #ifdef CMD_SAVEENV |
| 284 | |
| 285 | int saveenv(void) |
| 286 | { |
| 287 | int len, rc; |
| 288 | ulong end_addr; |
| 289 | ulong flash_sect_addr; |
| 290 | #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) |
| 291 | ulong flash_offset; |
| 292 | uchar env_buffer[CFG_ENV_SECT_SIZE]; |
| 293 | #else |
| 294 | uchar *env_buffer = (char *)env_ptr; |
| 295 | #endif /* CFG_ENV_SECT_SIZE */ |
| 296 | int rcode = 0; |
| 297 | |
| 298 | #if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE) |
| 299 | |
| 300 | flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1); |
| 301 | flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1); |
| 302 | |
| 303 | debug ( "copy old content: " |
| 304 | "sect_addr: %08lX env_addr: %08lX offset: %08lX\n", |
| 305 | flash_sect_addr, (ulong)flash_addr, flash_offset); |
| 306 | |
| 307 | /* copy old contents to temporary buffer */ |
| 308 | memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE); |
| 309 | |
| 310 | /* copy current environment to temporary buffer */ |
| 311 | memcpy ((uchar *)((unsigned long)env_buffer + flash_offset), |
| 312 | env_ptr, |
| 313 | CFG_ENV_SIZE); |
| 314 | |
| 315 | len = CFG_ENV_SECT_SIZE; |
| 316 | #else |
| 317 | flash_sect_addr = (ulong)flash_addr; |
| 318 | len = CFG_ENV_SIZE; |
| 319 | #endif /* CFG_ENV_SECT_SIZE */ |
| 320 | |
| 321 | #ifndef CONFIG_INFERNO |
| 322 | end_addr = flash_sect_addr + len - 1; |
| 323 | #else |
| 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; |
| 327 | #endif |
| 328 | |
| 329 | debug ("Protect off %08lX ... %08lX\n", |
| 330 | (ulong)flash_sect_addr, end_addr); |
| 331 | |
| 332 | if (flash_sect_protect (0, flash_sect_addr, end_addr)) |
| 333 | return 1; |
| 334 | |
| 335 | puts ("Erasing Flash..."); |
| 336 | if (flash_sect_erase (flash_sect_addr, end_addr)) |
| 337 | return 1; |
| 338 | |
| 339 | puts ("Writing to Flash... "); |
| 340 | rc = flash_write(env_buffer, flash_sect_addr, len); |
| 341 | if (rc != 0) { |
| 342 | flash_perror (rc); |
| 343 | rcode = 1; |
| 344 | } else { |
| 345 | puts ("done\n"); |
| 346 | } |
| 347 | |
| 348 | /* try to re-protect */ |
| 349 | (void) flash_sect_protect (1, flash_sect_addr, end_addr); |
| 350 | return rcode; |
| 351 | } |
| 352 | |
| 353 | #endif /* CMD_SAVEENV */ |
| 354 | |
| 355 | #endif /* CFG_ENV_ADDR_REDUND */ |
| 356 | |
| 357 | void env_relocate_spec (void) |
| 358 | { |
| 359 | #if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND) |
| 360 | #ifdef CFG_ENV_ADDR_REDUND |
| 361 | DECLARE_GLOBAL_DATA_PTR; |
| 362 | |
| 363 | if (gd->env_addr != (ulong)&(flash_addr->data)) |
| 364 | { |
| 365 | env_t * etmp = flash_addr; |
| 366 | ulong ltmp = end_addr; |
| 367 | |
| 368 | flash_addr = flash_addr_new; |
| 369 | flash_addr_new = etmp; |
| 370 | |
| 371 | end_addr = end_addr_new; |
| 372 | end_addr_new = ltmp; |
| 373 | } |
| 374 | |
| 375 | if (flash_addr_new->flags != obsolete_flag && |
| 376 | crc32(0, flash_addr_new->data, ENV_SIZE) == |
| 377 | flash_addr_new->crc) |
| 378 | { |
| 379 | gd->env_valid = 2; |
| 380 | flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new); |
| 381 | flash_write((char *)&obsolete_flag, |
| 382 | (ulong)&(flash_addr_new->flags), |
| 383 | sizeof(flash_addr_new->flags)); |
| 384 | flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 385 | } |
| 386 | |
| 387 | if (flash_addr->flags != active_flag && |
| 388 | (flash_addr->flags & active_flag) == active_flag) |
| 389 | { |
| 390 | gd->env_valid = 2; |
| 391 | flash_sect_protect (0, (ulong)flash_addr, end_addr); |
| 392 | flash_write((char *)&active_flag, |
| 393 | (ulong)&(flash_addr->flags), |
| 394 | sizeof(flash_addr->flags)); |
| 395 | flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 396 | } |
| 397 | |
| 398 | if (gd->env_valid == 2) |
| 399 | puts ("*** Warning - some problems detected " |
| 400 | "reading environment; recovered successfully\n\n"); |
| 401 | #endif /* CFG_ENV_ADDR_REDUND */ |
| 402 | memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE); |
| 403 | #endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */ |
| 404 | } |
| 405 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 406 | #endif /* CFG_ENV_IS_IN_FLASH */ |