wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 5 | * (C) Copyright 2008 |
| 6 | * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com> |
| 7 | * |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 8 | * (C) Copyright 2004 |
| 9 | * Jian Zhang, Texas Instruments, jzhang@ti.com. |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 10 | * |
| 11 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 12 | * Andreas Heppel <aheppel@sysgo.de> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 13 | * |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 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 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 33 | #define DEBUG |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 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> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 41 | #include <search.h> |
| 42 | #include <errno.h> |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 43 | |
Mike Frysinger | 78dcaf4 | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 44 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 45 | #define CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 46 | #elif defined(CONFIG_ENV_OFFSET_REDUND) |
Mike Frysinger | 78dcaf4 | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 47 | #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] | 48 | #endif |
| 49 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 50 | #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) |
| 51 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 54 | #ifndef CONFIG_ENV_RANGE |
| 55 | #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 56 | #endif |
| 57 | |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 58 | /* references to names in env_common.c */ |
| 59 | extern uchar default_environment[]; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 60 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 61 | char *env_name_spec = "NAND"; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 62 | |
| 63 | |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 64 | #if defined(ENV_IS_EMBEDDED) |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 65 | extern uchar environment[]; |
| 66 | env_t *env_ptr = (env_t *)(&environment[0]); |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 67 | #elif defined(CONFIG_NAND_ENV_DST) |
| 68 | env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 69 | #else /* ! ENV_IS_EMBEDDED */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 70 | env_t *env_ptr = 0; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 71 | #endif /* ENV_IS_EMBEDDED */ |
| 72 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 73 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 74 | |
| 75 | uchar env_get_char_spec (int index) |
| 76 | { |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 77 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 78 | } |
| 79 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 80 | /* |
| 81 | * This is called before nand_init() so we can't read NAND to |
| 82 | * validate env data. |
| 83 | * |
| 84 | * Mark it OK for now. env_relocate() in env_common.c will call our |
| 85 | * relocate function which does the real validation. |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 86 | * |
| 87 | * When using a NAND boot image (like sequoia_nand), the environment |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 88 | * can be embedded or attached to the U-Boot image in NAND flash. |
| 89 | * This way the SPL loads not only the U-Boot image from NAND but |
| 90 | * also the environment. |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 91 | */ |
| 92 | int env_init(void) |
| 93 | { |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 94 | #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST) |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 95 | int crc1_ok = 0, crc2_ok = 0; |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 96 | env_t *tmp_env1; |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 97 | |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 99 | env_t *tmp_env2; |
| 100 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 101 | tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE); |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 102 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 103 | #endif |
| 104 | |
| 105 | tmp_env1 = env_ptr; |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 106 | |
| 107 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 108 | |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 109 | if (!crc1_ok && !crc2_ok) { |
| 110 | gd->env_addr = 0; |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 111 | gd->env_valid = 0; |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 112 | |
| 113 | return 0; |
| 114 | } else if (crc1_ok && !crc2_ok) { |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 115 | gd->env_valid = 1; |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 116 | } |
| 117 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 118 | else if (!crc1_ok && crc2_ok) { |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 119 | gd->env_valid = 2; |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 120 | } else { |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 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 | |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 134 | if (gd->env_valid == 2) |
| 135 | env_ptr = tmp_env2; |
| 136 | else |
| 137 | #endif |
Stefan Roese | bbfcbb7 | 2006-09-12 20:19:10 +0200 | [diff] [blame] | 138 | if (gd->env_valid == 1) |
| 139 | env_ptr = tmp_env1; |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 140 | |
| 141 | gd->env_addr = (ulong)env_ptr->data; |
| 142 | |
| 143 | #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 144 | gd->env_addr = (ulong)&default_environment[0]; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 145 | gd->env_valid = 1; |
Guennadi Liakhovetski | fad2444 | 2009-05-18 16:07:22 +0200 | [diff] [blame] | 146 | #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 147 | |
| 148 | return (0); |
| 149 | } |
| 150 | |
| 151 | #ifdef CMD_SAVEENV |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 152 | /* |
| 153 | * The legacy NAND code saved the environment in the first NAND device i.e., |
| 154 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. |
| 155 | */ |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 156 | int writeenv(size_t offset, u_char *buf) |
| 157 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 158 | size_t end = offset + CONFIG_ENV_RANGE; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 159 | size_t amount_saved = 0; |
Guennadi Liakhovetski | 0c40ae94 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 160 | size_t blocksize, len; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 161 | |
| 162 | u_char *char_ptr; |
| 163 | |
| 164 | blocksize = nand_info[0].erasesize; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 165 | len = min(blocksize, CONFIG_ENV_SIZE); |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 166 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 167 | while (amount_saved < CONFIG_ENV_SIZE && offset < end) { |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 168 | if (nand_block_isbad(&nand_info[0], offset)) { |
| 169 | offset += blocksize; |
| 170 | } else { |
| 171 | char_ptr = &buf[amount_saved]; |
Guennadi Liakhovetski | 0c40ae94 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 172 | if (nand_write(&nand_info[0], offset, &len, |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 173 | char_ptr)) |
| 174 | return 1; |
| 175 | offset += blocksize; |
Guennadi Liakhovetski | 0c40ae94 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 176 | amount_saved += len; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 177 | } |
| 178 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 179 | if (amount_saved != CONFIG_ENV_SIZE) |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 180 | return 1; |
| 181 | |
| 182 | return 0; |
| 183 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 184 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 185 | int saveenv(void) |
| 186 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 187 | env_t env_new; |
| 188 | ssize_t len; |
| 189 | char *res; |
| 190 | int ret = 0; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 191 | nand_erase_options_t nand_erase_options; |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 192 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 193 | nand_erase_options.length = CONFIG_ENV_RANGE; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 194 | nand_erase_options.quiet = 0; |
| 195 | nand_erase_options.jffs2 = 0; |
| 196 | nand_erase_options.scrub = 0; |
| 197 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 198 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 199 | return 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 200 | |
| 201 | res = (char *)&env_new.data; |
| 202 | len = hexport('\0', &res, ENV_SIZE); |
| 203 | if (len < 0) { |
| 204 | error("Cannot export environment: errno = %d\n", errno); |
| 205 | return 1; |
| 206 | } |
| 207 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 208 | env_new.flags = ACTIVE_FLAG; |
| 209 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 210 | if(gd->env_valid == 1) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 211 | puts("Erasing redundant NAND...\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 212 | nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 213 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 214 | return 1; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 215 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 216 | puts("Writing to redundant NAND... "); |
| 217 | ret = writeenv(CONFIG_ENV_OFFSET_REDUND, |
| 218 | (u_char *)&env_new); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 219 | } else { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 220 | puts("Erasing NAND...\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 221 | nand_erase_options.offset = CONFIG_ENV_OFFSET; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 222 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 223 | return 1; |
| 224 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 225 | puts("Writing to NAND... "); |
| 226 | ret = writeenv(CONFIG_ENV_OFFSET, |
| 227 | (u_char *)&env_new); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 228 | } |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 229 | if (ret) { |
| 230 | puts("FAILED!\n"); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 231 | return 1; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 232 | } |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 233 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 234 | puts("done\n"); |
| 235 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 236 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 237 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 238 | return ret; |
| 239 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 240 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 241 | int saveenv(void) |
| 242 | { |
Markus Klotzbuecher | ffe7ef5 | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 243 | int ret = 0; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 244 | env_t env_new; |
| 245 | ssize_t len; |
| 246 | char *res; |
Philip Balister | 8c57e9a | 2008-06-16 08:58:07 -0400 | [diff] [blame] | 247 | nand_erase_options_t nand_erase_options; |
Wolfgang Denk | 41df50a | 2008-06-28 23:34:37 +0200 | [diff] [blame] | 248 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 249 | nand_erase_options.length = CONFIG_ENV_RANGE; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 250 | nand_erase_options.quiet = 0; |
| 251 | nand_erase_options.jffs2 = 0; |
| 252 | nand_erase_options.scrub = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 253 | nand_erase_options.offset = CONFIG_ENV_OFFSET; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 254 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 255 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 256 | return 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 257 | |
| 258 | res = (char *)&env_new.data; |
| 259 | len = hexport('\0', &res, ENV_SIZE); |
| 260 | if (len < 0) { |
| 261 | error("Cannot export environment: errno = %d\n", errno); |
| 262 | return 1; |
| 263 | } |
| 264 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 265 | |
| 266 | puts("Erasing Nand...\n"); |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 267 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 268 | return 1; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 269 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 270 | puts("Writing to Nand... "); |
| 271 | if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) { |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 272 | puts("FAILED!\n"); |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 273 | return 1; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 274 | } |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 275 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 276 | puts("done\n"); |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 277 | return ret; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 278 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 279 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 280 | #endif /* CMD_SAVEENV */ |
| 281 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 282 | int readenv(size_t offset, u_char * buf) |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 283 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 284 | size_t end = offset + CONFIG_ENV_RANGE; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 285 | size_t amount_loaded = 0; |
Guennadi Liakhovetski | 0c40ae94 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 286 | size_t blocksize, len; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 287 | |
| 288 | u_char *char_ptr; |
| 289 | |
| 290 | blocksize = nand_info[0].erasesize; |
Mike Frysinger | 3dc4ccd | 2010-08-11 23:42:26 -0400 | [diff] [blame] | 291 | if (!blocksize) |
| 292 | return 1; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 293 | len = min(blocksize, CONFIG_ENV_SIZE); |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 294 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 295 | while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 296 | if (nand_block_isbad(&nand_info[0], offset)) { |
| 297 | offset += blocksize; |
| 298 | } else { |
| 299 | char_ptr = &buf[amount_loaded]; |
Steve Sakoman | c5e84fa | 2010-12-03 13:25:43 +0000 | [diff] [blame^] | 300 | if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr)) |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 301 | return 1; |
| 302 | offset += blocksize; |
Guennadi Liakhovetski | 0c40ae94 | 2008-07-31 12:38:26 +0200 | [diff] [blame] | 303 | amount_loaded += len; |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 304 | } |
| 305 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 306 | if (amount_loaded != CONFIG_ENV_SIZE) |
Stuart Wood | 37e1144 | 2008-05-30 16:05:28 -0400 | [diff] [blame] | 307 | return 1; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 312 | #ifdef CONFIG_ENV_OFFSET_OOB |
| 313 | int get_nand_env_oob(nand_info_t *nand, unsigned long *result) |
| 314 | { |
| 315 | struct mtd_oob_ops ops; |
| 316 | uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)]; |
| 317 | int ret; |
| 318 | |
| 319 | ops.datbuf = NULL; |
| 320 | ops.mode = MTD_OOB_AUTO; |
| 321 | ops.ooboffs = 0; |
| 322 | ops.ooblen = ENV_OFFSET_SIZE; |
| 323 | ops.oobbuf = (void *) oob_buf; |
| 324 | |
| 325 | ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops); |
Scott Wood | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 326 | if (ret) { |
| 327 | printf("error reading OOB block 0\n"); |
| 328 | return ret; |
| 329 | } |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 330 | |
Scott Wood | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 331 | if (oob_buf[0] == ENV_OOB_MARKER) { |
| 332 | *result = oob_buf[1] * nand->erasesize; |
| 333 | } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) { |
| 334 | *result = oob_buf[1]; |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 335 | } else { |
Scott Wood | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 336 | printf("No dynamic environment marker in OOB block 0\n"); |
| 337 | return -ENOENT; |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 338 | } |
Scott Wood | 0268da0 | 2010-07-12 18:17:40 -0500 | [diff] [blame] | 339 | |
| 340 | return 0; |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 341 | } |
| 342 | #endif |
| 343 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 344 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 345 | void env_relocate_spec(void) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 346 | { |
| 347 | #if !defined(ENV_IS_EMBEDDED) |
Markus Klotzbuecher | ffe7ef5 | 2006-03-24 15:43:16 +0100 | [diff] [blame] | 348 | int crc1_ok = 0, crc2_ok = 0; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 349 | env_t *ep, *tmp_env1, *tmp_env2; |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 350 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 351 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
| 352 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 353 | |
Wolfgang Denk | be14eb1 | 2010-01-16 21:50:26 -0700 | [diff] [blame] | 354 | if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) { |
| 355 | puts("Can't allocate buffers for environment\n"); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 356 | free(tmp_env1); |
| 357 | free(tmp_env2); |
| 358 | set_default_env("!malloc() failed"); |
| 359 | return; |
Wolfgang Denk | be14eb1 | 2010-01-16 21:50:26 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 362 | if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 363 | puts("No Valid Environment Area found\n"); |
| 364 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 365 | if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2)) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 366 | puts("No Valid Redundant Environment Area found\n"); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 367 | |
| 368 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 369 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 370 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 371 | if (!crc1_ok && !crc2_ok) { |
derek@siconix.com | 4904385 | 2009-01-26 14:08:17 -0700 | [diff] [blame] | 372 | free(tmp_env1); |
| 373 | free(tmp_env2); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 374 | set_default_env("!bad CRC"); |
| 375 | return; |
| 376 | } else if (crc1_ok && !crc2_ok) { |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 377 | gd->env_valid = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 378 | } else if (!crc1_ok && crc2_ok) { |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 379 | gd->env_valid = 2; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 380 | } else { |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 381 | /* both ok - check serial */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 382 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 383 | gd->env_valid = 2; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 384 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 385 | gd->env_valid = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 386 | else if (tmp_env1->flags > tmp_env2->flags) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 387 | gd->env_valid = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 388 | else if (tmp_env2->flags > tmp_env1->flags) |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 389 | gd->env_valid = 2; |
| 390 | else /* flags are equal - almost impossible */ |
| 391 | gd->env_valid = 1; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 392 | |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | free(env_ptr); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 396 | |
| 397 | if (gd->env_valid == 1) |
| 398 | ep = tmp_env1; |
| 399 | else |
| 400 | ep = tmp_env2; |
| 401 | |
| 402 | env_import((char *)ep, 0); |
| 403 | |
| 404 | free(tmp_env1); |
| 405 | free(tmp_env2); |
Markus Klotzbuecher | 5d113e0 | 2006-03-20 18:02:44 +0100 | [diff] [blame] | 406 | |
| 407 | #endif /* ! ENV_IS_EMBEDDED */ |
| 408 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 409 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 410 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 411 | * The legacy NAND code saved the environment in the first NAND |
| 412 | * device i.e., nand_dev_desc + 0. This is also the behaviour using |
| 413 | * the new NAND code. |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 414 | */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 415 | void env_relocate_spec (void) |
| 416 | { |
| 417 | #if !defined(ENV_IS_EMBEDDED) |
Wolfgang Denk | 7fa6e90 | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 418 | int ret; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 419 | char buf[CONFIG_ENV_SIZE]; |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 420 | |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 421 | #if defined(CONFIG_ENV_OFFSET_OOB) |
| 422 | ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 423 | /* |
| 424 | * If unable to read environment offset from NAND OOB then fall through |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 425 | * to the normal environment reading code below |
| 426 | */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 427 | if (!ret) { |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 428 | printf("Found Environment offset in OOB..\n"); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 429 | } else { |
| 430 | set_default_env("!no env offset in OOB"); |
| 431 | return; |
| 432 | } |
Ben Gardiner | 5fc8b7f | 2010-07-05 13:27:07 -0400 | [diff] [blame] | 433 | #endif |
| 434 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 435 | ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf); |
| 436 | if (ret) { |
| 437 | set_default_env("!readenv() failed"); |
| 438 | return; |
| 439 | } |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 440 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 441 | env_import(buf, 1); |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 442 | #endif /* ! ENV_IS_EMBEDDED */ |
wdenk | 79b5937 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 443 | } |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 444 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |