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