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 | |
Clemens Gruber | f7b6144 | 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 | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 62 | int env_init(void) |
| 63 | { |
| 64 | /* use default */ |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 65 | gd->env_addr = (ulong)&default_environment[0]; |
| 66 | gd->env_valid = 1; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 71 | #ifdef CONFIG_SYS_MMC_ENV_PART |
Dmitry Lifshitz | 97d2fb9 | 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 | 1e0f92a | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 77 | static unsigned char env_mmc_orig_hwpart; |
| 78 | |
Dmitry Lifshitz | 97d2fb9 | 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 | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 82 | int dev = mmc_get_env_dev(); |
Dmitry Lifshitz | 97d2fb9 | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 83 | int ret = 0; |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 84 | |
Simon Glass | 11f2bb6 | 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 | 1e0f92a | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 87 | if (ret) |
| 88 | puts("MMC partition switch failed\n"); |
Dmitry Lifshitz | 97d2fb9 | 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 | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 94 | #endif |
| 95 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 96 | static const char *init_mmc_for_env(struct mmc *mmc) |
Dmitry Lifshitz | 97d2fb9 | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 97 | { |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 98 | if (!mmc) |
Hans de Goede | 8764fd6 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 99 | return "!No MMC card found"; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 100 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 101 | if (mmc_init(mmc)) |
Hans de Goede | 8764fd6 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 102 | return "!MMC init failed"; |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 103 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 104 | if (mmc_set_env_part(mmc)) |
Hans de Goede | 8764fd6 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 105 | return "!MMC partition switch failed"; |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 106 | |
| 107 | return NULL; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 108 | } |
| 109 | |
Stephen Warren | 27ee0f3 | 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 | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 113 | int dev = mmc_get_env_dev(); |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 114 | |
Simon Glass | 11f2bb6 | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 115 | blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 116 | #endif |
| 117 | } |
| 118 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 119 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | ecbfb62 | 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 | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 122 | { |
| 123 | uint blk_start, blk_cnt, n; |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 124 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 125 | |
Igor Grinberg | ecbfb62 | 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 | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 128 | |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 129 | n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 130 | |
| 131 | return (n == blk_cnt) ? 0 : -1; |
| 132 | } |
| 133 | |
Michael Heimpold | a1d25a3 | 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 | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 138 | int saveenv(void) |
| 139 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 140 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Clemens Gruber | f7b6144 | 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 | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 143 | u32 offset; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 144 | int ret, copy = 0; |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 145 | const char *errmsg; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 146 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 147 | errmsg = init_mmc_for_env(mmc); |
| 148 | if (errmsg) { |
| 149 | printf("%s\n", errmsg); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 150 | return 1; |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 151 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 152 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 153 | ret = env_export(env_new); |
| 154 | if (ret) |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 155 | goto fini; |
Michael Heimpold | a1d25a3 | 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 | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 169 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev); |
Stephen Warren | 42a3c44 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 170 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 171 | puts("failed\n"); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 172 | ret = 1; |
| 173 | goto fini; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | puts("done\n"); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 177 | ret = 0; |
| 178 | |
Michael Heimpold | a1d25a3 | 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 | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 183 | fini: |
| 184 | fini_mmc_for_env(mmc); |
| 185 | return ret; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 186 | } |
| 187 | #endif /* CONFIG_CMD_SAVEENV */ |
| 188 | |
Igor Grinberg | ecbfb62 | 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 | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 191 | { |
| 192 | uint blk_start, blk_cnt, n; |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 193 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 194 | |
Igor Grinberg | ecbfb62 | 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 | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 197 | |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 198 | n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 199 | |
| 200 | return (n == blk_cnt) ? 0 : -1; |
| 201 | } |
| 202 | |
Michael Heimpold | a1d25a3 | 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 | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 207 | struct mmc *mmc; |
Michael Heimpold | a1d25a3 | 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 | 6ef41fd | 2013-10-04 15:48:03 +0200 | [diff] [blame] | 211 | env_t *ep; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 212 | int ret; |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 213 | int dev = mmc_get_env_dev(); |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 214 | const char *errmsg = NULL; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 215 | |
Markus Niebel | 6ef41fd | 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 | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 219 | mmc = find_mmc_device(dev); |
| 220 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 221 | errmsg = init_mmc_for_env(mmc); |
| 222 | if (errmsg) { |
Michael Heimpold | a1d25a3 | 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 | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 248 | errmsg = "!bad CRC"; |
Michael Heimpold | a1d25a3 | 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 | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 284 | set_default_env(errmsg); |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 285 | #endif |
| 286 | } |
| 287 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 288 | void env_relocate_spec(void) |
| 289 | { |
| 290 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 291 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 292 | struct mmc *mmc; |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 293 | u32 offset; |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 294 | int ret; |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 295 | int dev = mmc_get_env_dev(); |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 296 | const char *errmsg; |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 297 | |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 298 | mmc = find_mmc_device(dev); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 299 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 300 | errmsg = init_mmc_for_env(mmc); |
| 301 | if (errmsg) { |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 302 | ret = 1; |
| 303 | goto err; |
| 304 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 305 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 306 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 307 | ret = 1; |
| 308 | goto fini; |
| 309 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 310 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 311 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 312 | errmsg = "!read failed"; |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 313 | ret = 1; |
| 314 | goto fini; |
| 315 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 316 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 317 | env_import(buf, 1); |
Stephen Warren | 27ee0f3 | 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 | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 324 | set_default_env(errmsg); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 325 | #endif |
| 326 | } |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 327 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |