blob: 7a5836ad66c693e300b71e0bc13d502a71e4cd76 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Terry Lvb4eceac2010-05-17 10:57:01 +08002/*
Mingkai Hua55fce32011-01-24 17:09:55 +00003 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
Terry Lvb4eceac2010-05-17 10:57:01 +08004 */
5
6/* #define DEBUG */
7
8#include <common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080010
11#include <command.h>
Simon Glass97385862019-08-01 09:47:00 -060012#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060013#include <env_internal.h>
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020014#include <fdtdec.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080015#include <linux/stddef.h>
16#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060017#include <memalign.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080018#include <mmc.h>
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010019#include <part.h>
Lei Wen9f84f212010-11-10 07:39:23 +080020#include <search.h>
Lei Wenff833ed2010-10-13 11:07:21 +080021#include <errno.h>
Simon Glass0034d962021-08-07 07:24:01 -060022#include <dm/ofnode.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080023
Patrick Delaunayf080c4c2022-11-10 11:48:58 +010024#define ENV_MMC_INVALID_OFFSET ((s64)-1)
25
Patrick Delaunay41e143f2022-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 Delaunayf080c4c2022-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 Delaunay41e143f2022-11-10 11:49:01 +010040#endif
Patrick Delaunayf080c4c2022-11-10 11:48:58 +010041
Terry Lvb4eceac2010-05-17 10:57:01 +080042DECLARE_GLOBAL_DATA_PTR;
43
Marek Vasut509f36e2021-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 Delaunay036a3582022-11-10 11:48:59 +010053#define ENV_MMC_HWPART_REDUND 1
Marek Vasut509f36e2021-10-17 19:23:36 +020054#endif
55
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020056#if CONFIG_IS_ENABLED(OF_CONTROL)
Patrick Delaunayeff81952020-06-15 10:38:57 +020057static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010058{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060059 struct disk_partition info;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010060 struct blk_desc *desc;
61 int len, i, ret;
Patrick Delaunay530cb452020-06-15 10:38:55 +020062 char dev_str[4];
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010063
Patrick Delaunay530cb452020-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-Ortiz03ab46c2017-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 Delaunayeb787872022-11-10 11:49:03 +010074 if (str && !strncmp((const char *)info.name, str, sizeof(info.name)))
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010075 break;
Patrick Delaunayeb787872022-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-Ortiz03ab46c2017-11-06 14:16:37 +010086 }
87
88 /* round up to info.blksz */
Patrick Delaunay66fba3a2020-06-15 10:38:56 +020089 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010090
91 /* use the top of the partion for the environment */
Patrick Delaunayeff81952020-06-15 10:38:57 +020092 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010093
94 return 0;
95}
96
Marek Vasut1f2473a2023-02-09 13:30:10 +010097static inline s64 mmc_offset(struct mmc *mmc, int copy)
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020098{
Jorge Ramirez-Ortiz03ab46c2017-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 Tomsich745d8de2017-11-21 23:29:40 +0100108 s64 val = 0, defvalue;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100109 const char *propname;
110 const char *str;
Marek Vasut1f2473a2023-02-09 13:30:10 +0100111 int hwpart = 0;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100112 int err;
113
Marek Vasut1f2473a2023-02-09 13:30:10 +0100114 if (IS_ENABLED(CONFIG_SYS_MMC_ENV_PART))
115 hwpart = mmc_get_env_part(mmc);
116
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100117 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
Simon Glass0034d962021-08-07 07:24:01 -0600118 str = ofnode_conf_read_str(dt_prop.partition);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100119 if (str) {
120 /* try to place the environment at end of the partition */
Patrick Delaunayeff81952020-06-15 10:38:57 +0200121 err = mmc_offset_try_partition(str, copy, &val);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100122 if (!err)
123 return val;
Patrick Delaunay01b07982022-11-10 11:49:04 +0100124 debug("env partition '%s' not found (%d)", str, err);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100125 }
126
Patrick Delaunayeb787872022-11-10 11:49:03 +0100127 /* try the GPT partition with "U-Boot ENV" TYPE GUID */
Marek Vasut1f2473a2023-02-09 13:30:10 +0100128 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID) && hwpart == 0) {
Patrick Delaunayeb787872022-11-10 11:49:03 +0100129 err = mmc_offset_try_partition(NULL, copy, &val);
130 if (!err)
131 return val;
132 }
133
Patrick Delaunayf080c4c2022-11-10 11:48:58 +0100134 defvalue = ENV_MMC_OFFSET;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100135 propname = dt_prop.offset;
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200136
Patrick Delaunay036a3582022-11-10 11:48:59 +0100137 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && copy) {
Patrick Delaunayf080c4c2022-11-10 11:48:58 +0100138 defvalue = ENV_MMC_OFFSET_REDUND;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100139 propname = dt_prop.offset_redund;
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200140 }
Patrick Delaunay036a3582022-11-10 11:48:59 +0100141
Simon Glass0034d962021-08-07 07:24:01 -0600142 return ofnode_conf_read_int(propname, defvalue);
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200143}
144#else
Marek Vasut1f2473a2023-02-09 13:30:10 +0100145static inline s64 mmc_offset(struct mmc *mmc, int copy)
Mingkai Hua55fce32011-01-24 17:09:55 +0000146{
Patrick Delaunayf080c4c2022-11-10 11:48:58 +0100147 s64 offset = ENV_MMC_OFFSET;
Stephen Warren24dc2032013-06-11 15:14:02 -0600148
Patrick Delaunay036a3582022-11-10 11:48:59 +0100149 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && copy)
Patrick Delaunayf080c4c2022-11-10 11:48:58 +0100150 offset = ENV_MMC_OFFSET_REDUND;
Patrick Delaunay036a3582022-11-10 11:48:59 +0100151
Philipp Tomsich8ab507b2017-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 Vasut1f2473a2023-02-09 13:30:10 +0100158 s64 offset = mmc_offset(mmc, copy);
Stephen Warren24dc2032013-06-11 15:14:02 -0600159
Patrick Delaunayf080c4c2022-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 Warren24dc2032013-06-11 15:14:02 -0600165 if (offset < 0)
166 offset += mmc->capacity;
167
168 *env_addr = offset;
169
Mingkai Hua55fce32011-01-24 17:09:55 +0000170 return 0;
171}
Mingkai Hua55fce32011-01-24 17:09:55 +0000172
Tom Rinic929f822014-03-28 12:03:34 -0400173#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz97d2fb92014-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 Warren1e0f92a2015-12-07 11:38:49 -0700179static unsigned char env_mmc_orig_hwpart;
180
Marek Vasut509f36e2021-10-17 19:23:36 +0200181static int mmc_set_env_part(struct mmc *mmc, uint part)
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300182{
Clemens Gruberf7b61442016-01-20 15:43:37 +0100183 int dev = mmc_get_env_dev();
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300184 int ret = 0;
Tom Rinic929f822014-03-28 12:03:34 -0400185
Simon Glassdbfa32c2022-08-11 19:34:59 -0600186 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700187 if (ret)
188 puts("MMC partition switch failed\n");
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300189
190 return ret;
191}
Patrick Delaunay036a3582022-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 Lifshitz97d2fb92014-07-30 13:19:06 +0300206#else
Marek Vasut509f36e2021-10-17 19:23:36 +0200207static inline int mmc_set_env_part(struct mmc *mmc, uint part) {return 0; };
Patrick Delaunay036a3582022-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 Rinic929f822014-03-28 12:03:34 -0400210#endif
211
Tim Harveyeda75c32015-05-08 14:52:09 -0700212static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300213{
Tim Harveyeda75c32015-05-08 14:52:09 -0700214 if (!mmc)
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300215 return "No MMC card found";
Terry Lvb4eceac2010-05-17 10:57:01 +0800216
Sjoerd Simons5e4546d2018-03-22 22:53:50 +0100217#if CONFIG_IS_ENABLED(BLK)
Simon Glass1d9907c2017-05-27 11:37:18 -0600218 struct udevice *dev;
219
220 if (blk_get_from_parent(mmc->dev, &dev))
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300221 return "No block device";
Simon Glass1d9907c2017-05-27 11:37:18 -0600222#else
Tim Harveyeda75c32015-05-08 14:52:09 -0700223 if (mmc_init(mmc))
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300224 return "MMC init failed";
Simon Glass86d78c72017-04-23 20:02:04 -0600225#endif
Patrick Delaunay036a3582022-11-10 11:48:59 +0100226 if (!mmc_set_env_part_init(mmc))
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300227 return "MMC partition switch failed";
Tim Harveyeda75c32015-05-08 14:52:09 -0700228
229 return NULL;
Terry Lvb4eceac2010-05-17 10:57:01 +0800230}
231
Stephen Warren27ee0f32012-07-30 10:55:44 +0000232static void fini_mmc_for_env(struct mmc *mmc)
233{
Patrick Delaunay036a3582022-11-10 11:48:59 +0100234 mmc_set_env_part_restore(mmc);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000235}
236
Simon Glass082af922017-08-03 12:22:01 -0600237#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
Igor Grinbergecbfb622011-11-07 01:14:05 +0000238static inline int write_env(struct mmc *mmc, unsigned long size,
239 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800240{
241 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600242 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800243
Igor Grinbergecbfb622011-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 Lvb4eceac2010-05-17 10:57:01 +0800246
Simon Glassda898922016-05-14 14:03:03 -0600247 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800248
249 return (n == blk_cnt) ? 0 : -1;
250}
251
Simon Glass082af922017-08-03 12:22:01 -0600252static int env_mmc_save(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800253{
Tom Rinia840b8e2013-04-05 14:55:21 -0400254 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Gruberf7b61442016-01-20 15:43:37 +0100255 int dev = mmc_get_env_dev();
256 struct mmc *mmc = find_mmc_device(dev);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000257 u32 offset;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000258 int ret, copy = 0;
Tim Harveyeda75c32015-05-08 14:52:09 -0700259 const char *errmsg;
Terry Lvb4eceac2010-05-17 10:57:01 +0800260
Tim Harveyeda75c32015-05-08 14:52:09 -0700261 errmsg = init_mmc_for_env(mmc);
262 if (errmsg) {
263 printf("%s\n", errmsg);
Terry Lvb4eceac2010-05-17 10:57:01 +0800264 return 1;
Tim Harveyeda75c32015-05-08 14:52:09 -0700265 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800266
Marek Vasutd73c1292014-03-05 19:59:50 +0100267 ret = env_export(env_new);
268 if (ret)
Stephen Warren27ee0f32012-07-30 10:55:44 +0000269 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000270
Patrick Delaunay036a3582022-11-10 11:48:59 +0100271 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
272 if (gd->env_valid == ENV_VALID)
273 copy = 1;
Marek Vasut509f36e2021-10-17 19:23:36 +0200274
Patrick Delaunay036a3582022-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 Li45d13872023-01-31 14:41:58 +0800280 }
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000281
Ye Li45d13872023-01-31 14:41:58 +0800282 if (mmc_get_env_addr(mmc, copy, &offset)) {
283 ret = 1;
284 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000285 }
286
Clemens Gruberf7b61442016-01-20 15:43:37 +0100287 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren42a3c442012-05-24 11:38:33 +0000288 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800289 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000290 ret = 1;
291 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800292 }
293
Stephen Warren27ee0f32012-07-30 10:55:44 +0000294 ret = 0;
295
Patrick Delaunay036a3582022-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 Heimpolda1d25a32013-04-10 10:36:19 +0000298
Stephen Warren27ee0f32012-07-30 10:55:44 +0000299fini:
300 fini_mmc_for_env(mmc);
Patrick Delaunay036a3582022-11-10 11:48:59 +0100301
Stephen Warren27ee0f32012-07-30 10:55:44 +0000302 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800303}
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200304
Frank Wunderlich81f087e2019-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 Delaunayab5ea4d2022-02-15 16:23:23 +0100310 u32 erase_size;
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200311
Patrick Delaunayab5ea4d2022-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 Wunderlich81f087e2019-06-29 11:36:20 +0200315
316 n = blk_derase(desc, blk_start, blk_cnt);
Patrick Delaunayab5ea4d2022-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 Wunderlich81f087e2019-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 Vasutd07a6e32021-10-06 18:29:53 +0200337 if (mmc_get_env_addr(mmc, copy, &offset)) {
338 ret = CMD_RET_FAILURE;
339 goto fini;
340 }
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200341
Patrick Delaunayab5ea4d2022-02-15 16:23:23 +0100342 printf("\n");
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200343 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
344
Patrick Delaunay036a3582022-11-10 11:48:59 +0100345 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
346 copy = 1;
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200347
Patrick Delaunay036a3582022-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 Vasut509f36e2021-10-17 19:23:36 +0200353
Patrick Delaunay036a3582022-11-10 11:48:59 +0100354 if (mmc_get_env_addr(mmc, copy, &offset)) {
355 ret = CMD_RET_FAILURE;
356 goto fini;
357 }
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200358
Patrick Delaunay036a3582022-11-10 11:48:59 +0100359 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
360 }
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200361
Marek Vasutd07a6e32021-10-06 18:29:53 +0200362fini:
363 fini_mmc_for_env(mmc);
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200364 return ret;
365}
Simon Glass082af922017-08-03 12:22:01 -0600366#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lvb4eceac2010-05-17 10:57:01 +0800367
Igor Grinbergecbfb622011-11-07 01:14:05 +0000368static inline int read_env(struct mmc *mmc, unsigned long size,
369 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800370{
371 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600372 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800373
Igor Grinbergecbfb622011-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 Lvb4eceac2010-05-17 10:57:01 +0800376
Simon Glassda898922016-05-14 14:03:03 -0600377 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800378
379 return (n == blk_cnt) ? 0 : -1;
380}
381
Patrick Delaunayd948e712022-11-10 11:49:00 +0100382#if defined(ENV_IS_EMBEDDED)
Simon Glass99778492017-08-03 12:22:17 -0600383static int env_mmc_load(void)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000384{
Patrick Delaunayd948e712022-11-10 11:49:00 +0100385 return 0;
386}
387#elif defined(CONFIG_SYS_REDUNDAND_ENVIRONMENT)
388static int env_mmc_load(void)
389{
Tom Rinic929f822014-03-28 12:03:34 -0400390 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000391 u32 offset1, offset2;
392 int read1_fail = 0, read2_fail = 0;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000393 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100394 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700395 const char *errmsg = NULL;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000396
Markus Niebel6ef41fd2013-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 Abbasaae518f2018-02-12 19:24:31 +0530400 mmc_initialize(NULL);
401
Tom Rinic929f822014-03-28 12:03:34 -0400402 mmc = find_mmc_device(dev);
403
Tim Harveyeda75c32015-05-08 14:52:09 -0700404 errmsg = init_mmc_for_env(mmc);
405 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600406 ret = -EIO;
Michael Heimpolda1d25a32013-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 Glass99778492017-08-03 12:22:17 -0600412 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000413 goto fini;
414 }
415
Patrick Delaunay036a3582022-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 Vasut509f36e2021-10-17 19:23:36 +0200421
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000422 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
Marek Vasut509f36e2021-10-17 19:23:36 +0200423
Patrick Delaunay036a3582022-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 Vasut509f36e2021-10-17 19:23:36 +0200429
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000430 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
431
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100432 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200433 read2_fail, H_EXTERNAL);
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000434
435fini:
436 fini_mmc_for_env(mmc);
437err:
438 if (ret)
Simon Glass97385862019-08-01 09:47:00 -0600439 env_set_default(errmsg, 0);
Simon Glass99778492017-08-03 12:22:17 -0600440
Simon Glass99778492017-08-03 12:22:17 -0600441 return ret;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000442}
Patrick Delaunay036a3582022-11-10 11:48:59 +0100443#else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glass99778492017-08-03 12:22:17 -0600444static int env_mmc_load(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800445{
Tom Rinia840b8e2013-04-05 14:55:21 -0400446 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400447 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000448 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000449 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100450 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700451 const char *errmsg;
Pankit Gargc2342f62019-11-19 09:49:31 +0000452 env_t *ep = NULL;
Tom Rinic929f822014-03-28 12:03:34 -0400453
Tom Rinic929f822014-03-28 12:03:34 -0400454 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000455
Tim Harveyeda75c32015-05-08 14:52:09 -0700456 errmsg = init_mmc_for_env(mmc);
457 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600458 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000459 goto err;
460 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800461
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000462 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glass99778492017-08-03 12:22:17 -0600463 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000464 goto fini;
465 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800466
Tom Rinia840b8e2013-04-05 14:55:21 -0400467 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700468 errmsg = "!read failed";
Simon Glass99778492017-08-03 12:22:17 -0600469 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000470 goto fini;
471 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800472
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200473 ret = env_import(buf, 1, H_EXTERNAL);
Pankit Gargc2342f62019-11-19 09:49:31 +0000474 if (!ret) {
475 ep = (env_t *)buf;
476 gd->env_addr = (ulong)&ep->data;
477 }
Stephen Warren27ee0f32012-07-30 10:55:44 +0000478
479fini:
480 fini_mmc_for_env(mmc);
481err:
482 if (ret)
Simon Glass97385862019-08-01 09:47:00 -0600483 env_set_default(errmsg, 0);
Patrick Delaunayd948e712022-11-10 11:49:00 +0100484
Simon Glass99778492017-08-03 12:22:17 -0600485 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800486}
Patrick Delaunay036a3582022-11-10 11:48:59 +0100487#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
Simon Glassc10a88e2017-08-03 12:21:58 -0600488
489U_BOOT_ENV_LOCATION(mmc) = {
490 .location = ENVL_MMC,
Simon Glassd8273ed2017-08-03 12:22:03 -0600491 ENV_NAME("MMC")
Simon Glass082af922017-08-03 12:22:01 -0600492 .load = env_mmc_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600493#ifndef CONFIG_SPL_BUILD
Simon Glass082af922017-08-03 12:22:01 -0600494 .save = env_save_ptr(env_mmc_save),
Patrick Delaunay50f6f112021-02-09 11:48:50 +0100495 .erase = ENV_ERASE_PTR(env_mmc_erase)
Simon Glassc10a88e2017-08-03 12:21:58 -0600496#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600497};