wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Jian Zhang, Texas Instruments, jzhang@ti.com. |
| 4 | |
| 5 | * (C) Copyright 2000-2004 |
| 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 | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 39 | #include <malloc.h> |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 40 | #include <nand.h> |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 41 | |
| 42 | #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND)) |
| 43 | #define CMD_SAVEENV |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 44 | #elif defined(CFG_ENV_OFFSET_REDUND) |
| 45 | #error Cannot use CFG_ENV_OFFSET_REDUND without CFG_CMD_ENV & CFG_CMD_NAND |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Markus Klotzbuecher | 5d113e0 | 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 | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
wdenk | 79b5937 | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 56 | int nand_legacy_rw (struct nand_chip* nand, int cmd, |
wdenk | 79b5937 | 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 | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 59 | |
| 60 | /* info for NAND chips, defined in drivers/nand/nand.c */ |
| 61 | extern nand_info_t nand_info[]; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 62 | |
| 63 | /* references to names in env_common.c */ |
| 64 | extern uchar default_environment[]; |
| 65 | extern int default_environment_size; |
| 66 | |
| 67 | char * env_name_spec = "NAND"; |
| 68 | |
| 69 | |
| 70 | #ifdef ENV_IS_EMBEDDED |
| 71 | extern uchar environment[]; |
| 72 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 73 | #else /* ! ENV_IS_EMBEDDED */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 74 | env_t *env_ptr = 0; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 75 | #endif /* ENV_IS_EMBEDDED */ |
| 76 | |
| 77 | |
| 78 | /* local functions */ |
| 79 | static void use_default(void); |
| 80 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 81 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 82 | |
| 83 | uchar env_get_char_spec (int index) |
| 84 | { |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 85 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* this is called before nand_init() |
| 90 | * so we can't read Nand to validate env data. |
| 91 | * Mark it OK for now. env_relocate() in env_common.c |
| 92 | * will call our relocate function which will does |
| 93 | * the real validation. |
| 94 | */ |
| 95 | int env_init(void) |
| 96 | { |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 97 | gd->env_addr = (ulong)&default_environment[0]; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 98 | gd->env_valid = 1; |
| 99 | |
| 100 | return (0); |
| 101 | } |
| 102 | |
| 103 | #ifdef CMD_SAVEENV |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 104 | /* |
| 105 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 106 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 107 | */ |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 108 | #ifdef CFG_ENV_OFFSET_REDUND |
| 109 | int saveenv(void) |
| 110 | { |
Wolfgang Denk | 7fa6e90 | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 111 | ulong total; |
| 112 | int ret = 0; |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 113 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 114 | env_ptr->flags++; |
| 115 | total = CFG_ENV_SIZE; |
| 116 | |
| 117 | if(gd->env_valid == 1) { |
| 118 | puts ("Erasing redundant Nand..."); |
| 119 | if (nand_erase(&nand_info[0], |
| 120 | CFG_ENV_OFFSET_REDUND, CFG_ENV_SIZE)) |
| 121 | return 1; |
| 122 | puts ("Writing to redundant Nand... "); |
| 123 | ret = nand_write(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total, |
| 124 | (u_char*) env_ptr); |
| 125 | } else { |
| 126 | puts ("Erasing Nand..."); |
| 127 | if (nand_erase(&nand_info[0], |
| 128 | CFG_ENV_OFFSET, CFG_ENV_SIZE)) |
| 129 | return 1; |
| 130 | |
| 131 | puts ("Writing to Nand... "); |
| 132 | ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, |
| 133 | (u_char*) env_ptr); |
| 134 | } |
| 135 | if (ret || total != CFG_ENV_SIZE) |
| 136 | return 1; |
| 137 | |
| 138 | puts ("done\n"); |
| 139 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
| 140 | return ret; |
| 141 | } |
| 142 | #else /* ! CFG_ENV_OFFSET_REDUND */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 143 | int saveenv(void) |
| 144 | { |
Markus Klotzbuecher | ffe7ef5 | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 145 | ulong total; |
| 146 | int ret = 0; |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 147 | |
| 148 | puts ("Erasing Nand..."); |
Markus Klotzbuecher | be8ebab | 2006-03-08 00:04:04 +0100 | [diff] [blame] | 149 | if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE)) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 150 | return 1; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 151 | |
| 152 | puts ("Writing to Nand... "); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 153 | total = CFG_ENV_SIZE; |
Wolfgang Denk | 7fa6e90 | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 154 | ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 155 | if (ret || total != CFG_ENV_SIZE) |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 156 | return 1; |
| 157 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 158 | puts ("done\n"); |
| 159 | return ret; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 160 | } |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 161 | #endif /* CFG_ENV_OFFSET_REDUND */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 162 | #endif /* CMD_SAVEENV */ |
| 163 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 164 | #ifdef CFG_ENV_OFFSET_REDUND |
| 165 | void env_relocate_spec (void) |
| 166 | { |
| 167 | #if !defined(ENV_IS_EMBEDDED) |
Markus Klotzbuecher | ffe7ef5 | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 168 | ulong total; |
| 169 | int crc1_ok = 0, crc2_ok = 0; |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 170 | env_t *tmp_env1, *tmp_env2; |
| 171 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 172 | total = CFG_ENV_SIZE; |
| 173 | |
| 174 | tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE); |
| 175 | tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE); |
| 176 | |
| 177 | nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, |
| 178 | (u_char*) tmp_env1); |
| 179 | nand_read(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total, |
| 180 | (u_char*) tmp_env2); |
| 181 | |
| 182 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 183 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 184 | |
| 185 | if(!crc1_ok && !crc2_ok) |
| 186 | return use_default(); |
| 187 | else if(crc1_ok && !crc2_ok) |
| 188 | gd->env_valid = 1; |
| 189 | else if(!crc1_ok && crc2_ok) |
| 190 | gd->env_valid = 2; |
| 191 | else { |
| 192 | /* both ok - check serial */ |
| 193 | if(tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 194 | gd->env_valid = 2; |
| 195 | else if(tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 196 | gd->env_valid = 1; |
| 197 | else if(tmp_env1->flags > tmp_env2->flags) |
| 198 | gd->env_valid = 1; |
| 199 | else if(tmp_env2->flags > tmp_env1->flags) |
| 200 | gd->env_valid = 2; |
| 201 | else /* flags are equal - almost impossible */ |
| 202 | gd->env_valid = 1; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 203 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | free(env_ptr); |
| 207 | if(gd->env_valid == 1) { |
| 208 | env_ptr = tmp_env1; |
| 209 | free(tmp_env2); |
| 210 | } else { |
| 211 | env_ptr = tmp_env2; |
| 212 | free(tmp_env1); |
| 213 | } |
| 214 | |
| 215 | #endif /* ! ENV_IS_EMBEDDED */ |
| 216 | } |
| 217 | #else /* ! CFG_ENV_OFFSET_REDUND */ |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 218 | /* |
| 219 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 220 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 221 | */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 222 | void env_relocate_spec (void) |
| 223 | { |
| 224 | #if !defined(ENV_IS_EMBEDDED) |
Wolfgang Denk | 7fa6e90 | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 225 | ulong total; |
| 226 | int ret; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 227 | |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 228 | total = CFG_ENV_SIZE; |
Wolfgang Denk | 7fa6e90 | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 229 | ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr); |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 230 | if (ret || total != CFG_ENV_SIZE) |
| 231 | return use_default(); |
| 232 | |
| 233 | if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) |
| 234 | return use_default(); |
| 235 | #endif /* ! ENV_IS_EMBEDDED */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 236 | } |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 237 | #endif /* CFG_ENV_OFFSET_REDUND */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 238 | |
| 239 | static void use_default() |
| 240 | { |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 241 | puts ("*** Warning - bad CRC or NAND, using default environment\n\n"); |
| 242 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 243 | if (default_environment_size > CFG_ENV_SIZE){ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 244 | puts ("*** Error - default environment is too large\n\n"); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | memset (env_ptr, 0, sizeof(env_t)); |
| 249 | memcpy (env_ptr->data, |
| 250 | default_environment, |
| 251 | default_environment_size); |
| 252 | env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 253 | gd->env_valid = 1; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 254 | |
| 255 | } |
| 256 | |
| 257 | #endif /* CFG_ENV_IS_IN_NAND */ |