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