blob: 7a5836ad66c693e300b71e0bc13d502a71e4cd76 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Terry Lva8060352010-05-17 10:57:01 +08002/*
Mingkai Hu97039ab2011-01-24 17:09:55 +00003 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
Terry Lva8060352010-05-17 10:57:01 +08004 */
5
6/* #define DEBUG */
7
8#include <common.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Terry Lva8060352010-05-17 10:57:01 +080010
11#include <command.h>
Simon Glass0ac7d722019-08-01 09:47:00 -060012#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060013#include <env_internal.h>
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020014#include <fdtdec.h>
Terry Lva8060352010-05-17 10:57:01 +080015#include <linux/stddef.h>
16#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060017#include <memalign.h>
Terry Lva8060352010-05-17 10:57:01 +080018#include <mmc.h>
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010019#include <part.h>
Lei Wen6d1d51b2010-11-10 07:39:23 +080020#include <search.h>
Lei Wene79f4832010-10-13 11:07:21 +080021#include <errno.h>
Simon Glass7de8bd02021-08-07 07:24:01 -060022#include <dm/ofnode.h>
Terry Lva8060352010-05-17 10:57:01 +080023
Patrick Delaunayf7e07a72022-11-10 11:48:58 +010024#define ENV_MMC_INVALID_OFFSET ((s64)-1)
25
Patrick Delaunayd2103e22022-11-10 11:49:01 +010026#if defined(CONFIG_ENV_MMC_USE_DT)
27/* ENV offset is invalid when not defined in Device Tree */
28#define ENV_MMC_OFFSET ENV_MMC_INVALID_OFFSET
29#define ENV_MMC_OFFSET_REDUND ENV_MMC_INVALID_OFFSET
30
31#else
Patrick Delaunayf7e07a72022-11-10 11:48:58 +010032/* Default ENV offset when not defined in Device Tree */
33#define ENV_MMC_OFFSET CONFIG_ENV_OFFSET
34
35#if defined(CONFIG_ENV_OFFSET_REDUND)
36#define ENV_MMC_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
37#else
38#define ENV_MMC_OFFSET_REDUND ENV_MMC_INVALID_OFFSET
39#endif
Patrick Delaunayd2103e22022-11-10 11:49:01 +010040#endif
Patrick Delaunayf7e07a72022-11-10 11:48:58 +010041
Terry Lva8060352010-05-17 10:57:01 +080042DECLARE_GLOBAL_DATA_PTR;
43
Marek Vasutd11d1be2021-10-17 19:23:36 +020044/*
45 * In case the environment is redundant, stored in eMMC hardware boot
46 * partition and the environment and redundant environment offsets are
47 * identical, store the environment and redundant environment in both
48 * eMMC boot partitions, one copy in each.
49 * */
50#if (defined(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && \
51 (CONFIG_SYS_MMC_ENV_PART == 1) && \
52 (CONFIG_ENV_OFFSET == CONFIG_ENV_OFFSET_REDUND))
Patrick Delaunay46c90162022-11-10 11:48:59 +010053#define ENV_MMC_HWPART_REDUND 1
Marek Vasutd11d1be2021-10-17 19:23:36 +020054#endif
55
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020056#if CONFIG_IS_ENABLED(OF_CONTROL)
Patrick Delaunay5d4f7b42020-06-15 10:38:57 +020057static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010058{
Simon Glass05289792020-05-10 11:39:57 -060059 struct disk_partition info;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010060 struct blk_desc *desc;
61 int len, i, ret;
Patrick Delaunay2b2f7272020-06-15 10:38:55 +020062 char dev_str[4];
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010063
Patrick Delaunay2b2f7272020-06-15 10:38:55 +020064 snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev());
65 ret = blk_get_device_by_str("mmc", dev_str, &desc);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010066 if (ret < 0)
67 return (ret);
68
69 for (i = 1;;i++) {
70 ret = part_get_info(desc, i, &info);
71 if (ret < 0)
72 return ret;
73
Patrick Delaunay80105d82022-11-10 11:49:03 +010074 if (str && !strncmp((const char *)info.name, str, sizeof(info.name)))
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010075 break;
Patrick Delaunay80105d82022-11-10 11:49:03 +010076#ifdef CONFIG_PARTITION_TYPE_GUID
77 if (!str) {
78 const efi_guid_t env_guid = PARTITION_U_BOOT_ENVIRONMENT;
79 efi_guid_t type_guid;
80
81 uuid_str_to_bin(info.type_guid, type_guid.b, UUID_STR_FORMAT_GUID);
82 if (!memcmp(&env_guid, &type_guid, sizeof(efi_guid_t)))
83 break;
84 }
85#endif
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010086 }
87
88 /* round up to info.blksz */
Patrick Delaunay76b640c2020-06-15 10:38:56 +020089 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010090
91 /* use the top of the partion for the environment */
Patrick Delaunay5d4f7b42020-06-15 10:38:57 +020092 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010093
94 return 0;
95}
96
Marek Vasut5b4acb02023-02-09 13:30:10 +010097static inline s64 mmc_offset(struct mmc *mmc, int copy)
Mingkai Hu97039ab2011-01-24 17:09:55 +000098{
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010099 const struct {
100 const char *offset_redund;
101 const char *partition;
102 const char *offset;
103 } dt_prop = {
104 .offset_redund = "u-boot,mmc-env-offset-redundant",
105 .partition = "u-boot,mmc-env-partition",
106 .offset = "u-boot,mmc-env-offset",
107 };
Philipp Tomsichfd374662017-11-21 23:29:40 +0100108 s64 val = 0, defvalue;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100109 const char *propname;
110 const char *str;
Marek Vasut5b4acb02023-02-09 13:30:10 +0100111 int hwpart = 0;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100112 int err;
113
Marek Vasut5b4acb02023-02-09 13:30:10 +0100114 if (IS_ENABLED(CONFIG_SYS_MMC_ENV_PART))
115 hwpart = mmc_get_env_part(mmc);
116
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100117 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
Simon Glass7de8bd02021-08-07 07:24:01 -0600118 str = ofnode_conf_read_str(dt_prop.partition);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100119 if (str) {
120 /* try to place the environment at end of the partition */
Patrick Delaunay5d4f7b42020-06-15 10:38:57 +0200121 err = mmc_offset_try_partition(str, copy, &val);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100122 if (!err)
123 return val;
Patrick Delaunay52e9aa32022-11-10 11:49:04 +0100124 debug("env partition '%s' not found (%d)", str, err);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100125 }
126
Patrick Delaunay80105d82022-11-10 11:49:03 +0100127 /* try the GPT partition with "U-Boot ENV" TYPE GUID */
Marek Vasut5b4acb02023-02-09 13:30:10 +0100128 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID) && hwpart == 0) {
Patrick Delaunay80105d82022-11-10 11:49:03 +0100129 err = mmc_offset_try_partition(NULL, copy, &val);
130 if (!err)
131 return val;
132 }
133
Patrick Delaunayf7e07a72022-11-10 11:48:58 +0100134 defvalue = ENV_MMC_OFFSET;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100135 propname = dt_prop.offset;
Stephen Warren5c088ee2013-06-11 15:14:02 -0600136
Patrick Delaunay46c90162022-11-10 11:48:59 +0100137 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && copy) {
Patrick Delaunayf7e07a72022-11-10 11:48:58 +0100138 defvalue = ENV_MMC_OFFSET_REDUND;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100139 propname = dt_prop.offset_redund;
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200140 }
Patrick Delaunay46c90162022-11-10 11:48:59 +0100141
Simon Glass7de8bd02021-08-07 07:24:01 -0600142 return ofnode_conf_read_int(propname, defvalue);
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200143}
144#else
Marek Vasut5b4acb02023-02-09 13:30:10 +0100145static inline s64 mmc_offset(struct mmc *mmc, int copy)
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200146{
Patrick Delaunayf7e07a72022-11-10 11:48:58 +0100147 s64 offset = ENV_MMC_OFFSET;
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200148
Patrick Delaunay46c90162022-11-10 11:48:59 +0100149 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && copy)
Patrick Delaunayf7e07a72022-11-10 11:48:58 +0100150 offset = ENV_MMC_OFFSET_REDUND;
Patrick Delaunay46c90162022-11-10 11:48:59 +0100151
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200152 return offset;
153}
154#endif
155
156__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
157{
Marek Vasut5b4acb02023-02-09 13:30:10 +0100158 s64 offset = mmc_offset(mmc, copy);
Stephen Warren5c088ee2013-06-11 15:14:02 -0600159
Patrick Delaunayf7e07a72022-11-10 11:48:58 +0100160 if (offset == ENV_MMC_INVALID_OFFSET) {
161 printf("Invalid ENV offset in MMC, copy=%d\n", copy);
162 return -ENOENT;
163 }
164
Stephen Warren5c088ee2013-06-11 15:14:02 -0600165 if (offset < 0)
166 offset += mmc->capacity;
167
168 *env_addr = offset;
169
Mingkai Hu97039ab2011-01-24 17:09:55 +0000170 return 0;
171}
Mingkai Hu97039ab2011-01-24 17:09:55 +0000172
Tom Rinib9c8cca2014-03-28 12:03:34 -0400173#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300174__weak uint mmc_get_env_part(struct mmc *mmc)
175{
176 return CONFIG_SYS_MMC_ENV_PART;
177}
178
Stephen Warren873cc1d2015-12-07 11:38:49 -0700179static unsigned char env_mmc_orig_hwpart;
180
Marek Vasutd11d1be2021-10-17 19:23:36 +0200181static int mmc_set_env_part(struct mmc *mmc, uint part)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300182{
Clemens Grubere92029c2016-01-20 15:43:37 +0100183 int dev = mmc_get_env_dev();
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300184 int ret = 0;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400185
Simon Glasse33a5c62022-08-11 19:34:59 -0600186 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren873cc1d2015-12-07 11:38:49 -0700187 if (ret)
188 puts("MMC partition switch failed\n");
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300189
190 return ret;
191}
Patrick Delaunay46c90162022-11-10 11:48:59 +0100192
193static bool mmc_set_env_part_init(struct mmc *mmc)
194{
195 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
196 if (mmc_set_env_part(mmc, mmc_get_env_part(mmc)))
197 return false;
198
199 return true;
200}
201
202static int mmc_set_env_part_restore(struct mmc *mmc)
203{
204 return mmc_set_env_part(mmc, env_mmc_orig_hwpart);
205}
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300206#else
Marek Vasutd11d1be2021-10-17 19:23:36 +0200207static inline int mmc_set_env_part(struct mmc *mmc, uint part) {return 0; };
Patrick Delaunay46c90162022-11-10 11:48:59 +0100208static bool mmc_set_env_part_init(struct mmc *mmc) {return true; }
209static inline int mmc_set_env_part_restore(struct mmc *mmc) {return 0; };
Tom Rinib9c8cca2014-03-28 12:03:34 -0400210#endif
211
Tim Harveyc75648d2015-05-08 14:52:09 -0700212static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300213{
Tim Harveyc75648d2015-05-08 14:52:09 -0700214 if (!mmc)
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300215 return "No MMC card found";
Terry Lva8060352010-05-17 10:57:01 +0800216
Sjoerd Simonsd48b8d12018-03-22 22:53:50 +0100217#if CONFIG_IS_ENABLED(BLK)
Simon Glass01b73fe2017-05-27 11:37:18 -0600218 struct udevice *dev;
219
220 if (blk_get_from_parent(mmc->dev, &dev))
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300221 return "No block device";
Simon Glass01b73fe2017-05-27 11:37:18 -0600222#else
Tim Harveyc75648d2015-05-08 14:52:09 -0700223 if (mmc_init(mmc))
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300224 return "MMC init failed";
Simon Glasse7017a32017-04-23 20:02:04 -0600225#endif
Patrick Delaunay46c90162022-11-10 11:48:59 +0100226 if (!mmc_set_env_part_init(mmc))
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300227 return "MMC partition switch failed";
Tim Harveyc75648d2015-05-08 14:52:09 -0700228
229 return NULL;
Terry Lva8060352010-05-17 10:57:01 +0800230}
231
Stephen Warren9404a5f2012-07-30 10:55:44 +0000232static void fini_mmc_for_env(struct mmc *mmc)
233{
Patrick Delaunay46c90162022-11-10 11:48:59 +0100234 mmc_set_env_part_restore(mmc);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000235}
236
Simon Glasse5bce242017-08-03 12:22:01 -0600237#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
Igor Grinberge8db8f72011-11-07 01:14:05 +0000238static inline int write_env(struct mmc *mmc, unsigned long size,
239 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800240{
241 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600242 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800243
Igor Grinberge8db8f72011-11-07 01:14:05 +0000244 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
245 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800246
Simon Glass5461acb2016-05-14 14:03:03 -0600247 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800248
249 return (n == blk_cnt) ? 0 : -1;
250}
251
Simon Glasse5bce242017-08-03 12:22:01 -0600252static int env_mmc_save(void)
Terry Lva8060352010-05-17 10:57:01 +0800253{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400254 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Grubere92029c2016-01-20 15:43:37 +0100255 int dev = mmc_get_env_dev();
256 struct mmc *mmc = find_mmc_device(dev);
Igor Grinberge8db8f72011-11-07 01:14:05 +0000257 u32 offset;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000258 int ret, copy = 0;
Tim Harveyc75648d2015-05-08 14:52:09 -0700259 const char *errmsg;
Terry Lva8060352010-05-17 10:57:01 +0800260
Tim Harveyc75648d2015-05-08 14:52:09 -0700261 errmsg = init_mmc_for_env(mmc);
262 if (errmsg) {
263 printf("%s\n", errmsg);
Mingkai Hu97039ab2011-01-24 17:09:55 +0000264 return 1;
Tim Harveyc75648d2015-05-08 14:52:09 -0700265 }
Mingkai Hu97039ab2011-01-24 17:09:55 +0000266
Marek Vasut7ce15262014-03-05 19:59:50 +0100267 ret = env_export(env_new);
268 if (ret)
Stephen Warren9404a5f2012-07-30 10:55:44 +0000269 goto fini;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000270
Patrick Delaunay46c90162022-11-10 11:48:59 +0100271 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
272 if (gd->env_valid == ENV_VALID)
273 copy = 1;
Marek Vasutd11d1be2021-10-17 19:23:36 +0200274
Patrick Delaunay46c90162022-11-10 11:48:59 +0100275 if (IS_ENABLED(ENV_MMC_HWPART_REDUND)) {
276 ret = mmc_set_env_part(mmc, copy + 1);
277 if (ret)
278 goto fini;
279 }
Ye Liccd05422023-01-31 14:41:58 +0800280 }
Marek Vasutd11d1be2021-10-17 19:23:36 +0200281
Ye Liccd05422023-01-31 14:41:58 +0800282 if (mmc_get_env_addr(mmc, copy, &offset)) {
283 ret = 1;
284 goto fini;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000285 }
286
Clemens Grubere92029c2016-01-20 15:43:37 +0100287 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren4036b632012-05-24 11:38:33 +0000288 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800289 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000290 ret = 1;
291 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800292 }
293
Stephen Warren9404a5f2012-07-30 10:55:44 +0000294 ret = 0;
295
Patrick Delaunay46c90162022-11-10 11:48:59 +0100296 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
297 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000298
Stephen Warren9404a5f2012-07-30 10:55:44 +0000299fini:
300 fini_mmc_for_env(mmc);
Patrick Delaunay46c90162022-11-10 11:48:59 +0100301
Stephen Warren9404a5f2012-07-30 10:55:44 +0000302 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800303}
Frank Wunderlich34853922019-06-29 11:36:20 +0200304
Frank Wunderlich34853922019-06-29 11:36:20 +0200305static inline int erase_env(struct mmc *mmc, unsigned long size,
306 unsigned long offset)
307{
308 uint blk_start, blk_cnt, n;
309 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Patrick Delaunayd7226702022-02-15 16:23:23 +0100310 u32 erase_size;
Frank Wunderlich34853922019-06-29 11:36:20 +0200311
Patrick Delaunayd7226702022-02-15 16:23:23 +0100312 erase_size = mmc->erase_grp_size * desc->blksz;
313 blk_start = ALIGN_DOWN(offset, erase_size) / desc->blksz;
314 blk_cnt = ALIGN(size, erase_size) / desc->blksz;
Frank Wunderlich34853922019-06-29 11:36:20 +0200315
316 n = blk_derase(desc, blk_start, blk_cnt);
Patrick Delaunayd7226702022-02-15 16:23:23 +0100317 printf("%d blocks erased at 0x%x: %s\n", n, blk_start,
318 (n == blk_cnt) ? "OK" : "ERROR");
Frank Wunderlich34853922019-06-29 11:36:20 +0200319
320 return (n == blk_cnt) ? 0 : 1;
321}
322
323static int env_mmc_erase(void)
324{
325 int dev = mmc_get_env_dev();
326 struct mmc *mmc = find_mmc_device(dev);
327 int ret, copy = 0;
328 u32 offset;
329 const char *errmsg;
330
331 errmsg = init_mmc_for_env(mmc);
332 if (errmsg) {
333 printf("%s\n", errmsg);
334 return 1;
335 }
336
Marek Vasutf47f87f2021-10-06 18:29:53 +0200337 if (mmc_get_env_addr(mmc, copy, &offset)) {
338 ret = CMD_RET_FAILURE;
339 goto fini;
340 }
Frank Wunderlich34853922019-06-29 11:36:20 +0200341
Patrick Delaunayd7226702022-02-15 16:23:23 +0100342 printf("\n");
Frank Wunderlich34853922019-06-29 11:36:20 +0200343 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
344
Patrick Delaunay46c90162022-11-10 11:48:59 +0100345 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
346 copy = 1;
Frank Wunderlich34853922019-06-29 11:36:20 +0200347
Patrick Delaunay46c90162022-11-10 11:48:59 +0100348 if (IS_ENABLED(ENV_MMC_HWPART_REDUND)) {
349 ret = mmc_set_env_part(mmc, copy + 1);
350 if (ret)
351 goto fini;
352 }
Marek Vasutd11d1be2021-10-17 19:23:36 +0200353
Patrick Delaunay46c90162022-11-10 11:48:59 +0100354 if (mmc_get_env_addr(mmc, copy, &offset)) {
355 ret = CMD_RET_FAILURE;
356 goto fini;
357 }
358
359 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
Marek Vasutf47f87f2021-10-06 18:29:53 +0200360 }
Frank Wunderlich34853922019-06-29 11:36:20 +0200361
Marek Vasutf47f87f2021-10-06 18:29:53 +0200362fini:
363 fini_mmc_for_env(mmc);
Frank Wunderlich34853922019-06-29 11:36:20 +0200364 return ret;
365}
Simon Glasse5bce242017-08-03 12:22:01 -0600366#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lva8060352010-05-17 10:57:01 +0800367
Igor Grinberge8db8f72011-11-07 01:14:05 +0000368static inline int read_env(struct mmc *mmc, unsigned long size,
369 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800370{
371 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600372 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800373
Igor Grinberge8db8f72011-11-07 01:14:05 +0000374 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
375 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800376
Simon Glass5461acb2016-05-14 14:03:03 -0600377 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800378
379 return (n == blk_cnt) ? 0 : -1;
380}
381
Patrick Delaunay85660502022-11-10 11:49:00 +0100382#if defined(ENV_IS_EMBEDDED)
Simon Glassc5951992017-08-03 12:22:17 -0600383static int env_mmc_load(void)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000384{
Patrick Delaunay85660502022-11-10 11:49:00 +0100385 return 0;
386}
387#elif defined(CONFIG_SYS_REDUNDAND_ENVIRONMENT)
388static int env_mmc_load(void)
389{
Tom Rinib9c8cca2014-03-28 12:03:34 -0400390 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000391 u32 offset1, offset2;
392 int read1_fail = 0, read2_fail = 0;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000393 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100394 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700395 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000396
Markus Niebel452a2722013-10-04 15:48:03 +0200397 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
398 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
399
Faiz Abbas26862b42018-02-12 19:24:31 +0530400 mmc_initialize(NULL);
401
Tom Rinib9c8cca2014-03-28 12:03:34 -0400402 mmc = find_mmc_device(dev);
403
Tim Harveyc75648d2015-05-08 14:52:09 -0700404 errmsg = init_mmc_for_env(mmc);
405 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600406 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000407 goto err;
408 }
409
410 if (mmc_get_env_addr(mmc, 0, &offset1) ||
411 mmc_get_env_addr(mmc, 1, &offset2)) {
Simon Glassc5951992017-08-03 12:22:17 -0600412 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000413 goto fini;
414 }
415
Patrick Delaunay46c90162022-11-10 11:48:59 +0100416 if (IS_ENABLED(ENV_MMC_HWPART_REDUND)) {
417 ret = mmc_set_env_part(mmc, 1);
418 if (ret)
419 goto fini;
420 }
Marek Vasutd11d1be2021-10-17 19:23:36 +0200421
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000422 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
Marek Vasutd11d1be2021-10-17 19:23:36 +0200423
Patrick Delaunay46c90162022-11-10 11:48:59 +0100424 if (IS_ENABLED(ENV_MMC_HWPART_REDUND)) {
425 ret = mmc_set_env_part(mmc, 2);
426 if (ret)
427 goto fini;
428 }
Marek Vasutd11d1be2021-10-17 19:23:36 +0200429
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000430 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
431
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100432 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasut890feec2020-07-07 20:51:35 +0200433 read2_fail, H_EXTERNAL);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000434
435fini:
436 fini_mmc_for_env(mmc);
437err:
438 if (ret)
Simon Glass0ac7d722019-08-01 09:47:00 -0600439 env_set_default(errmsg, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600440
Simon Glassc5951992017-08-03 12:22:17 -0600441 return ret;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000442}
Patrick Delaunay46c90162022-11-10 11:48:59 +0100443#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc5951992017-08-03 12:22:17 -0600444static int env_mmc_load(void)
Terry Lva8060352010-05-17 10:57:01 +0800445{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400446 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400447 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000448 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000449 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100450 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700451 const char *errmsg;
Pankit Garg0536b442019-11-19 09:49:31 +0000452 env_t *ep = NULL;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400453
Tom Rinib9c8cca2014-03-28 12:03:34 -0400454 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800455
Tim Harveyc75648d2015-05-08 14:52:09 -0700456 errmsg = init_mmc_for_env(mmc);
457 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600458 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000459 goto err;
460 }
Terry Lva8060352010-05-17 10:57:01 +0800461
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000462 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glassc5951992017-08-03 12:22:17 -0600463 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000464 goto fini;
465 }
466
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400467 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700468 errmsg = "!read failed";
Simon Glassc5951992017-08-03 12:22:17 -0600469 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000470 goto fini;
471 }
Terry Lva8060352010-05-17 10:57:01 +0800472
Marek Vasut890feec2020-07-07 20:51:35 +0200473 ret = env_import(buf, 1, H_EXTERNAL);
Pankit Garg0536b442019-11-19 09:49:31 +0000474 if (!ret) {
475 ep = (env_t *)buf;
476 gd->env_addr = (ulong)&ep->data;
477 }
Stephen Warren9404a5f2012-07-30 10:55:44 +0000478
479fini:
480 fini_mmc_for_env(mmc);
481err:
482 if (ret)
Simon Glass0ac7d722019-08-01 09:47:00 -0600483 env_set_default(errmsg, 0);
Patrick Delaunay85660502022-11-10 11:49:00 +0100484
Simon Glassc5951992017-08-03 12:22:17 -0600485 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800486}
Patrick Delaunay46c90162022-11-10 11:48:59 +0100487#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass4415f1d2017-08-03 12:21:58 -0600488
489U_BOOT_ENV_LOCATION(mmc) = {
490 .location = ENVL_MMC,
Simon Glassac358be2017-08-03 12:22:03 -0600491 ENV_NAME("MMC")
Simon Glasse5bce242017-08-03 12:22:01 -0600492 .load = env_mmc_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600493#ifndef CONFIG_SPL_BUILD
Simon Glasse5bce242017-08-03 12:22:01 -0600494 .save = env_save_ptr(env_mmc_save),
Patrick Delaunay1af031a2021-02-09 11:48:50 +0100495 .erase = ENV_ERASE_PTR(env_mmc_erase)
Simon Glass4415f1d2017-08-03 12:21:58 -0600496#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600497};