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 | 47cd00f | 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 | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 37 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | #define CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 39 | #elif defined(CONFIG_ENV_ADDR_REDUND) |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 40 | #error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 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 | 0e8d158 | 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 | 0e8d158 | 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 | 0e8d158 | 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 | 0e8d158 | 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 | 0e8d158 | 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 | 0e8d158 | 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 | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 81 | #define ACTIVE_FLAG 1 |
| 82 | #define OBSOLETE_FLAG 0 |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 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[]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | uchar env_get_char_spec (int index) |
| 89 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 91 | } |
| 92 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 93 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 94 | |
| 95 | int env_init(void) |
| 96 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 97 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | |
| 99 | uchar flag1 = flash_addr->flags; |
| 100 | uchar flag2 = flash_addr_new->flags; |
| 101 | |
| 102 | ulong addr_default = (ulong)&default_environment[0]; |
| 103 | ulong addr1 = (ulong)&(flash_addr->data); |
| 104 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 105 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 106 | crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 107 | crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 108 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 109 | if (crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | gd->env_addr = addr1; |
| 111 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 112 | } else if (! crc1_ok && crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | gd->env_addr = addr2; |
| 114 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 115 | } else if (! crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | gd->env_addr = addr_default; |
| 117 | gd->env_valid = 0; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 118 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 119 | gd->env_addr = addr1; |
| 120 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 121 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 122 | gd->env_addr = addr2; |
| 123 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 124 | } else if (flag1 == flag2) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 125 | gd->env_addr = addr1; |
| 126 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 127 | } else if (flag1 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | gd->env_addr = addr1; |
| 129 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 130 | } else if (flag2 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | gd->env_addr = addr2; |
| 132 | gd->env_valid = 2; |
| 133 | } |
| 134 | |
| 135 | return (0); |
| 136 | } |
| 137 | |
| 138 | #ifdef CMD_SAVEENV |
| 139 | int saveenv(void) |
| 140 | { |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 141 | char *saved_data = NULL; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 142 | int rc = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 143 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 144 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 145 | ulong up_data = 0; |
| 146 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | |
| 148 | debug ("Protect off %08lX ... %08lX\n", |
| 149 | (ulong)flash_addr, end_addr); |
| 150 | |
| 151 | if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) { |
| 152 | goto Done; |
| 153 | } |
| 154 | |
| 155 | debug ("Protect off %08lX ... %08lX\n", |
| 156 | (ulong)flash_addr_new, end_addr_new); |
| 157 | |
| 158 | if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) { |
| 159 | goto Done; |
| 160 | } |
| 161 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 162 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 163 | up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE)); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 164 | debug ("Data to save 0x%x\n", up_data); |
| 165 | if (up_data) { |
| 166 | if ((saved_data = malloc(up_data)) == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 167 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 168 | up_data); |
| 169 | goto Done; |
| 170 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | memcpy(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 172 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 173 | debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n", |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 174 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 175 | up_data, saved_data); |
| 176 | } |
| 177 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 178 | puts ("Erasing Flash..."); |
| 179 | debug (" %08lX ... %08lX ...", |
| 180 | (ulong)flash_addr_new, end_addr_new); |
| 181 | |
| 182 | if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) { |
| 183 | goto Done; |
| 184 | } |
| 185 | |
| 186 | puts ("Writing to Flash... "); |
| 187 | debug (" %08lX ... %08lX ...", |
| 188 | (ulong)&(flash_addr_new->data), |
| 189 | sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data)); |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 190 | if ((rc = flash_write((char *)env_ptr->data, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 191 | (ulong)&(flash_addr_new->data), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 192 | sizeof(env_ptr->data))) || |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 193 | (rc = flash_write((char *)&(env_ptr->crc), |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 194 | (ulong)&(flash_addr_new->crc), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 195 | sizeof(env_ptr->crc))) || |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 196 | (rc = flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 197 | (ulong)&(flash_addr->flags), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 198 | sizeof(flash_addr->flags))) || |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 199 | (rc = flash_write(&new_flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 200 | (ulong)&(flash_addr_new->flags), |
wdenk | c40b295 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 201 | sizeof(flash_addr_new->flags)))) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 202 | { |
| 203 | flash_perror (rc); |
| 204 | goto Done; |
| 205 | } |
| 206 | puts ("done\n"); |
| 207 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 208 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 209 | if (up_data) { /* restore the rest of sector */ |
| 210 | debug ("Restoring the rest of data to 0x%x len 0x%x\n", |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 211 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 212 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 213 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 214 | up_data)) { |
| 215 | flash_perror(rc); |
| 216 | goto Done; |
| 217 | } |
| 218 | } |
| 219 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | { |
| 221 | env_t * etmp = flash_addr; |
| 222 | ulong ltmp = end_addr; |
| 223 | |
| 224 | flash_addr = flash_addr_new; |
| 225 | flash_addr_new = etmp; |
| 226 | |
| 227 | end_addr = end_addr_new; |
| 228 | end_addr_new = ltmp; |
| 229 | } |
| 230 | |
| 231 | rc = 0; |
| 232 | Done: |
| 233 | |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 234 | if (saved_data) |
| 235 | free (saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | /* try to re-protect */ |
| 237 | (void) flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 238 | (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 239 | |
| 240 | return rc; |
| 241 | } |
| 242 | #endif /* CMD_SAVEENV */ |
| 243 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 244 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 245 | |
| 246 | int env_init(void) |
| 247 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 248 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 249 | gd->env_addr = (ulong)&(env_ptr->data); |
| 250 | gd->env_valid = 1; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 251 | return(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 252 | } |
Dirk Behme | 9bb8b20 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 253 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 254 | gd->env_addr = (ulong)&default_environment[0]; |
| 255 | gd->env_valid = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 256 | return (0); |
| 257 | } |
| 258 | |
| 259 | #ifdef CMD_SAVEENV |
| 260 | |
| 261 | int saveenv(void) |
| 262 | { |
| 263 | int len, rc; |
| 264 | ulong end_addr; |
| 265 | ulong flash_sect_addr; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 266 | #if defined(CONFIG_ENV_SECT_SIZE) && (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | ulong flash_offset; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 268 | uchar env_buffer[CONFIG_ENV_SECT_SIZE]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 269 | #else |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 270 | uchar *env_buffer = (uchar *)env_ptr; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 271 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | int rcode = 0; |
| 273 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 274 | #if defined(CONFIG_ENV_SECT_SIZE) && (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 275 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 276 | flash_offset = ((ulong)flash_addr) & (CONFIG_ENV_SECT_SIZE-1); |
| 277 | flash_sect_addr = ((ulong)flash_addr) & ~(CONFIG_ENV_SECT_SIZE-1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 278 | |
| 279 | debug ( "copy old content: " |
| 280 | "sect_addr: %08lX env_addr: %08lX offset: %08lX\n", |
| 281 | flash_sect_addr, (ulong)flash_addr, flash_offset); |
| 282 | |
| 283 | /* copy old contents to temporary buffer */ |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 284 | memcpy (env_buffer, (void *)flash_sect_addr, CONFIG_ENV_SECT_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 285 | |
| 286 | /* copy current environment to temporary buffer */ |
| 287 | memcpy ((uchar *)((unsigned long)env_buffer + flash_offset), |
| 288 | env_ptr, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 289 | CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 290 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 291 | len = CONFIG_ENV_SECT_SIZE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 292 | #else |
| 293 | flash_sect_addr = (ulong)flash_addr; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 294 | len = CONFIG_ENV_SIZE; |
| 295 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 296 | |
| 297 | #ifndef CONFIG_INFERNO |
| 298 | end_addr = flash_sect_addr + len - 1; |
| 299 | #else |
| 300 | /* this is the last sector, and the size is hardcoded here */ |
| 301 | /* otherwise we will get stack problems on loading 128 KB environment */ |
| 302 | end_addr = flash_sect_addr + 0x20000 - 1; |
| 303 | #endif |
| 304 | |
| 305 | debug ("Protect off %08lX ... %08lX\n", |
| 306 | (ulong)flash_sect_addr, end_addr); |
| 307 | |
| 308 | if (flash_sect_protect (0, flash_sect_addr, end_addr)) |
| 309 | return 1; |
| 310 | |
| 311 | puts ("Erasing Flash..."); |
| 312 | if (flash_sect_erase (flash_sect_addr, end_addr)) |
| 313 | return 1; |
| 314 | |
| 315 | puts ("Writing to Flash... "); |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 316 | rc = flash_write((char *)env_buffer, flash_sect_addr, len); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 317 | if (rc != 0) { |
| 318 | flash_perror (rc); |
| 319 | rcode = 1; |
| 320 | } else { |
| 321 | puts ("done\n"); |
| 322 | } |
| 323 | |
| 324 | /* try to re-protect */ |
| 325 | (void) flash_sect_protect (1, flash_sect_addr, end_addr); |
| 326 | return rcode; |
| 327 | } |
| 328 | |
| 329 | #endif /* CMD_SAVEENV */ |
| 330 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 331 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 332 | |
| 333 | void env_relocate_spec (void) |
| 334 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 335 | #if !defined(ENV_IS_EMBEDDED) || defined(CONFIG_ENV_ADDR_REDUND) |
| 336 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 337 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 338 | env_t * etmp = flash_addr; |
| 339 | ulong ltmp = end_addr; |
| 340 | |
| 341 | flash_addr = flash_addr_new; |
| 342 | flash_addr_new = etmp; |
| 343 | |
| 344 | end_addr = end_addr_new; |
| 345 | end_addr_new = ltmp; |
| 346 | } |
| 347 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 348 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 349 | crc32(0, flash_addr_new->data, ENV_SIZE) == |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 350 | flash_addr_new->crc) { |
| 351 | char flag = OBSOLETE_FLAG; |
| 352 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 353 | gd->env_valid = 2; |
| 354 | flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 355 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 356 | (ulong)&(flash_addr_new->flags), |
| 357 | sizeof(flash_addr_new->flags)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 358 | flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new); |
| 359 | } |
| 360 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 361 | if (flash_addr->flags != ACTIVE_FLAG && |
| 362 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 363 | char flag = ACTIVE_FLAG; |
| 364 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 365 | gd->env_valid = 2; |
| 366 | flash_sect_protect (0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 367 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 368 | (ulong)&(flash_addr->flags), |
| 369 | sizeof(flash_addr->flags)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 370 | flash_sect_protect (1, (ulong)flash_addr, end_addr); |
| 371 | } |
| 372 | |
| 373 | if (gd->env_valid == 2) |
| 374 | puts ("*** Warning - some problems detected " |
| 375 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 376 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Mike Frysinger | 4acc2a1 | 2008-05-02 18:17:50 -0400 | [diff] [blame] | 377 | #ifdef CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 378 | memcpy (env_ptr, (void*)flash_addr, CONFIG_ENV_SIZE); |
Mike Frysinger | 4acc2a1 | 2008-05-02 18:17:50 -0400 | [diff] [blame] | 379 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 380 | #endif /* ! ENV_IS_EMBEDDED || CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 381 | } |