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