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