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 | |
Mike Frysinger | 147c716 | 2011-04-23 23:43:21 +0000 | [diff] [blame] | 77 | extern const 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 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 85 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 86 | |
| 87 | int env_init(void) |
| 88 | { |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 89 | int crc1_ok = 0, crc2_ok = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | |
| 91 | uchar flag1 = flash_addr->flags; |
| 92 | uchar flag2 = flash_addr_new->flags; |
| 93 | |
| 94 | ulong addr_default = (ulong)&default_environment[0]; |
| 95 | ulong addr1 = (ulong)&(flash_addr->data); |
| 96 | ulong addr2 = (ulong)&(flash_addr_new->data); |
| 97 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 98 | crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc); |
| 99 | crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc); |
| 100 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 101 | if (crc1_ok && ! crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | gd->env_addr = addr1; |
| 103 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 104 | } else if (! crc1_ok && crc2_ok) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | gd->env_addr = addr2; |
| 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 = addr_default; |
| 109 | gd->env_valid = 0; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 110 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | gd->env_addr = addr1; |
| 112 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 113 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | gd->env_addr = addr2; |
| 115 | gd->env_valid = 1; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 116 | } else if (flag1 == flag2) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | gd->env_addr = addr1; |
| 118 | gd->env_valid = 2; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 119 | } else if (flag1 == 0xFF) { |
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 (flag2 == 0xFF) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 123 | gd->env_addr = addr2; |
| 124 | gd->env_valid = 2; |
| 125 | } |
| 126 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 127 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #ifdef CMD_SAVEENV |
| 131 | int saveenv(void) |
| 132 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 133 | env_t env_new; |
| 134 | ssize_t len; |
| 135 | char *saved_data = NULL; |
| 136 | char *res; |
| 137 | int rc = 1; |
| 138 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 139 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 140 | ulong up_data = 0; |
wdenk | 500545c | 2003-03-06 14:23:06 +0000 | [diff] [blame] | 141 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 142 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 143 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 144 | (ulong)flash_addr, end_addr); |
| 145 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 146 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) { |
| 147 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 150 | debug("Protect off %08lX ... %08lX\n", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 151 | (ulong)flash_addr_new, end_addr_new); |
| 152 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 153 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) { |
| 154 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 157 | res = (char *)&env_new.data; |
Mike Frysinger | 2eb1573 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 158 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 159 | if (len < 0) { |
| 160 | error("Cannot export environment: errno = %d\n", errno); |
| 161 | goto done; |
| 162 | } |
| 163 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 164 | env_new.flags = new_flag; |
| 165 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 166 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 167 | 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] | 168 | debug("Data to save 0x%lX\n", up_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 169 | if (up_data) { |
| 170 | if ((saved_data = malloc(up_data)) == NULL) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | printf("Unable to save the rest of sector (%ld)\n", |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 172 | up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 173 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 174 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | memcpy(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 176 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 177 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
| 178 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
| 179 | up_data, saved_data); |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 180 | } |
| 181 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 182 | puts("Erasing Flash..."); |
| 183 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 184 | (ulong)flash_addr_new, end_addr_new); |
| 185 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 186 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) { |
| 187 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 190 | puts("Writing to Flash... "); |
| 191 | debug(" %08lX ... %08lX ...", |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | (ulong)&(flash_addr_new->data), |
| 193 | sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 194 | if ((rc = flash_write((char *)&env_new, |
| 195 | (ulong)flash_addr_new, |
| 196 | sizeof(env_new))) || |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 197 | (rc = flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | (ulong)&(flash_addr->flags), |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 199 | sizeof(flash_addr->flags))) ) { |
| 200 | flash_perror(rc); |
| 201 | goto done; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 202 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 203 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 204 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 205 | if (up_data) { /* restore the rest of sector */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 206 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
| 207 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 208 | if (flash_write(saved_data, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 209 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 210 | up_data)) { |
| 211 | flash_perror(rc); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 212 | goto done; |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 216 | puts("done\n"); |
| 217 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 218 | { |
| 219 | env_t * etmp = flash_addr; |
| 220 | ulong ltmp = end_addr; |
| 221 | |
| 222 | flash_addr = flash_addr_new; |
| 223 | flash_addr_new = etmp; |
| 224 | |
| 225 | end_addr = end_addr_new; |
| 226 | end_addr_new = ltmp; |
| 227 | } |
| 228 | |
| 229 | rc = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 230 | done: |
wdenk | 47cd00f | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 231 | if (saved_data) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 232 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | /* try to re-protect */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 234 | (void) flash_sect_protect(1, (ulong)flash_addr, end_addr); |
| 235 | (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | |
| 237 | return rc; |
| 238 | } |
| 239 | #endif /* CMD_SAVEENV */ |
| 240 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 241 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 242 | |
| 243 | int env_init(void) |
| 244 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 245 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 246 | gd->env_addr = (ulong)&(env_ptr->data); |
| 247 | gd->env_valid = 1; |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 248 | return(0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | } |
Dirk Behme | 9bb8b20 | 2007-08-20 07:09:05 +0200 | [diff] [blame] | 250 | |
wdenk | 8ed9604 | 2005-01-09 23:16:25 +0000 | [diff] [blame] | 251 | gd->env_addr = (ulong)&default_environment[0]; |
| 252 | gd->env_valid = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 253 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | #ifdef CMD_SAVEENV |
| 257 | |
| 258 | int saveenv(void) |
| 259 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 260 | env_t env_new; |
| 261 | ssize_t len; |
| 262 | int rc = 1; |
| 263 | char *res; |
| 264 | char *saved_data = NULL; |
| 265 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 266 | ulong up_data = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 267 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 268 | up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE)); |
| 269 | debug("Data to save 0x%lx\n", up_data); |
| 270 | if (up_data) { |
| 271 | if ((saved_data = malloc(up_data)) == NULL) { |
| 272 | printf("Unable to save the rest of sector (%ld)\n", |
| 273 | up_data); |
| 274 | goto done; |
| 275 | } |
| 276 | memcpy(saved_data, |
| 277 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); |
| 278 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", |
| 279 | (ulong)flash_addr + CONFIG_ENV_SIZE, |
| 280 | up_data, |
| 281 | (ulong)saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 282 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 283 | #endif /* CONFIG_ENV_SECT_SIZE */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 284 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 285 | debug("Protect off %08lX ... %08lX\n", |
| 286 | (ulong)flash_addr, end_addr); |
| 287 | |
| 288 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
| 289 | goto done; |
| 290 | |
| 291 | res = (char *)&env_new.data; |
Mike Frysinger | 2eb1573 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 292 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 293 | if (len < 0) { |
| 294 | error("Cannot export environment: errno = %d\n", errno); |
| 295 | goto done; |
| 296 | } |
| 297 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 298 | |
| 299 | puts("Erasing Flash..."); |
| 300 | if (flash_sect_erase((long)flash_addr, end_addr)) |
| 301 | goto done; |
| 302 | |
| 303 | puts("Writing to Flash... "); |
| 304 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); |
| 305 | if (rc != 0) { |
| 306 | flash_perror(rc); |
| 307 | goto done; |
| 308 | } |
| 309 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
| 310 | if (up_data) { /* restore the rest of sector */ |
| 311 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", |
| 312 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); |
| 313 | if (flash_write(saved_data, |
| 314 | (long)flash_addr + CONFIG_ENV_SIZE, |
| 315 | up_data)) { |
| 316 | flash_perror(rc); |
| 317 | goto done; |
| 318 | } |
| 319 | } |
| 320 | #endif |
| 321 | puts("done\n"); |
| 322 | rc = 0; |
| 323 | done: |
| 324 | if (saved_data) |
| 325 | free(saved_data); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 326 | /* try to re-protect */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 327 | (void) flash_sect_protect(1, (long)flash_addr, end_addr); |
| 328 | return rc; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | #endif /* CMD_SAVEENV */ |
| 332 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 333 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 334 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 335 | void env_relocate_spec(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 336 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 337 | #ifdef CONFIG_ENV_ADDR_REDUND |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 338 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 339 | env_t *etmp = flash_addr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | c3f9d49 | 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 | c3f9d49 | 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; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 355 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 356 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 357 | (ulong)&(flash_addr_new->flags), |
| 358 | sizeof(flash_addr_new->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 359 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 360 | } |
| 361 | |
wdenk | c3f9d49 | 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; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 367 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 368 | flash_write(&flag, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 369 | (ulong)&(flash_addr->flags), |
| 370 | sizeof(flash_addr->flags)); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 371 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 377 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 378 | |
| 379 | env_import((char *)flash_addr, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 380 | } |