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> |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 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 | |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 42 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 43 | #define CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 44 | #elif defined(CONFIG_ENV_OFFSET_REDUND) |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 45 | #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 48 | #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) |
| 49 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_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 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 56 | #ifndef CONFIG_ENV_RANGE |
| 57 | #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 58 | #endif |
| 59 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 60 | int nand_legacy_rw (struct nand_chip* nand, int cmd, |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 61 | size_t start, size_t len, |
| 62 | size_t * retlen, u_char * buf); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 63 | |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 64 | /* references to names in env_common.c */ |
| 65 | extern uchar default_environment[]; |
| 66 | extern int default_environment_size; |
| 67 | |
| 68 | char * env_name_spec = "NAND"; |
| 69 | |
| 70 | |
| 71 | #ifdef ENV_IS_EMBEDDED |
| 72 | extern uchar environment[]; |
| 73 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 74 | #else /* ! ENV_IS_EMBEDDED */ |
wdenk | 49822e2 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 75 | env_t *env_ptr = 0; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 76 | #endif /* ENV_IS_EMBEDDED */ |
| 77 | |
| 78 | |
| 79 | /* local functions */ |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 80 | #if !defined(ENV_IS_EMBEDDED) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 81 | static void use_default(void); |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 82 | #endif |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 83 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 84 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 85 | |
| 86 | uchar env_get_char_spec (int index) |
| 87 | { |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 88 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /* this is called before nand_init() |
| 93 | * so we can't read Nand to validate env data. |
| 94 | * Mark it OK for now. env_relocate() in env_common.c |
Marcel Ziswiler | 99c2b43 | 2008-06-22 16:13:46 +0200 | [diff] [blame] | 95 | * will call our relocate function which does the real |
| 96 | * validation. |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 97 | * |
| 98 | * When using a NAND boot image (like sequoia_nand), the environment |
| 99 | * can be embedded or attached to the U-Boot image in NAND flash. This way |
| 100 | * the SPL loads not only the U-Boot image from NAND but also the |
| 101 | * environment. |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 102 | */ |
| 103 | int env_init(void) |
| 104 | { |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 105 | #if defined(ENV_IS_EMBEDDED) |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 106 | int crc1_ok = 0, crc2_ok = 0; |
| 107 | env_t *tmp_env1, *tmp_env2; |
| 108 | |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 109 | tmp_env1 = env_ptr; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 110 | tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE); |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 111 | |
| 112 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 113 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 114 | |
| 115 | if (!crc1_ok && !crc2_ok) |
| 116 | gd->env_valid = 0; |
| 117 | else if(crc1_ok && !crc2_ok) |
| 118 | gd->env_valid = 1; |
| 119 | else if(!crc1_ok && crc2_ok) |
| 120 | gd->env_valid = 2; |
| 121 | else { |
| 122 | /* both ok - check serial */ |
| 123 | if(tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 124 | gd->env_valid = 2; |
| 125 | else if(tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 126 | gd->env_valid = 1; |
| 127 | else if(tmp_env1->flags > tmp_env2->flags) |
| 128 | gd->env_valid = 1; |
| 129 | else if(tmp_env2->flags > tmp_env1->flags) |
| 130 | gd->env_valid = 2; |
| 131 | else /* flags are equal - almost impossible */ |
| 132 | gd->env_valid = 1; |
| 133 | } |
| 134 | |
| 135 | if (gd->env_valid == 1) |
| 136 | env_ptr = tmp_env1; |
| 137 | else if (gd->env_valid == 2) |
| 138 | env_ptr = tmp_env2; |
| 139 | #else /* ENV_IS_EMBEDDED */ |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 140 | gd->env_addr = (ulong)&default_environment[0]; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 141 | gd->env_valid = 1; |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 142 | #endif /* ENV_IS_EMBEDDED */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 143 | |
| 144 | return (0); |
| 145 | } |
| 146 | |
| 147 | #ifdef CMD_SAVEENV |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 148 | /* |
| 149 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 150 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 151 | */ |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 152 | int writeenv(size_t offset, u_char *buf) |
| 153 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 154 | size_t end = offset + CONFIG_ENV_RANGE; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 155 | size_t amount_saved = 0; |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 156 | size_t blocksize, len; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 157 | |
| 158 | u_char *char_ptr; |
| 159 | |
| 160 | blocksize = nand_info[0].erasesize; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 161 | len = min(blocksize, CONFIG_ENV_SIZE); |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 162 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 163 | while (amount_saved < CONFIG_ENV_SIZE && offset < end) { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 164 | if (nand_block_isbad(&nand_info[0], offset)) { |
| 165 | offset += blocksize; |
| 166 | } else { |
| 167 | char_ptr = &buf[amount_saved]; |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 168 | if (nand_write(&nand_info[0], offset, &len, |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 169 | char_ptr)) |
| 170 | return 1; |
| 171 | offset += blocksize; |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 172 | amount_saved += len; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 175 | if (amount_saved != CONFIG_ENV_SIZE) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 176 | return 1; |
| 177 | |
| 178 | return 0; |
| 179 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 180 | #ifdef CONFIG_ENV_OFFSET_REDUND |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 181 | int saveenv(void) |
| 182 | { |
Markus Klotzbuecher | 2770bcb | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 183 | int ret = 0; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 184 | nand_erase_options_t nand_erase_options; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 185 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 186 | env_ptr->flags++; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 187 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 188 | nand_erase_options.length = CONFIG_ENV_RANGE; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 189 | nand_erase_options.quiet = 0; |
| 190 | nand_erase_options.jffs2 = 0; |
| 191 | nand_erase_options.scrub = 0; |
| 192 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 193 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 194 | return 1; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 195 | if(gd->env_valid == 1) { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 196 | puts ("Erasing redundant Nand...\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 197 | nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 198 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 199 | return 1; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 200 | |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 201 | puts ("Writing to redundant Nand... "); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 202 | ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 203 | } else { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 204 | puts ("Erasing Nand...\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 205 | nand_erase_options.offset = CONFIG_ENV_OFFSET; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 206 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 207 | return 1; |
| 208 | |
| 209 | puts ("Writing to Nand... "); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 210 | ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 211 | } |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 212 | if (ret) { |
| 213 | puts("FAILED!\n"); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 214 | return 1; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 215 | } |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 216 | |
| 217 | puts ("done\n"); |
| 218 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
| 219 | return ret; |
| 220 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 221 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 222 | int saveenv(void) |
| 223 | { |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 224 | int ret = 0; |
Philip Balister | 9e4006bc | 2008-06-16 08:58:07 -0400 | [diff] [blame] | 225 | nand_erase_options_t nand_erase_options; |
Wolfgang Denk | e093a24 | 2008-06-28 23:34:37 +0200 | [diff] [blame] | 226 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 227 | nand_erase_options.length = CONFIG_ENV_RANGE; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 228 | nand_erase_options.quiet = 0; |
| 229 | nand_erase_options.jffs2 = 0; |
| 230 | nand_erase_options.scrub = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 231 | nand_erase_options.offset = CONFIG_ENV_OFFSET; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 232 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 233 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 234 | return 1; |
| 235 | puts ("Erasing Nand...\n"); |
| 236 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 237 | return 1; |
| 238 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 239 | puts ("Writing to Nand... "); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 240 | if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 241 | puts("FAILED!\n"); |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 242 | return 1; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 243 | } |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 244 | |
| 245 | puts ("done\n"); |
| 246 | return ret; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 247 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 248 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 249 | #endif /* CMD_SAVEENV */ |
| 250 | |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 251 | int readenv (size_t offset, u_char * buf) |
| 252 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 253 | size_t end = offset + CONFIG_ENV_RANGE; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 254 | size_t amount_loaded = 0; |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 255 | size_t blocksize, len; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 256 | |
| 257 | u_char *char_ptr; |
| 258 | |
| 259 | blocksize = nand_info[0].erasesize; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 260 | len = min(blocksize, CONFIG_ENV_SIZE); |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 261 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 262 | while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 263 | if (nand_block_isbad(&nand_info[0], offset)) { |
| 264 | offset += blocksize; |
| 265 | } else { |
| 266 | char_ptr = &buf[amount_loaded]; |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 267 | if (nand_read(&nand_info[0], offset, &len, char_ptr)) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 268 | return 1; |
| 269 | offset += blocksize; |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 270 | amount_loaded += len; |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 271 | } |
| 272 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 273 | if (amount_loaded != CONFIG_ENV_SIZE) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 274 | return 1; |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 279 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 280 | void env_relocate_spec (void) |
| 281 | { |
| 282 | #if !defined(ENV_IS_EMBEDDED) |
Markus Klotzbuecher | 2770bcb | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 283 | int crc1_ok = 0, crc2_ok = 0; |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 284 | env_t *tmp_env1, *tmp_env2; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 285 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 286 | tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE); |
| 287 | tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 288 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 289 | if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1)) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 290 | puts("No Valid Environment Area Found\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 291 | if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2)) |
Stuart Wood | cc49cad | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 292 | puts("No Valid Reundant Environment Area Found\n"); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 293 | |
| 294 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 295 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 296 | |
derek@siconix.com | 5a9427d | 2009-01-26 14:08:17 -0700 | [diff] [blame] | 297 | if(!crc1_ok && !crc2_ok) { |
| 298 | free(tmp_env1); |
| 299 | free(tmp_env2); |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 300 | return use_default(); |
derek@siconix.com | 5a9427d | 2009-01-26 14:08:17 -0700 | [diff] [blame] | 301 | } else if(crc1_ok && !crc2_ok) |
Markus Klotzbuecher | e443c94 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 302 | gd->env_valid = 1; |
| 303 | else if(!crc1_ok && crc2_ok) |
| 304 | gd->env_valid = 2; |
| 305 | else { |
| 306 | /* both ok - check serial */ |
| 307 | if(tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 308 | gd->env_valid = 2; |
| 309 | else if(tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 310 | gd->env_valid = 1; |
| 311 | else if(tmp_env1->flags > tmp_env2->flags) |
| 312 | gd->env_valid = 1; |
| 313 | else if(tmp_env2->flags > tmp_env1->flags) |
| 314 | gd->env_valid = 2; |
| 315 | else /* flags are equal - almost impossible */ |
| 316 | gd->env_valid = 1; |
| 317 | |
| 318 | } |
| 319 | |
| 320 | free(env_ptr); |
| 321 | if(gd->env_valid == 1) { |
| 322 | env_ptr = tmp_env1; |
| 323 | free(tmp_env2); |
| 324 | } else { |
| 325 | env_ptr = tmp_env2; |
| 326 | free(tmp_env1); |
| 327 | } |
| 328 | |
| 329 | #endif /* ! ENV_IS_EMBEDDED */ |
| 330 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 331 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 332 | /* |
| 333 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 334 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 335 | */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 336 | void env_relocate_spec (void) |
| 337 | { |
| 338 | #if !defined(ENV_IS_EMBEDDED) |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 339 | int ret; |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 340 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 341 | ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr); |
Guennadi Liakhovetski | c3db8c6 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 342 | if (ret) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 343 | return use_default(); |
| 344 | |
| 345 | if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc) |
| 346 | return use_default(); |
| 347 | #endif /* ! ENV_IS_EMBEDDED */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 348 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 349 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 350 | |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 351 | #if !defined(ENV_IS_EMBEDDED) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 352 | static void use_default() |
| 353 | { |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 354 | puts ("*** Warning - bad CRC or NAND, using default environment\n\n"); |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 355 | set_default_env(); |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 356 | } |
Stefan Roese | d12ae80 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 357 | #endif |