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