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