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