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