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