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