wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 34 | #include <search.h> |
| 35 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 39 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | #define CMD_SAVEENV |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 41 | #elif defined(CONFIG_ENV_ADDR_REDUND) |
Mike Frysinger | bdab39d | 2009-01-28 19:08:14 -0500 | [diff] [blame] | 42 | #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] | 43 | #endif |
| 44 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 45 | #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE) |
| 46 | #error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | char * env_name_spec = "Flash"; |
| 50 | |
| 51 | #ifdef ENV_IS_EMBEDDED |
| 52 | |
| 53 | extern uchar environment[]; |
| 54 | env_t *env_ptr = (env_t *)(&environment[0]); |
| 55 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 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 | |
| 58 | #else /* ! ENV_IS_EMBEDDED */ |
| 59 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 60 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 61 | static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | |
| 63 | #endif /* ENV_IS_EMBEDDED */ |
| 64 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 65 | #if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND) |
| 66 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
| 67 | static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1; |
| 68 | #endif |
| 69 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_ENV_ADDR_REDUND |
| 71 | static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 73 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 74 | static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 75 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | |
| 77 | extern uchar default_environment[]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 78 | |
| 79 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 80 | uchar env_get_char_spec(int index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 82 | return (*((uchar *)(gd->env_addr + index))); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 85 | #undef debug |
| 86 | #define debug printf |
| 87 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 88 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 89 | |
| 90 | int env_init(void) |
| 91 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 92 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 93 | |
| 94 | uchar flag1 = flash_addr->flags; |
| 95 | uchar flag2 = flash_addr_new->flags; |
| 96 | |
| 97 | ulong addr_default = (ulong)&default_environment[0]; |
| 98 | ulong addr1 = (ulong)&(flash_addr->data); |
| 99 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 100 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 101 | crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 102 | crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 103 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 104 | if (crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | gd->env_addr = addr1; |
| 106 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 107 | } else if (! crc1_ok && crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | gd->env_addr = addr2; |
| 109 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 110 | } else if (! crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | gd->env_addr = addr_default; |
| 112 | gd->env_valid = 0; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 113 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | gd->env_addr = addr1; |
| 115 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 116 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | gd->env_addr = addr2; |
| 118 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 119 | } else if (flag1 == flag2) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | gd->env_addr = addr1; |
| 121 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 122 | } else if (flag1 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | gd->env_addr = addr1; |
| 124 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 125 | } else if (flag2 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | gd->env_addr = addr2; |
| 127 | gd->env_valid = 2; |
| 128 | } |
| 129 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 130 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #ifdef CMD_SAVEENV |
| 134 | int saveenv(void) |
| 135 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 136 | env_t env_new; |
| 137 | ssize_t len; |
| 138 | char *saved_data = NULL; |
| 139 | char *res; |
| 140 | int rc = 1; |
| 141 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 142 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 143 | ulong up_data = 0; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 144 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 145 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 146 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 147 | (ulong)flash_addr, end_addr); |
| 148 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 149 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) { |
| 150 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 153 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | (ulong)flash_addr_new, end_addr_new); |
| 155 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 156 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) { |
| 157 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 160 | res = (char *)&env_new.data; |
| 161 | len = hexport('\0', &res, ENV_SIZE); |
| 162 | if (len < 0) { |
| 163 | error("Cannot export environment: errno = %d\n", errno); |
| 164 | goto done; |
| 165 | } |
| 166 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 167 | env_new.flags = new_flag; |
| 168 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 169 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 170 | up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 171 | debug("Data to save 0x%lX\n", up_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 172 | if (up_data) { |
| 173 | if ((saved_data = malloc(up_data)) == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 174 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 175 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 176 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 177 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 178 | memcpy(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 179 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 180 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 181 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 182 | up_data, saved_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 183 | } |
| 184 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 185 | puts("Erasing Flash..."); |
| 186 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 187 | (ulong)flash_addr_new, end_addr_new); |
| 188 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 189 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) { |
| 190 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 193 | puts("Writing to Flash... "); |
| 194 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 195 | (ulong)&(flash_addr_new->data), |
| 196 | sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | if ((rc = flash_write((char *)&env_new, |
| 198 | (ulong)flash_addr_new, |
| 199 | sizeof(env_new))) || |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 200 | (rc = flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 201 | (ulong)&(flash_addr->flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 202 | sizeof(flash_addr->flags))) ) { |
| 203 | flash_perror(rc); |
| 204 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 205 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 207 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 208 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 209 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 210 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 211 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 212 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 213 | up_data)) { |
| 214 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 215 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 219 | puts("done\n"); |
| 220 | |
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; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 233 | done: |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 234 | if (saved_data) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 235 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | /* try to re-protect */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 237 | (void) flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 238 | (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 256 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | #ifdef CMD_SAVEENV |
| 260 | |
| 261 | int saveenv(void) |
| 262 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 263 | env_t env_new; |
| 264 | ssize_t len; |
| 265 | int rc = 1; |
| 266 | char *res; |
| 267 | char *saved_data = NULL; |
| 268 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 269 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 270 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 271 | up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE)); |
| 272 | debug("Data to save 0x%lx\n", up_data); |
| 273 | if (up_data) { |
| 274 | if ((saved_data = malloc(up_data)) == NULL) { |
| 275 | printf("Unable to save the rest of sector (%ld)\n", |
| 276 | up_data); |
| 277 | goto done; |
| 278 | } |
| 279 | memcpy(saved_data, |
| 280 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 281 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 282 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 283 | up_data, |
| 284 | (ulong)saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 285 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 286 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 287 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 288 | debug("Protect off %08lX ... %08lX\n", |
| 289 | (ulong)flash_addr, end_addr); |
| 290 | |
| 291 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 292 | goto done; |
| 293 | |
| 294 | res = (char *)&env_new.data; |
| 295 | len = hexport('\0', &res, ENV_SIZE); |
| 296 | if (len < 0) { |
| 297 | error("Cannot export environment: errno = %d\n", errno); |
| 298 | goto done; |
| 299 | } |
| 300 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 301 | |
| 302 | puts("Erasing Flash..."); |
| 303 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 304 | goto done; |
| 305 | |
| 306 | puts("Writing to Flash... "); |
| 307 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
| 308 | if (rc != 0) { |
| 309 | flash_perror(rc); |
| 310 | goto done; |
| 311 | } |
| 312 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 313 | if (up_data) { /* restore the rest of sector */ |
| 314 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 315 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 316 | if (flash_write(saved_data, |
| 317 | (long)flash_addr + CONFIG_ENV_SIZE, |
| 318 | up_data)) { |
| 319 | flash_perror(rc); |
| 320 | goto done; |
| 321 | } |
| 322 | } |
| 323 | #endif |
| 324 | puts("done\n"); |
| 325 | rc = 0; |
| 326 | done: |
| 327 | if (saved_data) |
| 328 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 329 | /* try to re-protect */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 330 | (void) flash_sect_protect(1, (long)flash_addr, end_addr); |
| 331 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | #endif /* CMD_SAVEENV */ |
| 335 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 336 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 337 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 338 | void env_relocate_spec(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 339 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 340 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 341 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 342 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 343 | ulong ltmp = end_addr; |
| 344 | |
| 345 | flash_addr = flash_addr_new; |
| 346 | flash_addr_new = etmp; |
| 347 | |
| 348 | end_addr = end_addr_new; |
| 349 | end_addr_new = ltmp; |
| 350 | } |
| 351 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 352 | if (flash_addr_new->flags != OBSOLETE_FLAG && |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 353 | crc32(0, flash_addr_new->data, ENV_SIZE) == |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 354 | flash_addr_new->crc) { |
| 355 | char flag = OBSOLETE_FLAG; |
| 356 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 357 | gd->env_valid = 2; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 358 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 359 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 360 | (ulong)&(flash_addr_new->flags), |
| 361 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 362 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 363 | } |
| 364 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 365 | if (flash_addr->flags != ACTIVE_FLAG && |
| 366 | (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) { |
| 367 | char flag = ACTIVE_FLAG; |
| 368 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 369 | gd->env_valid = 2; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 370 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 371 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 372 | (ulong)&(flash_addr->flags), |
| 373 | sizeof(flash_addr->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 374 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | if (gd->env_valid == 2) |
| 378 | puts ("*** Warning - some problems detected " |
| 379 | "reading environment; recovered successfully\n\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 380 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 381 | |
| 382 | env_import((char *)flash_addr, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 383 | } |