Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 1 | /* |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 2 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | /* #define DEBUG */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | |
| 27 | #include <command.h> |
| 28 | #include <environment.h> |
| 29 | #include <linux/stddef.h> |
| 30 | #include <malloc.h> |
| 31 | #include <mmc.h> |
Lei Wen | 9f84f21 | 2010-11-10 07:39:23 +0800 | [diff] [blame] | 32 | #include <search.h> |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 33 | #include <errno.h> |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 34 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 35 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
| 36 | (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) |
| 37 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE |
| 38 | #endif |
| 39 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 40 | char *env_name_spec = "MMC"; |
| 41 | |
| 42 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 23b54b9 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 43 | env_t *env_ptr = &environment; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 44 | #else /* ! ENV_IS_EMBEDDED */ |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 45 | env_t *env_ptr; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 46 | #endif /* ENV_IS_EMBEDDED */ |
| 47 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 48 | DECLARE_GLOBAL_DATA_PTR; |
| 49 | |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 50 | #if !defined(CONFIG_ENV_OFFSET) |
| 51 | #define CONFIG_ENV_OFFSET 0 |
| 52 | #endif |
| 53 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 54 | __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 55 | { |
Stephen Warren | 24dc203 | 2013-06-11 15:14:02 -0600 | [diff] [blame] | 56 | s64 offset; |
| 57 | |
| 58 | offset = CONFIG_ENV_OFFSET; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 59 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 60 | if (copy) |
Stephen Warren | 24dc203 | 2013-06-11 15:14:02 -0600 | [diff] [blame] | 61 | offset = CONFIG_ENV_OFFSET_REDUND; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 62 | #endif |
Stephen Warren | 24dc203 | 2013-06-11 15:14:02 -0600 | [diff] [blame] | 63 | |
| 64 | if (offset < 0) |
| 65 | offset += mmc->capacity; |
| 66 | |
| 67 | *env_addr = offset; |
| 68 | |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 71 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 72 | int env_init(void) |
| 73 | { |
| 74 | /* use default */ |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 75 | gd->env_addr = (ulong)&default_environment[0]; |
| 76 | gd->env_valid = 1; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 81 | static int init_mmc_for_env(struct mmc *mmc) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 82 | { |
| 83 | if (!mmc) { |
| 84 | puts("No MMC card found\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | if (mmc_init(mmc)) { |
| 89 | puts("MMC init failed\n"); |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 90 | return -1; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 93 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 94 | if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) { |
| 95 | if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, |
| 96 | CONFIG_SYS_MMC_ENV_PART)) { |
| 97 | puts("MMC partition switch failed\n"); |
| 98 | return -1; |
| 99 | } |
| 100 | } |
| 101 | #endif |
| 102 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 106 | static void fini_mmc_for_env(struct mmc *mmc) |
| 107 | { |
| 108 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 109 | if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) |
| 110 | mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, |
| 111 | mmc->part_num); |
| 112 | #endif |
| 113 | } |
| 114 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 115 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 116 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 117 | unsigned long offset, const void *buffer) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 118 | { |
| 119 | uint blk_start, blk_cnt, n; |
| 120 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 121 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 122 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 123 | |
| 124 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 125 | blk_cnt, (u_char *)buffer); |
| 126 | |
| 127 | return (n == blk_cnt) ? 0 : -1; |
| 128 | } |
| 129 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 131 | static unsigned char env_flags; |
| 132 | #endif |
| 133 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 134 | int saveenv(void) |
| 135 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 136 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 137 | ssize_t len; |
| 138 | char *res; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 139 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 140 | u32 offset; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 141 | int ret, copy = 0; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 142 | |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 143 | if (init_mmc_for_env(mmc)) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 144 | return 1; |
| 145 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 146 | res = (char *)&env_new->data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 147 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 148 | if (len < 0) { |
| 149 | error("Cannot export environment: errno = %d\n", errno); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 150 | ret = 1; |
| 151 | goto fini; |
Lei Wen | ff833ed | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 152 | } |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 153 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 154 | env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE); |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 155 | |
| 156 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 157 | env_new->flags = ++env_flags; /* increase the serial */ |
| 158 | |
| 159 | if (gd->env_valid == 1) |
| 160 | copy = 1; |
| 161 | #endif |
| 162 | |
| 163 | if (mmc_get_env_addr(mmc, copy, &offset)) { |
| 164 | ret = 1; |
| 165 | goto fini; |
| 166 | } |
| 167 | |
| 168 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", |
| 169 | CONFIG_SYS_MMC_ENV_DEV); |
Stephen Warren | 42a3c44 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 170 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 171 | puts("failed\n"); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 172 | ret = 1; |
| 173 | goto fini; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | puts("done\n"); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 177 | ret = 0; |
| 178 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 179 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 180 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
| 181 | #endif |
| 182 | |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 183 | fini: |
| 184 | fini_mmc_for_env(mmc); |
| 185 | return ret; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 186 | } |
| 187 | #endif /* CONFIG_CMD_SAVEENV */ |
| 188 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 189 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 190 | unsigned long offset, const void *buffer) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 191 | { |
| 192 | uint blk_start, blk_cnt, n; |
| 193 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 194 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 195 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 196 | |
| 197 | n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 198 | blk_cnt, (uchar *)buffer); |
| 199 | |
| 200 | return (n == blk_cnt) ? 0 : -1; |
| 201 | } |
| 202 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 203 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 204 | void env_relocate_spec(void) |
| 205 | { |
| 206 | #if !defined(ENV_IS_EMBEDDED) |
| 207 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
| 208 | u32 offset1, offset2; |
| 209 | int read1_fail = 0, read2_fail = 0; |
| 210 | int crc1_ok = 0, crc2_ok = 0; |
| 211 | env_t *ep, *tmp_env1, *tmp_env2; |
| 212 | int ret; |
| 213 | |
| 214 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
| 215 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
| 216 | if (tmp_env1 == NULL || tmp_env2 == NULL) { |
| 217 | puts("Can't allocate buffers for environment\n"); |
| 218 | ret = 1; |
| 219 | goto err; |
| 220 | } |
| 221 | |
| 222 | if (init_mmc_for_env(mmc)) { |
| 223 | ret = 1; |
| 224 | goto err; |
| 225 | } |
| 226 | |
| 227 | if (mmc_get_env_addr(mmc, 0, &offset1) || |
| 228 | mmc_get_env_addr(mmc, 1, &offset2)) { |
| 229 | ret = 1; |
| 230 | goto fini; |
| 231 | } |
| 232 | |
| 233 | read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1); |
| 234 | read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); |
| 235 | |
| 236 | if (read1_fail && read2_fail) |
| 237 | puts("*** Error - No Valid Environment Area found\n"); |
| 238 | else if (read1_fail || read2_fail) |
| 239 | puts("*** Warning - some problems detected " |
| 240 | "reading environment; recovered successfully\n"); |
| 241 | |
| 242 | crc1_ok = !read1_fail && |
| 243 | (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 244 | crc2_ok = !read2_fail && |
| 245 | (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 246 | |
| 247 | if (!crc1_ok && !crc2_ok) { |
| 248 | ret = 1; |
| 249 | goto fini; |
| 250 | } else if (crc1_ok && !crc2_ok) { |
| 251 | gd->env_valid = 1; |
| 252 | } else if (!crc1_ok && crc2_ok) { |
| 253 | gd->env_valid = 2; |
| 254 | } else { |
| 255 | /* both ok - check serial */ |
| 256 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 257 | gd->env_valid = 2; |
| 258 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 259 | gd->env_valid = 1; |
| 260 | else if (tmp_env1->flags > tmp_env2->flags) |
| 261 | gd->env_valid = 1; |
| 262 | else if (tmp_env2->flags > tmp_env1->flags) |
| 263 | gd->env_valid = 2; |
| 264 | else /* flags are equal - almost impossible */ |
| 265 | gd->env_valid = 1; |
| 266 | } |
| 267 | |
| 268 | free(env_ptr); |
| 269 | |
| 270 | if (gd->env_valid == 1) |
| 271 | ep = tmp_env1; |
| 272 | else |
| 273 | ep = tmp_env2; |
| 274 | |
| 275 | env_flags = ep->flags; |
| 276 | env_import((char *)ep, 0); |
| 277 | ret = 0; |
| 278 | |
| 279 | fini: |
| 280 | fini_mmc_for_env(mmc); |
| 281 | err: |
| 282 | if (ret) |
| 283 | set_default_env(NULL); |
| 284 | |
| 285 | free(tmp_env1); |
| 286 | free(tmp_env2); |
| 287 | #endif |
| 288 | } |
| 289 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 290 | void env_relocate_spec(void) |
| 291 | { |
| 292 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 293 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 294 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 295 | u32 offset; |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 296 | int ret; |
| 297 | |
| 298 | if (init_mmc_for_env(mmc)) { |
| 299 | ret = 1; |
| 300 | goto err; |
| 301 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 302 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 303 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 304 | ret = 1; |
| 305 | goto fini; |
| 306 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 307 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 308 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 309 | ret = 1; |
| 310 | goto fini; |
| 311 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 312 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 313 | env_import(buf, 1); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 314 | ret = 0; |
| 315 | |
| 316 | fini: |
| 317 | fini_mmc_for_env(mmc); |
| 318 | err: |
| 319 | if (ret) |
| 320 | set_default_env(NULL); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 321 | #endif |
| 322 | } |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 323 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |