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 | |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 65 | #ifdef CONFIG_SYS_MMC_ENV_PART |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 66 | __weak uint mmc_get_env_part(struct mmc *mmc) |
| 67 | { |
| 68 | return CONFIG_SYS_MMC_ENV_PART; |
| 69 | } |
| 70 | |
| 71 | static int mmc_set_env_part(struct mmc *mmc) |
| 72 | { |
| 73 | uint part = mmc_get_env_part(mmc); |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 74 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 75 | int ret = 0; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 76 | |
| 77 | #ifdef CONFIG_SPL_BUILD |
| 78 | dev = 0; |
| 79 | #endif |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 80 | |
| 81 | if (part != mmc->part_num) { |
| 82 | ret = mmc_switch_part(dev, part); |
| 83 | if (ret) |
| 84 | puts("MMC partition switch failed\n"); |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | #else |
| 90 | static inline int mmc_set_env_part(struct mmc *mmc) {return 0; }; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 91 | #endif |
| 92 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 93 | static const char *init_mmc_for_env(struct mmc *mmc) |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 94 | { |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 95 | if (!mmc) |
| 96 | return "No MMC card found"; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 97 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 98 | if (mmc_init(mmc)) |
| 99 | return "MMC init failed"; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 100 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 101 | if (mmc_set_env_part(mmc)) |
| 102 | return "MMC partition switch failed"; |
| 103 | |
| 104 | return NULL; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 107 | static void fini_mmc_for_env(struct mmc *mmc) |
| 108 | { |
| 109 | #ifdef CONFIG_SYS_MMC_ENV_PART |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 110 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
| 111 | |
| 112 | #ifdef CONFIG_SPL_BUILD |
| 113 | dev = 0; |
| 114 | #endif |
Peter Bigot | 19345d7 | 2014-09-03 12:22:43 -0500 | [diff] [blame] | 115 | if (mmc_get_env_part(mmc) != mmc->part_num) |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 116 | mmc_switch_part(dev, mmc->part_num); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 117 | #endif |
| 118 | } |
| 119 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 120 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 121 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 122 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 123 | { |
| 124 | uint blk_start, blk_cnt, n; |
| 125 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 126 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 127 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 128 | |
| 129 | n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start, |
| 130 | blk_cnt, (u_char *)buffer); |
| 131 | |
| 132 | return (n == blk_cnt) ? 0 : -1; |
| 133 | } |
| 134 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 135 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 136 | static unsigned char env_flags; |
| 137 | #endif |
| 138 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 139 | int saveenv(void) |
| 140 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 141 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 142 | struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV); |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 143 | u32 offset; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 144 | int ret, copy = 0; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 145 | const char *errmsg; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 146 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 147 | errmsg = init_mmc_for_env(mmc); |
| 148 | if (errmsg) { |
| 149 | printf("%s\n", errmsg); |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 150 | return 1; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 151 | } |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 152 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 153 | ret = env_export(env_new); |
| 154 | if (ret) |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 155 | goto fini; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 156 | |
| 157 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 158 | env_new->flags = ++env_flags; /* increase the serial */ |
| 159 | |
| 160 | if (gd->env_valid == 1) |
| 161 | copy = 1; |
| 162 | #endif |
| 163 | |
| 164 | if (mmc_get_env_addr(mmc, copy, &offset)) { |
| 165 | ret = 1; |
| 166 | goto fini; |
| 167 | } |
| 168 | |
| 169 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", |
| 170 | CONFIG_SYS_MMC_ENV_DEV); |
Stephen Warren | 4036b63 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 171 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 172 | puts("failed\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 173 | ret = 1; |
| 174 | goto fini; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | puts("done\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 178 | ret = 0; |
| 179 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 180 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 181 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
| 182 | #endif |
| 183 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 184 | fini: |
| 185 | fini_mmc_for_env(mmc); |
| 186 | return ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 187 | } |
| 188 | #endif /* CONFIG_CMD_SAVEENV */ |
| 189 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 190 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 191 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 192 | { |
| 193 | uint blk_start, blk_cnt, n; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 194 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
| 195 | |
| 196 | #ifdef CONFIG_SPL_BUILD |
| 197 | dev = 0; |
| 198 | #endif |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 199 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 200 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 201 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 202 | |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 203 | 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] | 204 | |
| 205 | return (n == blk_cnt) ? 0 : -1; |
| 206 | } |
| 207 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 208 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 209 | void env_relocate_spec(void) |
| 210 | { |
| 211 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 212 | struct mmc *mmc; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 213 | u32 offset1, offset2; |
| 214 | int read1_fail = 0, read2_fail = 0; |
| 215 | int crc1_ok = 0, crc2_ok = 0; |
Markus Niebel | 452a272 | 2013-10-04 15:48:03 +0200 | [diff] [blame] | 216 | env_t *ep; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 217 | int ret; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 218 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 219 | const char *errmsg = NULL; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 220 | |
Markus Niebel | 452a272 | 2013-10-04 15:48:03 +0200 | [diff] [blame] | 221 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1); |
| 222 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1); |
| 223 | |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 224 | #ifdef CONFIG_SPL_BUILD |
| 225 | dev = 0; |
| 226 | #endif |
| 227 | |
| 228 | mmc = find_mmc_device(dev); |
| 229 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 230 | errmsg = init_mmc_for_env(mmc); |
| 231 | if (errmsg) { |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 232 | ret = 1; |
| 233 | goto err; |
| 234 | } |
| 235 | |
| 236 | if (mmc_get_env_addr(mmc, 0, &offset1) || |
| 237 | mmc_get_env_addr(mmc, 1, &offset2)) { |
| 238 | ret = 1; |
| 239 | goto fini; |
| 240 | } |
| 241 | |
| 242 | read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1); |
| 243 | read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); |
| 244 | |
| 245 | if (read1_fail && read2_fail) |
| 246 | puts("*** Error - No Valid Environment Area found\n"); |
| 247 | else if (read1_fail || read2_fail) |
| 248 | puts("*** Warning - some problems detected " |
| 249 | "reading environment; recovered successfully\n"); |
| 250 | |
| 251 | crc1_ok = !read1_fail && |
| 252 | (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 253 | crc2_ok = !read2_fail && |
| 254 | (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 255 | |
| 256 | if (!crc1_ok && !crc2_ok) { |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 257 | errmsg = "!bad CRC"; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 258 | ret = 1; |
| 259 | goto fini; |
| 260 | } else if (crc1_ok && !crc2_ok) { |
| 261 | gd->env_valid = 1; |
| 262 | } else if (!crc1_ok && crc2_ok) { |
| 263 | gd->env_valid = 2; |
| 264 | } else { |
| 265 | /* both ok - check serial */ |
| 266 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 267 | gd->env_valid = 2; |
| 268 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 269 | gd->env_valid = 1; |
| 270 | else if (tmp_env1->flags > tmp_env2->flags) |
| 271 | gd->env_valid = 1; |
| 272 | else if (tmp_env2->flags > tmp_env1->flags) |
| 273 | gd->env_valid = 2; |
| 274 | else /* flags are equal - almost impossible */ |
| 275 | gd->env_valid = 1; |
| 276 | } |
| 277 | |
| 278 | free(env_ptr); |
| 279 | |
| 280 | if (gd->env_valid == 1) |
| 281 | ep = tmp_env1; |
| 282 | else |
| 283 | ep = tmp_env2; |
| 284 | |
| 285 | env_flags = ep->flags; |
| 286 | env_import((char *)ep, 0); |
| 287 | ret = 0; |
| 288 | |
| 289 | fini: |
| 290 | fini_mmc_for_env(mmc); |
| 291 | err: |
| 292 | if (ret) |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 293 | set_default_env(errmsg); |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 294 | #endif |
| 295 | } |
| 296 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 297 | void env_relocate_spec(void) |
| 298 | { |
| 299 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 300 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 301 | struct mmc *mmc; |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 302 | u32 offset; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 303 | int ret; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 304 | int dev = CONFIG_SYS_MMC_ENV_DEV; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 305 | const char *errmsg; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 306 | |
| 307 | #ifdef CONFIG_SPL_BUILD |
| 308 | dev = 0; |
| 309 | #endif |
| 310 | |
| 311 | mmc = find_mmc_device(dev); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 312 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 313 | errmsg = init_mmc_for_env(mmc); |
| 314 | if (errmsg) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 315 | ret = 1; |
| 316 | goto err; |
| 317 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 318 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 319 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 320 | ret = 1; |
| 321 | goto fini; |
| 322 | } |
| 323 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 324 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 325 | errmsg = "!read failed"; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 326 | ret = 1; |
| 327 | goto fini; |
| 328 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 329 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 330 | env_import(buf, 1); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 331 | ret = 0; |
| 332 | |
| 333 | fini: |
| 334 | fini_mmc_for_env(mmc); |
| 335 | err: |
| 336 | if (ret) |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 337 | set_default_env(errmsg); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 338 | #endif |
| 339 | } |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 340 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |