Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [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 | * (C) Copyright 2008 Atmel Corporation |
| 9 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | #include <common.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 29 | #include <environment.h> |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 30 | #include <malloc.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 31 | #include <spi_flash.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | #include <search.h> |
| 33 | #include <errno.h> |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 34 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 35 | #ifndef CONFIG_ENV_SPI_BUS |
| 36 | # define CONFIG_ENV_SPI_BUS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 37 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 38 | #ifndef CONFIG_ENV_SPI_CS |
| 39 | # define CONFIG_ENV_SPI_CS 0 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 40 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 41 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
| 42 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 43 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 44 | #ifndef CONFIG_ENV_SPI_MODE |
| 45 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 49 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 50 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 51 | |
| 52 | #define ACTIVE_FLAG 1 |
| 53 | #define OBSOLETE_FLAG 0 |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 54 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 55 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 56 | DECLARE_GLOBAL_DATA_PTR; |
| 57 | |
| 58 | /* references to names in env_common.c */ |
| 59 | extern uchar default_environment[]; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 60 | |
| 61 | char * env_name_spec = "SPI Flash"; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 62 | |
| 63 | static struct spi_flash *env_flash; |
| 64 | |
| 65 | uchar env_get_char_spec(int index) |
| 66 | { |
| 67 | return *((uchar *)(gd->env_addr + index)); |
| 68 | } |
| 69 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 70 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 71 | |
| 72 | int saveenv(void) |
| 73 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 74 | env_t env_new; |
| 75 | ssize_t len; |
| 76 | char *res; |
| 77 | u32 saved_size, saved_offset; |
| 78 | char *saved_buffer = NULL; |
| 79 | u32 sector = 1; |
| 80 | int ret; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 81 | char flag = OBSOLETE_FLAG; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 82 | |
| 83 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 84 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 85 | CONFIG_ENV_SPI_CS, |
| 86 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 87 | if (!env_flash) { |
| 88 | set_default_env("!spi_flash_probe() failed"); |
| 89 | return 1; |
| 90 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 93 | res = (char *)&env_new.data; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 94 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 95 | if (len < 0) { |
| 96 | error("Cannot export environment: errno = %d\n", errno); |
| 97 | return 1; |
| 98 | } |
| 99 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 100 | env_new.flags = ACTIVE_FLAG; |
| 101 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 102 | if (gd->env_valid == 1) { |
| 103 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 104 | env_offset = CONFIG_ENV_OFFSET; |
| 105 | } else { |
| 106 | env_new_offset = CONFIG_ENV_OFFSET; |
| 107 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 108 | } |
| 109 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 110 | /* Is the sector larger than the env (i.e. embedded) */ |
| 111 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 112 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 113 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
| 114 | saved_buffer = malloc(saved_size); |
| 115 | if (!saved_buffer) { |
| 116 | ret = 1; |
| 117 | goto done; |
| 118 | } |
| 119 | ret = spi_flash_read(env_flash, saved_offset, |
| 120 | saved_size, saved_buffer); |
| 121 | if (ret) |
| 122 | goto done; |
| 123 | } |
| 124 | |
| 125 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 126 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 127 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
| 128 | sector++; |
| 129 | } |
| 130 | |
| 131 | puts("Erasing SPI flash..."); |
| 132 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 133 | sector * CONFIG_ENV_SECT_SIZE); |
| 134 | if (ret) |
| 135 | goto done; |
| 136 | |
| 137 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 138 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 139 | ret = spi_flash_write(env_flash, env_new_offset, |
| 140 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 141 | if (ret) |
| 142 | goto done; |
| 143 | |
| 144 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 145 | ret = spi_flash_write(env_flash, saved_offset, |
| 146 | saved_size, saved_buffer); |
| 147 | if (ret) |
| 148 | goto done; |
| 149 | } |
| 150 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 151 | ret = spi_flash_write(env_flash, |
| 152 | env_offset + offsetof(env_t, flags), |
| 153 | sizeof(env_new.flags), &flag); |
| 154 | if (ret) |
| 155 | goto done; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 156 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 157 | puts("done\n"); |
| 158 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 159 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
| 160 | |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 161 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 162 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 163 | done: |
| 164 | if (saved_buffer) |
| 165 | free(saved_buffer); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | void env_relocate_spec(void) |
| 170 | { |
| 171 | int ret; |
| 172 | int crc1_ok = 0, crc2_ok = 0; |
| 173 | env_t *tmp_env1 = NULL; |
| 174 | env_t *tmp_env2 = NULL; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 175 | env_t *ep = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 176 | |
| 177 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 178 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 179 | |
| 180 | if (!tmp_env1 || !tmp_env2) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 181 | set_default_env("!malloc() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 182 | goto out; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 186 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 187 | if (!env_flash) { |
| 188 | set_default_env("!spi_flash_probe() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 189 | goto out; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 190 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 191 | |
| 192 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 193 | CONFIG_ENV_SIZE, tmp_env1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 194 | if (ret) { |
| 195 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 196 | goto err_read; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 198 | |
| 199 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) |
| 200 | crc1_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 201 | |
| 202 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 203 | CONFIG_ENV_SIZE, tmp_env2); |
| 204 | if (!ret) { |
| 205 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) |
| 206 | crc2_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 209 | if (!crc1_ok && !crc2_ok) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 210 | set_default_env("!bad CRC"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 211 | goto err_read; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 212 | } else if (crc1_ok && !crc2_ok) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 213 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 214 | } else if (!crc1_ok && crc2_ok) { |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 215 | gd->env_valid = 2; |
| 216 | } else if (tmp_env1->flags == ACTIVE_FLAG && |
| 217 | tmp_env2->flags == OBSOLETE_FLAG) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 218 | gd->env_valid = 1; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 219 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
| 220 | tmp_env2->flags == ACTIVE_FLAG) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 221 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 222 | } else if (tmp_env1->flags == tmp_env2->flags) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 223 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 224 | } else if (tmp_env1->flags == 0xFF) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 225 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 226 | } else { |
| 227 | /* |
| 228 | * this differs from code in env_flash.c, but I think a sane |
| 229 | * default path is desirable. |
| 230 | */ |
| 231 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 232 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 233 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 234 | if (gd->env_valid == 1) |
| 235 | ep = tmp_env1; |
| 236 | else |
| 237 | ep = tmp_env2; |
| 238 | |
| 239 | ret = env_import((char *)ep, 0); |
| 240 | if (!ret) { |
| 241 | error("Cannot import environment: errno = %d\n", errno); |
| 242 | set_default_env("env_import failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 243 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 244 | |
| 245 | err_read: |
| 246 | spi_flash_free(env_flash); |
| 247 | env_flash = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 248 | out: |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 249 | free(tmp_env1); |
| 250 | free(tmp_env2); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 251 | |
| 252 | return; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 253 | } |
| 254 | #else |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 255 | int saveenv(void) |
| 256 | { |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 257 | u32 saved_size, saved_offset; |
| 258 | char *saved_buffer = NULL; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 259 | u32 sector = 1; |
Stefano Babic | 5e26a1f | 2010-10-29 11:49:45 +0200 | [diff] [blame] | 260 | int ret = 1; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 261 | env_t env_new; |
| 262 | char *res; |
| 263 | ssize_t len; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 264 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 265 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 266 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 267 | CONFIG_ENV_SPI_CS, |
| 268 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 269 | if (!env_flash) { |
| 270 | set_default_env("!spi_flash_probe() failed"); |
| 271 | return 1; |
| 272 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 275 | /* Is the sector larger than the env (i.e. embedded) */ |
| 276 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 277 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 278 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 279 | saved_buffer = malloc(saved_size); |
| 280 | if (!saved_buffer) { |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 281 | goto done; |
| 282 | } |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 283 | ret = spi_flash_read(env_flash, saved_offset, |
| 284 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 285 | if (ret) |
| 286 | goto done; |
| 287 | } |
| 288 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 289 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 290 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 291 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 292 | sector++; |
| 293 | } |
| 294 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 295 | res = (char *)&env_new.data; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 296 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 297 | if (len < 0) { |
| 298 | error("Cannot export environment: errno = %d\n", errno); |
| 299 | goto done; |
| 300 | } |
| 301 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 302 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 303 | puts("Erasing SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 304 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 305 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 306 | if (ret) |
| 307 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 308 | |
| 309 | puts("Writing to SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 310 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
| 311 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 312 | if (ret) |
| 313 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 314 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 315 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 316 | ret = spi_flash_write(env_flash, saved_offset, |
| 317 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 318 | if (ret) |
| 319 | goto done; |
| 320 | } |
| 321 | |
| 322 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 323 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 324 | |
| 325 | done: |
| 326 | if (saved_buffer) |
| 327 | free(saved_buffer); |
| 328 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void env_relocate_spec(void) |
| 332 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 333 | char buf[CONFIG_ENV_SIZE]; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 334 | int ret; |
| 335 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 336 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 337 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 338 | if (!env_flash) { |
| 339 | set_default_env("!spi_flash_probe() failed"); |
| 340 | return; |
| 341 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 342 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 343 | ret = spi_flash_read(env_flash, |
| 344 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 345 | if (ret) { |
| 346 | set_default_env("!spi_flash_read() failed"); |
| 347 | goto out; |
| 348 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 349 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 350 | ret = env_import(buf, 1); |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 351 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 352 | if (ret) |
| 353 | gd->env_valid = 1; |
| 354 | out: |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 355 | spi_flash_free(env_flash); |
| 356 | env_flash = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 357 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 358 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 359 | |
| 360 | int env_init(void) |
| 361 | { |
| 362 | /* SPI flash isn't usable before relocation */ |
| 363 | gd->env_addr = (ulong)&default_environment[0]; |
| 364 | gd->env_valid = 1; |
| 365 | |
| 366 | return 0; |
| 367 | } |