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