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