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 |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 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 |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 49 | static ulong env_offset = CONFIG_ENV_OFFSET; |
| 50 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 51 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 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 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 58 | char *env_name_spec = "SPI Flash"; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 59 | |
| 60 | static struct spi_flash *env_flash; |
| 61 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 62 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 63 | int saveenv(void) |
| 64 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 65 | env_t env_new; |
| 66 | ssize_t len; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 67 | char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG; |
| 68 | u32 saved_size, saved_offset, sector = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | int ret; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 70 | |
| 71 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 72 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 73 | CONFIG_ENV_SPI_CS, |
| 74 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 75 | if (!env_flash) { |
| 76 | set_default_env("!spi_flash_probe() failed"); |
| 77 | return 1; |
| 78 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 81 | res = (char *)&env_new.data; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 82 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 83 | if (len < 0) { |
| 84 | error("Cannot export environment: errno = %d\n", errno); |
| 85 | return 1; |
| 86 | } |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 87 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 88 | env_new.flags = ACTIVE_FLAG; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 89 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 90 | if (gd->env_valid == 1) { |
| 91 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
| 92 | env_offset = CONFIG_ENV_OFFSET; |
| 93 | } else { |
| 94 | env_new_offset = CONFIG_ENV_OFFSET; |
| 95 | env_offset = CONFIG_ENV_OFFSET_REDUND; |
| 96 | } |
| 97 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 98 | /* Is the sector larger than the env (i.e. embedded) */ |
| 99 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 100 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 101 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; |
| 102 | saved_buffer = malloc(saved_size); |
| 103 | if (!saved_buffer) { |
| 104 | ret = 1; |
| 105 | goto done; |
| 106 | } |
| 107 | ret = spi_flash_read(env_flash, saved_offset, |
| 108 | saved_size, saved_buffer); |
| 109 | if (ret) |
| 110 | goto done; |
| 111 | } |
| 112 | |
| 113 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 114 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 115 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
| 116 | sector++; |
| 117 | } |
| 118 | |
| 119 | puts("Erasing SPI flash..."); |
| 120 | ret = spi_flash_erase(env_flash, env_new_offset, |
| 121 | sector * CONFIG_ENV_SECT_SIZE); |
| 122 | if (ret) |
| 123 | goto done; |
| 124 | |
| 125 | puts("Writing to SPI flash..."); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 126 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 127 | ret = spi_flash_write(env_flash, env_new_offset, |
| 128 | CONFIG_ENV_SIZE, &env_new); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 129 | if (ret) |
| 130 | goto done; |
| 131 | |
| 132 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 133 | ret = spi_flash_write(env_flash, saved_offset, |
| 134 | saved_size, saved_buffer); |
| 135 | if (ret) |
| 136 | goto done; |
| 137 | } |
| 138 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 139 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
| 140 | sizeof(env_new.flags), &flag); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 141 | if (ret) |
| 142 | goto done; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 143 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 144 | puts("done\n"); |
| 145 | |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 146 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 147 | |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 148 | printf("Valid environment: %d\n", (int)gd->env_valid); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 149 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 150 | done: |
| 151 | if (saved_buffer) |
| 152 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 153 | |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | void env_relocate_spec(void) |
| 158 | { |
| 159 | int ret; |
| 160 | int crc1_ok = 0, crc2_ok = 0; |
| 161 | env_t *tmp_env1 = NULL; |
| 162 | env_t *tmp_env2 = NULL; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 163 | env_t *ep = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 164 | |
| 165 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 166 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 167 | |
| 168 | if (!tmp_env1 || !tmp_env2) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 169 | set_default_env("!malloc() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 170 | goto out; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 174 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 175 | if (!env_flash) { |
| 176 | set_default_env("!spi_flash_probe() failed"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 177 | goto out; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 178 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 179 | |
| 180 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
| 181 | CONFIG_ENV_SIZE, tmp_env1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 182 | if (ret) { |
| 183 | set_default_env("!spi_flash_read() failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 184 | goto err_read; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 185 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 186 | |
| 187 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) |
| 188 | crc1_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 189 | |
| 190 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, |
| 191 | CONFIG_ENV_SIZE, tmp_env2); |
| 192 | if (!ret) { |
| 193 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) |
| 194 | crc2_ok = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 197 | if (!crc1_ok && !crc2_ok) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 198 | set_default_env("!bad CRC"); |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 199 | goto err_read; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 200 | } else if (crc1_ok && !crc2_ok) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 201 | gd->env_valid = 1; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 202 | } else if (!crc1_ok && crc2_ok) { |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 203 | gd->env_valid = 2; |
| 204 | } else if (tmp_env1->flags == ACTIVE_FLAG && |
| 205 | tmp_env2->flags == OBSOLETE_FLAG) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 206 | gd->env_valid = 1; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 207 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
| 208 | tmp_env2->flags == ACTIVE_FLAG) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 209 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 210 | } else if (tmp_env1->flags == tmp_env2->flags) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 211 | gd->env_valid = 2; |
thomas.langer@lantiq.com | 97dfdcf | 2011-03-29 02:35:14 +0000 | [diff] [blame] | 212 | } else if (tmp_env1->flags == 0xFF) { |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 213 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 214 | } else { |
| 215 | /* |
| 216 | * this differs from code in env_flash.c, but I think a sane |
| 217 | * default path is desirable. |
| 218 | */ |
| 219 | gd->env_valid = 2; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 220 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 221 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 222 | if (gd->env_valid == 1) |
| 223 | ep = tmp_env1; |
| 224 | else |
| 225 | ep = tmp_env2; |
| 226 | |
| 227 | ret = env_import((char *)ep, 0); |
| 228 | if (!ret) { |
| 229 | error("Cannot import environment: errno = %d\n", errno); |
| 230 | set_default_env("env_import failed"); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 231 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 232 | |
| 233 | err_read: |
| 234 | spi_flash_free(env_flash); |
| 235 | env_flash = NULL; |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 236 | out: |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 237 | free(tmp_env1); |
| 238 | free(tmp_env2); |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 239 | } |
| 240 | #else |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 241 | int saveenv(void) |
| 242 | { |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 243 | u32 saved_size, saved_offset, sector = 1; |
| 244 | char *res, *saved_buffer = NULL; |
| 245 | int ret = 1; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 246 | env_t env_new; |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 247 | ssize_t len; |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 248 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 249 | if (!env_flash) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 250 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
| 251 | CONFIG_ENV_SPI_CS, |
| 252 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
| 253 | if (!env_flash) { |
| 254 | set_default_env("!spi_flash_probe() failed"); |
| 255 | return 1; |
| 256 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 259 | /* Is the sector larger than the env (i.e. embedded) */ |
| 260 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
| 261 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; |
| 262 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; |
| 263 | saved_buffer = malloc(saved_size); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 264 | if (!saved_buffer) |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 265 | goto done; |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 266 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 267 | ret = spi_flash_read(env_flash, saved_offset, |
| 268 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 269 | if (ret) |
| 270 | goto done; |
| 271 | } |
| 272 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 273 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
| 274 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; |
| 275 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) |
TsiChung Liew | 08792d7 | 2008-08-06 19:37:17 -0500 | [diff] [blame] | 276 | sector++; |
| 277 | } |
| 278 | |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 279 | res = (char *)&env_new.data; |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 280 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 281 | if (len < 0) { |
| 282 | error("Cannot export environment: errno = %d\n", errno); |
| 283 | goto done; |
| 284 | } |
| 285 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
| 286 | |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 287 | puts("Erasing SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 288 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
| 289 | sector * CONFIG_ENV_SECT_SIZE); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 290 | if (ret) |
| 291 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 292 | |
| 293 | puts("Writing to SPI flash..."); |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 294 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
| 295 | CONFIG_ENV_SIZE, &env_new); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 296 | if (ret) |
| 297 | goto done; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 298 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 299 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
Stefano Babic | 5f6c286 | 2010-10-27 11:06:20 +0200 | [diff] [blame] | 300 | ret = spi_flash_write(env_flash, saved_offset, |
| 301 | saved_size, saved_buffer); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 302 | if (ret) |
| 303 | goto done; |
| 304 | } |
| 305 | |
| 306 | ret = 0; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 307 | puts("done\n"); |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 308 | |
| 309 | done: |
| 310 | if (saved_buffer) |
| 311 | free(saved_buffer); |
Igor Grinberg | c3c1bed | 2011-11-07 01:14:08 +0000 | [diff] [blame] | 312 | |
Mike Frysinger | 9af10d5 | 2008-12-11 06:23:37 -0500 | [diff] [blame] | 313 | return ret; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void env_relocate_spec(void) |
| 317 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 318 | char buf[CONFIG_ENV_SIZE]; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 319 | int ret; |
| 320 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 321 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
| 322 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 323 | if (!env_flash) { |
| 324 | set_default_env("!spi_flash_probe() failed"); |
| 325 | return; |
| 326 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 327 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 328 | ret = spi_flash_read(env_flash, |
| 329 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); |
| 330 | if (ret) { |
| 331 | set_default_env("!spi_flash_read() failed"); |
| 332 | goto out; |
| 333 | } |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 334 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 335 | ret = env_import(buf, 1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 336 | if (ret) |
| 337 | gd->env_valid = 1; |
| 338 | out: |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 339 | spi_flash_free(env_flash); |
| 340 | env_flash = NULL; |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 341 | } |
Wolfgang Wegner | 0cae3d4 | 2010-04-23 17:22:55 +0200 | [diff] [blame] | 342 | #endif |
Haavard Skinnemoen | eb8951c | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 343 | |
| 344 | int env_init(void) |
| 345 | { |
| 346 | /* SPI flash isn't usable before relocation */ |
| 347 | gd->env_addr = (ulong)&default_environment[0]; |
| 348 | gd->env_valid = 1; |
| 349 | |
| 350 | return 0; |
| 351 | } |