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