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