wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | /* #define DEBUG */ |
| 28 | |
| 29 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | #include <command.h> |
| 31 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | #include <linux/stddef.h> |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 33 | #include <malloc.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 34 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
Jon Loeliger | 052fc84 | 2007-07-08 18:10:08 -0500 | [diff] [blame] | 37 | #if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | #define CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 39 | #elif defined(CONFIG_ENV_ADDR_REDUND) |
| 40 | #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_FLASH |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 43 | #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE) |
| 44 | #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | #ifdef CONFIG_INFERNO |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 48 | # ifdef CONFIG_ENV_ADDR_REDUND |
| 49 | #error CONFIG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | # endif |
| 51 | #endif |
| 52 | |
| 53 | char * env_name_spec = "Flash"; |
| 54 | |
| 55 | #ifdef ENV_IS_EMBEDDED |
| 56 | |
| 57 | extern uchar environment[]; |
| 58 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 59 | |
| 60 | #ifdef CMD_SAVEENV |
| 61 | /* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/ |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 62 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
| 65 | #else /* ! ENV_IS_EMBEDDED */ |
| 66 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 67 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | #ifdef CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 69 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | #endif /* ENV_IS_EMBEDDED */ |
| 73 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 75 | static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 77 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
| 78 | static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1; |
| 79 | static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 81 | #define ACTIVE_FLAG 1 |
| 82 | #define OBSOLETE_FLAG 0 |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 83 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 84 | |
| 85 | extern uchar default_environment[]; |
| 86 | extern int default_environment_size; |
| 87 | |
| 88 | |
| 89 | uchar env_get_char_spec (int index) |
| 90 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 92 | } |
| 93 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 94 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 95 | |
| 96 | int env_init(void) |
| 97 | { |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 98 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | |
| 100 | uchar flag1 = flash_addr->flags; |
| 101 | uchar flag2 = flash_addr_new->flags; |
| 102 | |
| 103 | ulong addr_default = (ulong)&default_environment[0]; |
| 104 | ulong addr1 = (ulong)&(flash_addr->data); |
| 105 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 106 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 107 | crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 108 | crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 109 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 110 | if (crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | gd->env_addr = addr1; |
| 112 | gd->env_valid = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 113 | } else if (! crc1_ok && crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | gd->env_addr = addr2; |
| 115 | gd->env_valid = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 116 | } else if (! crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | gd->env_addr = addr_default; |
| 118 | gd->env_valid = 0; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 119 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | gd->env_addr = addr1; |
| 121 | gd->env_valid = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 122 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | gd->env_addr = addr2; |
| 124 | gd->env_valid = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 125 | } else if (flag1 == flag2) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | gd->env_addr = addr1; |
| 127 | gd->env_valid = 2; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 128 | } else if (flag1 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | gd->env_addr = addr1; |
| 130 | gd->env_valid = 2; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 131 | } else if (flag2 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | gd->env_addr = addr2; |
| 133 | gd->env_valid = 2; |
| 134 | } |
| 135 | |
| 136 | return (0); |
| 137 | } |
| 138 | |
| 139 | #ifdef CMD_SAVEENV |
| 140 | int saveenv(void) |
| 141 | { |
wdenk | d730fbc | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 142 | char *saved_data = NULL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 143 | int rc = 1; |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 144 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 145 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 146 | ulong up_data = 0; |
wdenk | d730fbc | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 147 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | |
| 149 | debug ("Protect off %08lX ... %08lX\n", |
| 150 | (ulong)flash_addr, end_addr); |
| 151 | |
| 152 | if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) { |
| 153 | goto Done; |
| 154 | } |
| 155 | |
| 156 | debug ("Protect off %08lX ... %08lX\n", |
| 157 | (ulong)flash_addr_new, end_addr_new); |
| 158 | |
| 159 | if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) { |
| 160 | goto Done; |
| 161 | } |
| 162 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 163 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 164 | up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE)); |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 165 | debug ("Data to save 0x%x\n", up_data); |
| 166 | if (up_data) { |
| 167 | if ((saved_data = malloc(up_data)) == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 168 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 169 | up_data); |
| 170 | goto Done; |
| 171 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 172 | memcpy(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 173 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 174 | debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n", |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 175 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 176 | up_data, saved_data); |
| 177 | } |
| 178 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 179 | puts ("Erasing Flash..."); |
| 180 | debug (" %08lX ... %08lX ...", |
| 181 | (ulong)flash_addr_new, end_addr_new); |
| 182 | |
| 183 | if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) { |
| 184 | goto Done; |
| 185 | } |
| 186 | |
| 187 | puts ("Writing to Flash... "); |
| 188 | debug (" %08lX ... %08lX ...", |
| 189 | (ulong)&(flash_addr_new->data), |
| 190 | sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data)); |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 191 | if ((rc = flash_write((char *)env_ptr->data, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 192 | (ulong)&(flash_addr_new->data), |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 193 | sizeof(env_ptr->data))) || |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 194 | (rc = flash_write((char *)&(env_ptr->crc), |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 195 | (ulong)&(flash_addr_new->crc), |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 196 | sizeof(env_ptr->crc))) || |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 197 | (rc = flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | (ulong)&(flash_addr->flags), |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 199 | sizeof(flash_addr->flags))) || |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 200 | (rc = flash_write(&new_flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 201 | (ulong)&(flash_addr_new->flags), |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 202 | sizeof(flash_addr_new->flags)))) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | { |
| 204 | flash_perror (rc); |
| 205 | goto Done; |
| 206 | } |
| 207 | puts ("done\n"); |
| 208 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 209 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 210 | if (up_data) { /* restore the rest of sector */ |
| 211 | debug ("Restoring the rest of data to 0x%x len 0x%x\n", |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 212 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 213 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 214 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 215 | up_data)) { |
| 216 | flash_perror(rc); |
| 217 | goto Done; |
| 218 | } |
| 219 | } |
| 220 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 221 | { |
| 222 | env_t * etmp = flash_addr; |
| 223 | ulong ltmp = end_addr; |
| 224 | |
| 225 | flash_addr = flash_addr_new; |
| 226 | flash_addr_new = etmp; |
| 227 | |
| 228 | end_addr = end_addr_new; |
| 229 | end_addr_new = ltmp; |
| 230 | } |
| 231 | |
| 232 | rc = 0; |
| 233 | Done: |
| 234 | |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 235 | if (saved_data) |
| 236 | free (saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 237 | /* try to re-protect */ |
| 238 | (void) flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 239 | (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 240 | |
| 241 | return rc; |
| 242 | } |
| 243 | #endif /* CMD_SAVEENV */ |
| 244 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 245 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 246 | |
| 247 | int env_init(void) |
| 248 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 250 | gd->env_addr = (ulong)&(env_ptr->data); |
| 251 | gd->env_valid = 1; |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 252 | return(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | } |
Dirk Behme | afa1dd5 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 254 | |
wdenk | f806271 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 255 | gd->env_addr = (ulong)&default_environment[0]; |
| 256 | gd->env_valid = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 257 | return (0); |
| 258 | } |
| 259 | |
| 260 | #ifdef CMD_SAVEENV |
| 261 | |
| 262 | int saveenv(void) |
| 263 | { |
| 264 | int len, rc; |
| 265 | ulong end_addr; |
| 266 | ulong flash_sect_addr; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 267 | #if defined(CONFIG_ENV_SECT_SIZE) && (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | ulong flash_offset; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 269 | uchar env_buffer[CONFIG_ENV_SECT_SIZE]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 270 | #else |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 271 | uchar *env_buffer = (uchar *)env_ptr; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 272 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 273 | int rcode = 0; |
| 274 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 275 | #if defined(CONFIG_ENV_SECT_SIZE) && (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 277 | flash_offset = ((ulong)flash_addr) & (CONFIG_ENV_SECT_SIZE-1); |
| 278 | flash_sect_addr = ((ulong)flash_addr) & ~(CONFIG_ENV_SECT_SIZE-1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 279 | |
| 280 | debug ( "copy old content: " |
| 281 | "sect_addr: %08lX env_addr: %08lX offset: %08lX\n", |
| 282 | flash_sect_addr, (ulong)flash_addr, flash_offset); |
| 283 | |
| 284 | /* copy old contents to temporary buffer */ |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 285 | memcpy (env_buffer, (void *)flash_sect_addr, CONFIG_ENV_SECT_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 286 | |
| 287 | /* copy current environment to temporary buffer */ |
| 288 | memcpy ((uchar *)((unsigned long)env_buffer + flash_offset), |
| 289 | env_ptr, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 290 | CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 291 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 292 | len = CONFIG_ENV_SECT_SIZE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 293 | #else |
| 294 | flash_sect_addr = (ulong)flash_addr; |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 295 | len = CONFIG_ENV_SIZE; |
| 296 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 297 | |
| 298 | #ifndef CONFIG_INFERNO |
| 299 | end_addr = flash_sect_addr + len - 1; |
| 300 | #else |
| 301 | /* this is the last sector, and the size is hardcoded here */ |
| 302 | /* otherwise we will get stack problems on loading 128 KB environment */ |
| 303 | end_addr = flash_sect_addr + 0x20000 - 1; |
| 304 | #endif |
| 305 | |
| 306 | debug ("Protect off %08lX ... %08lX\n", |
| 307 | (ulong)flash_sect_addr, end_addr); |
| 308 | |
| 309 | if (flash_sect_protect (0, flash_sect_addr, end_addr)) |
| 310 | return 1; |
| 311 | |
| 312 | puts ("Erasing Flash..."); |
| 313 | if (flash_sect_erase (flash_sect_addr, end_addr)) |
| 314 | return 1; |
| 315 | |
| 316 | puts ("Writing to Flash... "); |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 317 | rc = flash_write((char *)env_buffer, flash_sect_addr, len); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 318 | if (rc != 0) { |
| 319 | flash_perror (rc); |
| 320 | rcode = 1; |
| 321 | } else { |
| 322 | puts ("done\n"); |
| 323 | } |
| 324 | |
| 325 | /* try to re-protect */ |
| 326 | (void) flash_sect_protect (1, flash_sect_addr, end_addr); |
| 327 | return rcode; |
| 328 | } |
| 329 | |
| 330 | #endif /* CMD_SAVEENV */ |
| 331 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 332 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 333 | |
| 334 | void env_relocate_spec (void) |
| 335 | { |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 336 | #if !defined(ENV_IS_EMBEDDED) || defined(CONFIG_ENV_ADDR_REDUND) |
| 337 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 338 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 339 | env_t * etmp = flash_addr; |
| 340 | ulong ltmp = end_addr; |
| 341 | |
| 342 | flash_addr = flash_addr_new; |
| 343 | flash_addr_new = etmp; |
| 344 | |
| 345 | end_addr = end_addr_new; |
| 346 | end_addr_new = ltmp; |
| 347 | } |
| 348 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 349 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 350 | crc32(0, flash_addr_new->data, ENV_SIZE) == |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 351 | flash_addr_new->crc) { |
| 352 | char flag = OBSOLETE_FLAG; |
| 353 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 354 | gd->env_valid = 2; |
| 355 | flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new); |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 356 | flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 357 | (ulong)&(flash_addr_new->flags), |
| 358 | sizeof(flash_addr_new->flags)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 359 | flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 360 | } |
| 361 | |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 362 | if (flash_addr->flags != ACTIVE_FLAG && |
| 363 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 364 | char flag = ACTIVE_FLAG; |
| 365 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 366 | gd->env_valid = 2; |
| 367 | flash_sect_protect (0, (ulong)flash_addr, end_addr); |
wdenk | acd9b10 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 368 | flash_write(&flag, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 369 | (ulong)&(flash_addr->flags), |
| 370 | sizeof(flash_addr->flags)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 371 | flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 372 | } |
| 373 | |
| 374 | if (gd->env_valid == 2) |
| 375 | puts ("*** Warning - some problems detected " |
| 376 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 377 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Mike Frysinger | 45cdb5f | 2008-05-02 18:17:50 -0400 | [diff] [blame] | 378 | #ifdef CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 379 | memcpy (env_ptr, (void*)flash_addr, CONFIG_ENV_SIZE); |
Mike Frysinger | 45cdb5f | 2008-05-02 18:17:50 -0400 | [diff] [blame] | 380 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 381 | #endif /* ! ENV_IS_EMBEDDED || CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 382 | } |