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