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