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> |
Philipp Tomsich | f8b8a55 | 2017-05-16 00:16:31 +0200 | [diff] [blame] | 13 | #include <fdtdec.h> |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 14 | #include <linux/stddef.h> |
| 15 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 16 | #include <memalign.h> |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 17 | #include <mmc.h> |
Lei Wen | 6d1d51b | 2010-11-10 07:39:23 +0800 | [diff] [blame] | 18 | #include <search.h> |
Lei Wen | e79f483 | 2010-10-13 11:07:21 +0800 | [diff] [blame] | 19 | #include <errno.h> |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 20 | |
Michael Heimpold | d196bd8 | 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 | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 26 | #ifdef ENV_IS_EMBEDDED |
Igor Grinberg | 994bc67 | 2011-11-17 06:07:23 +0000 | [diff] [blame] | 27 | env_t *env_ptr = &environment; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 28 | #else /* ! ENV_IS_EMBEDDED */ |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 29 | env_t *env_ptr; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 30 | #endif /* ENV_IS_EMBEDDED */ |
| 31 | |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 34 | #if !defined(CONFIG_ENV_OFFSET) |
| 35 | #define CONFIG_ENV_OFFSET 0 |
| 36 | #endif |
| 37 | |
Philipp Tomsich | f8b8a55 | 2017-05-16 00:16:31 +0200 | [diff] [blame] | 38 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 39 | static inline s64 mmc_offset(int copy) |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 40 | { |
Philipp Tomsich | f8b8a55 | 2017-05-16 00:16:31 +0200 | [diff] [blame] | 41 | const char *propname = "u-boot,mmc-env-offset"; |
| 42 | s64 defvalue = CONFIG_ENV_OFFSET; |
Stephen Warren | 5c088ee | 2013-06-11 15:14:02 -0600 | [diff] [blame] | 43 | |
Philipp Tomsich | f8b8a55 | 2017-05-16 00:16:31 +0200 | [diff] [blame] | 44 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
| 45 | if (copy) { |
| 46 | propname = "u-boot,mmc-env-offset-redundant"; |
| 47 | defvalue = CONFIG_ENV_OFFSET_REDUND; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue); |
| 52 | } |
| 53 | #else |
| 54 | static inline s64 mmc_offset(int copy) |
| 55 | { |
| 56 | s64 offset = CONFIG_ENV_OFFSET; |
| 57 | |
| 58 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 59 | if (copy) |
Stephen Warren | 5c088ee | 2013-06-11 15:14:02 -0600 | [diff] [blame] | 60 | offset = CONFIG_ENV_OFFSET_REDUND; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 61 | #endif |
Philipp Tomsich | f8b8a55 | 2017-05-16 00:16:31 +0200 | [diff] [blame] | 62 | return offset; |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) |
| 67 | { |
| 68 | s64 offset = mmc_offset(copy); |
Stephen Warren | 5c088ee | 2013-06-11 15:14:02 -0600 | [diff] [blame] | 69 | |
| 70 | if (offset < 0) |
| 71 | offset += mmc->capacity; |
| 72 | |
| 73 | *env_addr = offset; |
| 74 | |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 77 | |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 78 | __weak int mmc_get_env_dev(void) |
| 79 | { |
| 80 | return CONFIG_SYS_MMC_ENV_DEV; |
| 81 | } |
| 82 | |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 83 | #ifdef CONFIG_SYS_MMC_ENV_PART |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 84 | __weak uint mmc_get_env_part(struct mmc *mmc) |
| 85 | { |
| 86 | return CONFIG_SYS_MMC_ENV_PART; |
| 87 | } |
| 88 | |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 89 | static unsigned char env_mmc_orig_hwpart; |
| 90 | |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 91 | static int mmc_set_env_part(struct mmc *mmc) |
| 92 | { |
| 93 | uint part = mmc_get_env_part(mmc); |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 94 | int dev = mmc_get_env_dev(); |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 95 | int ret = 0; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 96 | |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 97 | env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart; |
| 98 | ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part); |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 99 | if (ret) |
| 100 | puts("MMC partition switch failed\n"); |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | #else |
| 105 | static inline int mmc_set_env_part(struct mmc *mmc) {return 0; }; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 106 | #endif |
| 107 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 108 | static const char *init_mmc_for_env(struct mmc *mmc) |
Dmitry Lifshitz | 6e7b7df | 2014-07-30 13:19:06 +0300 | [diff] [blame] | 109 | { |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 110 | if (!mmc) |
Hans de Goede | a85da21 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 111 | return "!No MMC card found"; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 112 | |
Simon Glass | 01b73fe | 2017-05-27 11:37:18 -0600 | [diff] [blame] | 113 | #ifdef CONFIG_BLK |
| 114 | struct udevice *dev; |
| 115 | |
| 116 | if (blk_get_from_parent(mmc->dev, &dev)) |
| 117 | return "!No block device"; |
| 118 | #else |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 119 | if (mmc_init(mmc)) |
Hans de Goede | a85da21 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 120 | return "!MMC init failed"; |
Simon Glass | e7017a3 | 2017-04-23 20:02:04 -0600 | [diff] [blame] | 121 | #endif |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 122 | if (mmc_set_env_part(mmc)) |
Hans de Goede | a85da21 | 2015-08-15 20:05:01 +0200 | [diff] [blame] | 123 | return "!MMC partition switch failed"; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 124 | |
| 125 | return NULL; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 128 | static void fini_mmc_for_env(struct mmc *mmc) |
| 129 | { |
| 130 | #ifdef CONFIG_SYS_MMC_ENV_PART |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 131 | int dev = mmc_get_env_dev(); |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 132 | |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 133 | blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 134 | #endif |
| 135 | } |
| 136 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 137 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD) |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 138 | static inline int write_env(struct mmc *mmc, unsigned long size, |
| 139 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 140 | { |
| 141 | uint blk_start, blk_cnt, n; |
Simon Glass | 5461acb | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 142 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 143 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 144 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
| 145 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 146 | |
Simon Glass | 5461acb | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 147 | n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 148 | |
| 149 | return (n == blk_cnt) ? 0 : -1; |
| 150 | } |
| 151 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 152 | static int env_mmc_save(void) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 153 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 154 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 155 | int dev = mmc_get_env_dev(); |
| 156 | struct mmc *mmc = find_mmc_device(dev); |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 157 | u32 offset; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 158 | int ret, copy = 0; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 159 | const char *errmsg; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 160 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 161 | errmsg = init_mmc_for_env(mmc); |
| 162 | if (errmsg) { |
| 163 | printf("%s\n", errmsg); |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 164 | return 1; |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 165 | } |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 166 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 167 | ret = env_export(env_new); |
| 168 | if (ret) |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 169 | goto fini; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 170 | |
| 171 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 172 | if (gd->env_valid == ENV_VALID) |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 173 | copy = 1; |
| 174 | #endif |
| 175 | |
| 176 | if (mmc_get_env_addr(mmc, copy, &offset)) { |
| 177 | ret = 1; |
| 178 | goto fini; |
| 179 | } |
| 180 | |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 181 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev); |
Stephen Warren | 4036b63 | 2012-05-24 11:38:33 +0000 | [diff] [blame] | 182 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 183 | puts("failed\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 184 | ret = 1; |
| 185 | goto fini; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | puts("done\n"); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 189 | ret = 0; |
| 190 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 191 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 192 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 193 | #endif |
| 194 | |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 195 | fini: |
| 196 | fini_mmc_for_env(mmc); |
| 197 | return ret; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 198 | } |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 199 | #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */ |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 200 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 201 | static inline int read_env(struct mmc *mmc, unsigned long size, |
| 202 | unsigned long offset, const void *buffer) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 203 | { |
| 204 | uint blk_start, blk_cnt, n; |
Simon Glass | 5461acb | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 205 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 206 | |
Igor Grinberg | e8db8f7 | 2011-11-07 01:14:05 +0000 | [diff] [blame] | 207 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 208 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 209 | |
Simon Glass | 5461acb | 2016-05-14 14:03:03 -0600 | [diff] [blame] | 210 | n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 211 | |
| 212 | return (n == blk_cnt) ? 0 : -1; |
| 213 | } |
| 214 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 215 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 216 | static void env_mmc_load(void) |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 217 | { |
| 218 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 219 | struct mmc *mmc; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 220 | u32 offset1, offset2; |
| 221 | int read1_fail = 0, read2_fail = 0; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 222 | int ret; |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 223 | int dev = mmc_get_env_dev(); |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 224 | const char *errmsg = NULL; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 225 | |
Markus Niebel | 452a272 | 2013-10-04 15:48:03 +0200 | [diff] [blame] | 226 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1); |
| 227 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1); |
| 228 | |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 229 | mmc = find_mmc_device(dev); |
| 230 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 231 | errmsg = init_mmc_for_env(mmc); |
| 232 | if (errmsg) { |
Michael Heimpold | d196bd8 | 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 | |
Fiach Antaw | 9d364af | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 252 | if (read1_fail && read2_fail) { |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 253 | errmsg = "!bad CRC"; |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 254 | ret = 1; |
| 255 | goto fini; |
Fiach Antaw | 9d364af | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 256 | } else if (!read1_fail && read2_fail) { |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 257 | gd->env_valid = ENV_VALID; |
Fiach Antaw | 9d364af | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 258 | env_import((char *)tmp_env1, 1); |
| 259 | } else if (read1_fail && !read2_fail) { |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 260 | gd->env_valid = ENV_REDUND; |
Fiach Antaw | 9d364af | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 261 | env_import((char *)tmp_env2, 1); |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 262 | } else { |
Fiach Antaw | 9d364af | 2017-01-25 18:53:12 +1000 | [diff] [blame] | 263 | env_import_redund((char *)tmp_env1, (char *)tmp_env2); |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 266 | ret = 0; |
| 267 | |
| 268 | fini: |
| 269 | fini_mmc_for_env(mmc); |
| 270 | err: |
| 271 | if (ret) |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 272 | set_default_env(errmsg); |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 273 | #endif |
| 274 | } |
| 275 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 276 | static void env_mmc_load(void) |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 277 | { |
| 278 | #if !defined(ENV_IS_EMBEDDED) |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 279 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 280 | struct mmc *mmc; |
Mingkai Hu | 97039ab | 2011-01-24 17:09:55 +0000 | [diff] [blame] | 281 | u32 offset; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 282 | int ret; |
Clemens Gruber | e92029c | 2016-01-20 15:43:37 +0100 | [diff] [blame] | 283 | int dev = mmc_get_env_dev(); |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 284 | const char *errmsg; |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 285 | |
Tom Rini | b9c8cca | 2014-03-28 12:03:34 -0400 | [diff] [blame] | 286 | mmc = find_mmc_device(dev); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 287 | |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 288 | errmsg = init_mmc_for_env(mmc); |
| 289 | if (errmsg) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 290 | ret = 1; |
| 291 | goto err; |
| 292 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 293 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 294 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 295 | ret = 1; |
| 296 | goto fini; |
| 297 | } |
| 298 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 299 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 300 | errmsg = "!read failed"; |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 301 | ret = 1; |
| 302 | goto fini; |
| 303 | } |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 304 | |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 305 | env_import(buf, 1); |
Stephen Warren | 9404a5f | 2012-07-30 10:55:44 +0000 | [diff] [blame] | 306 | ret = 0; |
| 307 | |
| 308 | fini: |
| 309 | fini_mmc_for_env(mmc); |
| 310 | err: |
| 311 | if (ret) |
Tim Harvey | c75648d | 2015-05-08 14:52:09 -0700 | [diff] [blame] | 312 | set_default_env(errmsg); |
Terry Lv | a806035 | 2010-05-17 10:57:01 +0800 | [diff] [blame] | 313 | #endif |
| 314 | } |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 315 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 316 | |
| 317 | U_BOOT_ENV_LOCATION(mmc) = { |
| 318 | .location = ENVL_MMC, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame^] | 319 | ENV_NAME("MMC") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 320 | .load = env_mmc_load, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 321 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 322 | .save = env_save_ptr(env_mmc_save), |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 323 | #endif |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 324 | }; |