wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 1 | /* |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 2 | * (C) Copyright 2008 |
| 3 | * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com> |
| 4 | * |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 5 | * (C) Copyright 2004 |
| 6 | * Jian Zhang, Texas Instruments, jzhang@ti.com. |
| 7 | |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 8 | * (C) Copyright 2000-2006 |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 9 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 10 | * |
| 11 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 12 | * Andreas Heppel <aheppel@sysgo.de> |
| 13 | |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | */ |
| 32 | |
| 33 | /* #define DEBUG */ |
| 34 | |
| 35 | #include <common.h> |
| 36 | |
| 37 | #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */ |
| 38 | |
| 39 | #include <command.h> |
| 40 | #include <environment.h> |
| 41 | #include <linux/stddef.h> |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 42 | #include <malloc.h> |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 43 | #include <nand.h> |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 44 | |
Jon Loeliger | c3517f9 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 45 | #if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 46 | #define CMD_SAVEENV |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 47 | #elif defined(CFG_ENV_OFFSET_REDUND) |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 48 | #error Cannot use CFG_ENV_OFFSET_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_NAND |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 51 | #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE) |
| 52 | #error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 55 | #ifdef CONFIG_INFERNO |
| 56 | #error CONFIG_INFERNO not supported yet |
| 57 | #endif |
| 58 | |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 59 | #ifndef CFG_ENV_RANGE |
| 60 | #define CFG_ENV_RANGE CFG_ENV_SIZE |
| 61 | #endif |
| 62 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 63 | int nand_legacy_rw (struct nand_chip* nand, int cmd, |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 64 | size_t start, size_t len, |
| 65 | size_t * retlen, u_char * buf); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 66 | |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 67 | /* references to names in env_common.c */ |
| 68 | extern uchar default_environment[]; |
| 69 | extern int default_environment_size; |
| 70 | |
| 71 | char * env_name_spec = "NAND"; |
| 72 | |
| 73 | |
| 74 | #ifdef ENV_IS_EMBEDDED |
| 75 | extern uchar environment[]; |
| 76 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 77 | #else /* ! ENV_IS_EMBEDDED */ |
wdenk | 49822e2 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 78 | env_t *env_ptr = 0; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 79 | #endif /* ENV_IS_EMBEDDED */ |
| 80 | |
| 81 | |
| 82 | /* local functions */ |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 83 | #if !defined(ENV_IS_EMBEDDED) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 84 | static void use_default(void); |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 85 | #endif |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 86 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 87 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 88 | |
| 89 | uchar env_get_char_spec (int index) |
| 90 | { |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 91 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* this is called before nand_init() |
| 96 | * so we can't read Nand to validate env data. |
| 97 | * Mark it OK for now. env_relocate() in env_common.c |
| 98 | * will call our relocate function which will does |
| 99 | * the real validation. |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 100 | * |
| 101 | * When using a NAND boot image (like sequoia_nand), the environment |
| 102 | * can be embedded or attached to the U-Boot image in NAND flash. This way |
| 103 | * the SPL loads not only the U-Boot image from NAND but also the |
| 104 | * environment. |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 105 | */ |
| 106 | int env_init(void) |
| 107 | { |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 108 | #if defined(ENV_IS_EMBEDDED) |
dirk.behme@googlemail.com | 378e7ec | 2008-04-30 18:02:59 +0200 | [diff] [blame] | 109 | size_t total; |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 110 | int crc1_ok = 0, crc2_ok = 0; |
| 111 | env_t *tmp_env1, *tmp_env2; |
| 112 | |
| 113 | total = CFG_ENV_SIZE; |
| 114 | |
| 115 | tmp_env1 = env_ptr; |
| 116 | tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE); |
| 117 | |
| 118 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 119 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 120 | |
| 121 | if (!crc1_ok && !crc2_ok) |
| 122 | gd->env_valid = 0; |
| 123 | else if(crc1_ok && !crc2_ok) |
| 124 | gd->env_valid = 1; |
| 125 | else if(!crc1_ok && crc2_ok) |
| 126 | gd->env_valid = 2; |
| 127 | else { |
| 128 | /* both ok - check serial */ |
| 129 | if(tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 130 | gd->env_valid = 2; |
| 131 | else if(tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 132 | gd->env_valid = 1; |
| 133 | else if(tmp_env1->flags > tmp_env2->flags) |
| 134 | gd->env_valid = 1; |
| 135 | else if(tmp_env2->flags > tmp_env1->flags) |
| 136 | gd->env_valid = 2; |
| 137 | else /* flags are equal - almost impossible */ |
| 138 | gd->env_valid = 1; |
| 139 | } |
| 140 | |
| 141 | if (gd->env_valid == 1) |
| 142 | env_ptr = tmp_env1; |
| 143 | else if (gd->env_valid == 2) |
| 144 | env_ptr = tmp_env2; |
| 145 | #else /* ENV_IS_EMBEDDED */ |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 146 | gd->env_addr = (ulong)&default_environment[0]; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 147 | gd->env_valid = 1; |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 148 | #endif /* ENV_IS_EMBEDDED */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 149 | |
| 150 | return (0); |
| 151 | } |
| 152 | |
| 153 | #ifdef CMD_SAVEENV |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 154 | /* |
| 155 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 156 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 157 | */ |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 158 | int writeenv(size_t offset, u_char *buf) |
| 159 | { |
| 160 | size_t end = offset + CFG_ENV_RANGE; |
| 161 | size_t amount_saved = 0; |
| 162 | size_t blocksize; |
| 163 | |
| 164 | u_char *char_ptr; |
| 165 | |
| 166 | blocksize = nand_info[0].erasesize; |
| 167 | |
| 168 | while (amount_saved < CFG_ENV_SIZE && offset < end) { |
| 169 | if (nand_block_isbad(&nand_info[0], offset)) { |
| 170 | offset += blocksize; |
| 171 | } else { |
| 172 | char_ptr = &buf[amount_saved]; |
| 173 | if (nand_write(&nand_info[0], offset, &blocksize, |
| 174 | char_ptr)) |
| 175 | return 1; |
| 176 | offset += blocksize; |
| 177 | amount_saved += blocksize; |
| 178 | } |
| 179 | } |
| 180 | if (amount_saved != CFG_ENV_SIZE) |
| 181 | return 1; |
| 182 | |
| 183 | return 0; |
| 184 | } |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 185 | #ifdef CFG_ENV_OFFSET_REDUND |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 186 | int saveenv(void) |
| 187 | { |
Wolfgang Denk | 4ca79f4 | 2008-04-28 12:08:18 +0200 | [diff] [blame] | 188 | size_t total; |
Markus Klotzbuecher | 2770bcb | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 189 | int ret = 0; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 190 | nand_erase_options_t nand_erase_options; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 191 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 192 | env_ptr->flags++; |
| 193 | total = CFG_ENV_SIZE; |
| 194 | |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 195 | nand_erase_options.length = CFG_ENV_RANGE; |
| 196 | nand_erase_options.quiet = 0; |
| 197 | nand_erase_options.jffs2 = 0; |
| 198 | nand_erase_options.scrub = 0; |
| 199 | |
| 200 | if (CFG_ENV_RANGE < CFG_ENV_SIZE) |
| 201 | return 1; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 202 | if(gd->env_valid == 1) { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 203 | puts ("Erasing redundant Nand...\n"); |
| 204 | nand_erase_options.offset = CFG_ENV_OFFSET_REDUND; |
| 205 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 206 | return 1; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 207 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 208 | puts ("Writing to redundant Nand... "); |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 209 | ret = writeenv(CFG_ENV_OFFSET_REDUND, (u_char *) env_ptr); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 210 | } else { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 211 | puts ("Erasing Nand...\n"); |
| 212 | nand_erase_options.offset = CFG_ENV_OFFSET; |
| 213 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 214 | return 1; |
| 215 | |
| 216 | puts ("Writing to Nand... "); |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 217 | ret = writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 218 | } |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 219 | if (ret) { |
| 220 | puts("FAILED!\n"); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 221 | return 1; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 222 | } |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 223 | |
| 224 | puts ("done\n"); |
| 225 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
| 226 | return ret; |
| 227 | } |
| 228 | #else /* ! CFG_ENV_OFFSET_REDUND */ |
| 229 | int saveenv(void) |
| 230 | { |
dirk.behme@googlemail.com | 378e7ec | 2008-04-30 18:02:59 +0200 | [diff] [blame] | 231 | size_t total; |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 232 | int ret = 0; |
Philip Balister | 9e4006bc | 2008-06-16 08:58:07 -0400 | [diff] [blame^] | 233 | nand_erase_options_t nand_erase_options; |
| 234 | |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 235 | nand_erase_options.length = CFG_ENV_RANGE; |
| 236 | nand_erase_options.quiet = 0; |
| 237 | nand_erase_options.jffs2 = 0; |
| 238 | nand_erase_options.scrub = 0; |
| 239 | nand_erase_options.offset = CFG_ENV_OFFSET; |
| 240 | |
| 241 | if (CFG_ENV_RANGE < CFG_ENV_SIZE) |
| 242 | return 1; |
| 243 | puts ("Erasing Nand...\n"); |
| 244 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 245 | return 1; |
| 246 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 247 | puts ("Writing to Nand... "); |
| 248 | total = CFG_ENV_SIZE; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 249 | if (writeenv(CFG_ENV_OFFSET, env_ptr)) { |
| 250 | puts("FAILED!\n"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 251 | return 1; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 252 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 253 | |
| 254 | puts ("done\n"); |
| 255 | return ret; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 256 | } |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 257 | #endif /* CFG_ENV_OFFSET_REDUND */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 258 | #endif /* CMD_SAVEENV */ |
| 259 | |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 260 | int readenv (size_t offset, u_char * buf) |
| 261 | { |
| 262 | size_t end = offset + CFG_ENV_RANGE; |
| 263 | size_t amount_loaded = 0; |
| 264 | size_t blocksize; |
| 265 | |
| 266 | u_char *char_ptr; |
| 267 | |
| 268 | blocksize = nand_info[0].erasesize; |
| 269 | |
| 270 | while (amount_loaded < CFG_ENV_SIZE && offset < end) { |
| 271 | if (nand_block_isbad(&nand_info[0], offset)) { |
| 272 | offset += blocksize; |
| 273 | } else { |
| 274 | char_ptr = &buf[amount_loaded]; |
| 275 | if (nand_read(&nand_info[0], offset, &blocksize, char_ptr)) |
| 276 | return 1; |
| 277 | offset += blocksize; |
| 278 | amount_loaded += blocksize; |
| 279 | } |
| 280 | } |
| 281 | if (amount_loaded != CFG_ENV_SIZE) |
| 282 | return 1; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 287 | #ifdef CFG_ENV_OFFSET_REDUND |
| 288 | void env_relocate_spec (void) |
| 289 | { |
| 290 | #if !defined(ENV_IS_EMBEDDED) |
Wolfgang Denk | f7b16a0 | 2008-04-29 23:32:20 +0200 | [diff] [blame] | 291 | size_t total; |
Markus Klotzbuecher | 2770bcb | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 292 | int crc1_ok = 0, crc2_ok = 0; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 293 | env_t *tmp_env1, *tmp_env2; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 294 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 295 | total = CFG_ENV_SIZE; |
| 296 | |
| 297 | tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE); |
| 298 | tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE); |
| 299 | |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 300 | if (readenv(CFG_ENV_OFFSET, (u_char *) tmp_env1)) |
| 301 | puts("No Valid Environment Area Found\n"); |
| 302 | if (readenv(CFG_ENV_OFFSET_REDUND, (u_char *) tmp_env2)) |
| 303 | puts("No Valid Reundant Environment Area Found\n"); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 304 | |
| 305 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 306 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 307 | |
| 308 | if(!crc1_ok && !crc2_ok) |
| 309 | return use_default(); |
| 310 | else if(crc1_ok && !crc2_ok) |
| 311 | gd->env_valid = 1; |
| 312 | else if(!crc1_ok && crc2_ok) |
| 313 | gd->env_valid = 2; |
| 314 | else { |
| 315 | /* both ok - check serial */ |
| 316 | if(tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 317 | gd->env_valid = 2; |
| 318 | else if(tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 319 | gd->env_valid = 1; |
| 320 | else if(tmp_env1->flags > tmp_env2->flags) |
| 321 | gd->env_valid = 1; |
| 322 | else if(tmp_env2->flags > tmp_env1->flags) |
| 323 | gd->env_valid = 2; |
| 324 | else /* flags are equal - almost impossible */ |
| 325 | gd->env_valid = 1; |
| 326 | |
| 327 | } |
| 328 | |
| 329 | free(env_ptr); |
| 330 | if(gd->env_valid == 1) { |
| 331 | env_ptr = tmp_env1; |
| 332 | free(tmp_env2); |
| 333 | } else { |
| 334 | env_ptr = tmp_env2; |
| 335 | free(tmp_env1); |
| 336 | } |
| 337 | |
| 338 | #endif /* ! ENV_IS_EMBEDDED */ |
| 339 | } |
| 340 | #else /* ! CFG_ENV_OFFSET_REDUND */ |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 341 | /* |
| 342 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 343 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 344 | */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 345 | void env_relocate_spec (void) |
| 346 | { |
| 347 | #if !defined(ENV_IS_EMBEDDED) |
dirk.behme@googlemail.com | 378e7ec | 2008-04-30 18:02:59 +0200 | [diff] [blame] | 348 | size_t total; |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 349 | int ret; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 350 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 351 | total = CFG_ENV_SIZE; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 352 | ret = readenv(CFG_ENV_OFFSET, env_ptr); |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 353 | if (ret || total != CFG_ENV_SIZE) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 354 | return use_default(); |
| 355 | |
| 356 | if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) |
| 357 | return use_default(); |
| 358 | #endif /* ! ENV_IS_EMBEDDED */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 359 | } |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 360 | #endif /* CFG_ENV_OFFSET_REDUND */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 361 | |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 362 | #if !defined(ENV_IS_EMBEDDED) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 363 | static void use_default() |
| 364 | { |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 365 | puts ("*** Warning - bad CRC or NAND, using default environment\n\n"); |
| 366 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 367 | if (default_environment_size > CFG_ENV_SIZE){ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 368 | puts ("*** Error - default environment is too large\n\n"); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | memset (env_ptr, 0, sizeof(env_t)); |
| 373 | memcpy (env_ptr->data, |
| 374 | default_environment, |
| 375 | default_environment_size); |
| 376 | env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 377 | gd->env_valid = 1; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 378 | |
| 379 | } |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 380 | #endif |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 381 | |
| 382 | #endif /* CFG_ENV_IS_IN_NAND */ |