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