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