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]; |
| 89 | gd->env_valid = 1; |
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 | 86d78c7 | 2017-04-23 20:02:04 -0600 | [diff] [blame] | 124 | #ifndef CONFIG_BLK |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 125 | if (mmc_init(mmc)) |
Hans de Goede | 8764fd6 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 126 | return "!MMC init failed"; |
Simon Glass | 86d78c7 | 2017-04-23 20:02:04 -0600 | [diff] [blame] | 127 | #endif |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 128 | if (mmc_set_env_part(mmc)) |
Hans de Goede | 8764fd6 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 129 | return "!MMC partition switch failed"; |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 130 | |
| 131 | return NULL; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 134 | static void fini_mmc_for_env(struct mmc *mmc) |
| 135 | { |
| 136 | #ifdef CONFIG_SYS_MMC_ENV_PART |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 137 | int dev = mmc_get_env_dev(); |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 138 | |
Simon Glass | 11f2bb6 | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 139 | blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 140 | #endif |
| 141 | } |
| 142 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 143 | #ifdef CONFIG_CMD_SAVEENV |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 144 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 145 | unsigned long offset, const void *buffer) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 146 | { |
| 147 | uint blk_start, blk_cnt, n; |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 148 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 149 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 150 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 151 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 152 | |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 153 | n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 154 | |
| 155 | return (n == blk_cnt) ? 0 : -1; |
| 156 | } |
| 157 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 158 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 159 | static unsigned char env_flags; |
| 160 | #endif |
| 161 | |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 162 | int saveenv(void) |
| 163 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 164 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 165 | int dev = mmc_get_env_dev(); |
| 166 | struct mmc *mmc = find_mmc_device(dev); |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 167 | u32 offset; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 168 | int ret, copy = 0; |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 169 | const char *errmsg; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 170 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 171 | errmsg = init_mmc_for_env(mmc); |
| 172 | if (errmsg) { |
| 173 | printf("%s\n", errmsg); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 174 | return 1; |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 175 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 176 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 177 | ret = env_export(env_new); |
| 178 | if (ret) |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 179 | goto fini; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 180 | |
| 181 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 182 | env_new->flags = ++env_flags; /* increase the serial */ |
| 183 | |
| 184 | if (gd->env_valid == 1) |
| 185 | copy = 1; |
| 186 | #endif |
| 187 | |
| 188 | if (mmc_get_env_addr(mmc, copy, &offset)) { |
| 189 | ret = 1; |
| 190 | goto fini; |
| 191 | } |
| 192 | |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 193 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev); |
Stephen Warren | 42a3c44 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 194 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 195 | puts("failed\n"); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 196 | ret = 1; |
| 197 | goto fini; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | puts("done\n"); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 201 | ret = 0; |
| 202 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 203 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 204 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; |
| 205 | #endif |
| 206 | |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 207 | fini: |
| 208 | fini_mmc_for_env(mmc); |
| 209 | return ret; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 210 | } |
| 211 | #endif /* CONFIG_CMD_SAVEENV */ |
| 212 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 213 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 214 | unsigned long offset, const void *buffer) |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 215 | { |
| 216 | uint blk_start, blk_cnt, n; |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 217 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 218 | |
Igor Grinberg | ecbfb62 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 219 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 220 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 221 | |
Simon Glass | da89892 | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 222 | n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 223 | |
| 224 | return (n == blk_cnt) ? 0 : -1; |
| 225 | } |
| 226 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 227 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 228 | void env_relocate_spec(void) |
| 229 | { |
| 230 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 231 | struct mmc *mmc; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 232 | u32 offset1, offset2; |
| 233 | int read1_fail = 0, read2_fail = 0; |
| 234 | int crc1_ok = 0, crc2_ok = 0; |
Markus Niebel | 6ef41fd | 2013-10-04 15:48:03 +0200 | [diff] [blame] | 235 | env_t *ep; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 236 | int ret; |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 237 | int dev = mmc_get_env_dev(); |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 238 | const char *errmsg = NULL; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 239 | |
Markus Niebel | 6ef41fd | 2013-10-04 15:48:03 +0200 | [diff] [blame] | 240 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1); |
| 241 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1); |
| 242 | |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 243 | mmc = find_mmc_device(dev); |
| 244 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 245 | errmsg = init_mmc_for_env(mmc); |
| 246 | if (errmsg) { |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 247 | ret = 1; |
| 248 | goto err; |
| 249 | } |
| 250 | |
| 251 | if (mmc_get_env_addr(mmc, 0, &offset1) || |
| 252 | mmc_get_env_addr(mmc, 1, &offset2)) { |
| 253 | ret = 1; |
| 254 | goto fini; |
| 255 | } |
| 256 | |
| 257 | read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1); |
| 258 | read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); |
| 259 | |
| 260 | if (read1_fail && read2_fail) |
| 261 | puts("*** Error - No Valid Environment Area found\n"); |
| 262 | else if (read1_fail || read2_fail) |
| 263 | puts("*** Warning - some problems detected " |
| 264 | "reading environment; recovered successfully\n"); |
| 265 | |
| 266 | crc1_ok = !read1_fail && |
| 267 | (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); |
| 268 | crc2_ok = !read2_fail && |
| 269 | (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
| 270 | |
| 271 | if (!crc1_ok && !crc2_ok) { |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 272 | errmsg = "!bad CRC"; |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 273 | ret = 1; |
| 274 | goto fini; |
| 275 | } else if (crc1_ok && !crc2_ok) { |
| 276 | gd->env_valid = 1; |
| 277 | } else if (!crc1_ok && crc2_ok) { |
| 278 | gd->env_valid = 2; |
| 279 | } else { |
| 280 | /* both ok - check serial */ |
| 281 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
| 282 | gd->env_valid = 2; |
| 283 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
| 284 | gd->env_valid = 1; |
| 285 | else if (tmp_env1->flags > tmp_env2->flags) |
| 286 | gd->env_valid = 1; |
| 287 | else if (tmp_env2->flags > tmp_env1->flags) |
| 288 | gd->env_valid = 2; |
| 289 | else /* flags are equal - almost impossible */ |
| 290 | gd->env_valid = 1; |
| 291 | } |
| 292 | |
| 293 | free(env_ptr); |
| 294 | |
| 295 | if (gd->env_valid == 1) |
| 296 | ep = tmp_env1; |
| 297 | else |
| 298 | ep = tmp_env2; |
| 299 | |
| 300 | env_flags = ep->flags; |
| 301 | env_import((char *)ep, 0); |
| 302 | ret = 0; |
| 303 | |
| 304 | fini: |
| 305 | fini_mmc_for_env(mmc); |
| 306 | err: |
| 307 | if (ret) |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 308 | set_default_env(errmsg); |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 309 | #endif |
| 310 | } |
| 311 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 312 | void env_relocate_spec(void) |
| 313 | { |
| 314 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 315 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 316 | struct mmc *mmc; |
Mingkai Hu | a55fce3 | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 317 | u32 offset; |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 318 | int ret; |
Clemens Gruber | f7b6144 | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 319 | int dev = mmc_get_env_dev(); |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 320 | const char *errmsg; |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 321 | |
Tom Rini | c929f82 | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 322 | mmc = find_mmc_device(dev); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 323 | |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 324 | errmsg = init_mmc_for_env(mmc); |
| 325 | if (errmsg) { |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 326 | ret = 1; |
| 327 | goto err; |
| 328 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 329 | |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 330 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 331 | ret = 1; |
| 332 | goto fini; |
| 333 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 334 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 335 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 336 | errmsg = "!read failed"; |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 337 | ret = 1; |
| 338 | goto fini; |
| 339 | } |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 340 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 341 | env_import(buf, 1); |
Stephen Warren | 27ee0f3 | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 342 | ret = 0; |
| 343 | |
| 344 | fini: |
| 345 | fini_mmc_for_env(mmc); |
| 346 | err: |
| 347 | if (ret) |
Tim Harvey | eda75c3 | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 348 | set_default_env(errmsg); |
Terry Lv | b4eceac | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 349 | #endif |
| 350 | } |
Michael Heimpold | a1d25a3 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 351 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |