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