Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 1 | /* |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 2 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
Terry Lv | a806035 | 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 | 6d1d51b | 2010-11-10 07:39:23 +0800 | [diff] [blame] | 32 | #include <search.h> |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 33 | #include <errno.h> |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 34 | |
Michael Heimpold | d196bd8 | 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 | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 40 | char *env_name_spec = "MMC"; |
| 41 | |
| 42 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 994bc67 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 43 | env_t *env_ptr = &environment; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 44 | #else /* ! ENV_IS_EMBEDDED */ |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 45 | env_t *env_ptr; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 46 | #endif /* ENV_IS_EMBEDDED */ |
| 47 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 48 | DECLARE_GLOBAL_DATA_PTR; |
| 49 | |
Mingkai Hu | 97039ab | 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 | d196bd8 | 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 | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 55 | { |
| 56 | *env_addr = CONFIG_ENV_OFFSET; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 57 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 58 | if (copy) |
| 59 | *env_addr = CONFIG_ENV_OFFSET_REDUND; |
| 60 | #endif |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 61 | return 0; |
| 62 | } |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 63 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 64 | int env_init(void) |
| 65 | { |
| 66 | /* use default */ |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 67 | gd->env_addr = (ulong)&default_environment[0]; |
| 68 | gd->env_valid = 1; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 73 | static int init_mmc_for_env(struct mmc *mmc) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 74 | { |
| 75 | if (!mmc) { |
| 76 | puts("No MMC card found\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | if (mmc_init(mmc)) { |
| 81 | puts("MMC init failed\n"); |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 82 | return -1; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 83 | } |
| 84 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 85 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 86 | if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) { |
| 87 | if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, |
| 88 | CONFIG_SYS_MMC_ENV_PART)) { |
| 89 | puts("MMC partition switch failed\n"); |
| 90 | return -1; |
| 91 | } |
| 92 | } |
| 93 | #endif |
| 94 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 98 | static void fini_mmc_for_env(struct mmc *mmc) |
| 99 | { |
| 100 | #ifdef CONFIG_SYS_MMC_ENV_PART |
| 101 | if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) |
| 102 | mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV, |
| 103 | mmc->part_num); |
| 104 | #endif |
| 105 | } |
| 106 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 107 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 108 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 109 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 110 | { |
| 111 | uint blk_start, blk_cnt, n; |
| 112 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 113 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 114 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 115 | |
| 116 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 117 | blk_cnt, (u_char *)buffer); |
| 118 | |
| 119 | return (n == blk_cnt) ? 0 : -1; |
| 120 | } |
| 121 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 122 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 123 | static unsigned char env_flags; |
| 124 | #endif |
| 125 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 126 | int saveenv(void) |
| 127 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 128 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 129 | ssize_t len; |
| 130 | char *res; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 131 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 132 | u32 offset; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 133 | int ret, copy = 0; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 134 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 135 | if (init_mmc_for_env(mmc)) |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 136 | return 1; |
| 137 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 138 | res = (char *)&env_new->data; |
Joe Hershberger | be11235 | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 139 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 140 | if (len < 0) { |
| 141 | error("Cannot export environment: errno = %d\n", errno); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 142 | ret = 1; |
| 143 | goto fini; |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 144 | } |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 145 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 146 | env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE); |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 149 | env_new->flags = ++env_flags; /* increase the serial */ |
| 150 | |
| 151 | if (gd->env_valid == 1) |
| 152 | copy = 1; |
| 153 | #endif |
| 154 | |
| 155 | if (mmc_get_env_addr(mmc, copy, &offset)) { |
| 156 | ret = 1; |
| 157 | goto fini; |
| 158 | } |
| 159 | |
| 160 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", |
| 161 | CONFIG_SYS_MMC_ENV_DEV); |
Stephen Warren | 4036b63 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 162 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 163 | puts("failed\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 164 | ret = 1; |
| 165 | goto fini; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | puts("done\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 169 | ret = 0; |
| 170 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 171 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 172 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
| 173 | #endif |
| 174 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 175 | fini: |
| 176 | fini_mmc_for_env(mmc); |
| 177 | return ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 178 | } |
| 179 | #endif /* CONFIG_CMD_SAVEENV */ |
| 180 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 181 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 182 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 183 | { |
| 184 | uint blk_start, blk_cnt, n; |
| 185 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 186 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 187 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 188 | |
| 189 | n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 190 | blk_cnt, (uchar *)buffer); |
| 191 | |
| 192 | return (n == blk_cnt) ? 0 : -1; |
| 193 | } |
| 194 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 196 | void env_relocate_spec(void) |
| 197 | { |
| 198 | #if !defined(ENV_IS_EMBEDDED) |
| 199 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
| 200 | u32 offset1, offset2; |
| 201 | int read1_fail = 0, read2_fail = 0; |
| 202 | int crc1_ok = 0, crc2_ok = 0; |
| 203 | env_t *ep, *tmp_env1, *tmp_env2; |
| 204 | int ret; |
| 205 | |
| 206 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
| 207 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
| 208 | if (tmp_env1 == NULL || tmp_env2 == NULL) { |
| 209 | puts("Can't allocate buffers for environment\n"); |
| 210 | ret = 1; |
| 211 | goto err; |
| 212 | } |
| 213 | |
| 214 | if (init_mmc_for_env(mmc)) { |
| 215 | ret = 1; |
| 216 | goto err; |
| 217 | } |
| 218 | |
| 219 | if (mmc_get_env_addr(mmc, 0, &offset1) || |
| 220 | mmc_get_env_addr(mmc, 1, &offset2)) { |
| 221 | ret = 1; |
| 222 | goto fini; |
| 223 | } |
| 224 | |
| 225 | read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1); |
| 226 | read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); |
| 227 | |
| 228 | if (read1_fail && read2_fail) |
| 229 | puts("*** Error - No Valid Environment Area found\n"); |
| 230 | else if (read1_fail || read2_fail) |
| 231 | puts("*** Warning - some problems detected " |
| 232 | "reading environment; recovered successfully\n"); |
| 233 | |
| 234 | crc1_ok = !read1_fail && |
| 235 | (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 236 | crc2_ok = !read2_fail && |
| 237 | (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 238 | |
| 239 | if (!crc1_ok && !crc2_ok) { |
| 240 | ret = 1; |
| 241 | goto fini; |
| 242 | } else if (crc1_ok && !crc2_ok) { |
| 243 | gd->env_valid = 1; |
| 244 | } else if (!crc1_ok && crc2_ok) { |
| 245 | gd->env_valid = 2; |
| 246 | } else { |
| 247 | /* both ok - check serial */ |
| 248 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 249 | gd->env_valid = 2; |
| 250 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 251 | gd->env_valid = 1; |
| 252 | else if (tmp_env1->flags > tmp_env2->flags) |
| 253 | gd->env_valid = 1; |
| 254 | else if (tmp_env2->flags > tmp_env1->flags) |
| 255 | gd->env_valid = 2; |
| 256 | else /* flags are equal - almost impossible */ |
| 257 | gd->env_valid = 1; |
| 258 | } |
| 259 | |
| 260 | free(env_ptr); |
| 261 | |
| 262 | if (gd->env_valid == 1) |
| 263 | ep = tmp_env1; |
| 264 | else |
| 265 | ep = tmp_env2; |
| 266 | |
| 267 | env_flags = ep->flags; |
| 268 | env_import((char *)ep, 0); |
| 269 | ret = 0; |
| 270 | |
| 271 | fini: |
| 272 | fini_mmc_for_env(mmc); |
| 273 | err: |
| 274 | if (ret) |
| 275 | set_default_env(NULL); |
| 276 | |
| 277 | free(tmp_env1); |
| 278 | free(tmp_env2); |
| 279 | #endif |
| 280 | } |
| 281 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 282 | void env_relocate_spec(void) |
| 283 | { |
| 284 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 285 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 286 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 287 | u32 offset; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 288 | int ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 289 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 290 | if (init_mmc_for_env(mmc)) { |
| 291 | ret = 1; |
| 292 | goto err; |
| 293 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 294 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 295 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 296 | ret = 1; |
| 297 | goto fini; |
| 298 | } |
| 299 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 300 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 301 | ret = 1; |
| 302 | goto fini; |
| 303 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 304 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 305 | env_import(buf, 1); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 306 | ret = 0; |
| 307 | |
| 308 | fini: |
| 309 | fini_mmc_for_env(mmc); |
| 310 | err: |
| 311 | if (ret) |
| 312 | set_default_env(NULL); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 313 | #endif |
| 314 | } |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 315 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |