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