blob: c28f4c6c6dc0f474d397eda1f76ac73e10d1d4a7 [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
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010024#define __STR(X) #X
25#define STR(X) __STR(X)
26
Terry Lvb4eceac2010-05-17 10:57:01 +080027DECLARE_GLOBAL_DATA_PTR;
28
Marek Vasut509f36e2021-10-17 19:23:36 +020029/*
30 * In case the environment is redundant, stored in eMMC hardware boot
31 * partition and the environment and redundant environment offsets are
32 * identical, store the environment and redundant environment in both
33 * eMMC boot partitions, one copy in each.
34 * */
35#if (defined(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && \
36 (CONFIG_SYS_MMC_ENV_PART == 1) && \
37 (CONFIG_ENV_OFFSET == CONFIG_ENV_OFFSET_REDUND))
38#define ENV_MMC_HWPART_REDUND
39#endif
40
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020041#if CONFIG_IS_ENABLED(OF_CONTROL)
Patrick Delaunayeff81952020-06-15 10:38:57 +020042static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010043{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060044 struct disk_partition info;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010045 struct blk_desc *desc;
46 int len, i, ret;
Patrick Delaunay530cb452020-06-15 10:38:55 +020047 char dev_str[4];
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010048
Patrick Delaunay530cb452020-06-15 10:38:55 +020049 snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev());
50 ret = blk_get_device_by_str("mmc", dev_str, &desc);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010051 if (ret < 0)
52 return (ret);
53
54 for (i = 1;;i++) {
55 ret = part_get_info(desc, i, &info);
56 if (ret < 0)
57 return ret;
58
Hoyeonjiki Kim626c3ce2020-11-17 12:32:08 +090059 if (!strncmp((const char *)info.name, str, sizeof(info.name)))
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010060 break;
61 }
62
63 /* round up to info.blksz */
Patrick Delaunay66fba3a2020-06-15 10:38:56 +020064 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010065
66 /* use the top of the partion for the environment */
Patrick Delaunayeff81952020-06-15 10:38:57 +020067 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010068
69 return 0;
70}
71
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020072static inline s64 mmc_offset(int copy)
73{
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010074 const struct {
75 const char *offset_redund;
76 const char *partition;
77 const char *offset;
78 } dt_prop = {
79 .offset_redund = "u-boot,mmc-env-offset-redundant",
80 .partition = "u-boot,mmc-env-partition",
81 .offset = "u-boot,mmc-env-offset",
82 };
Philipp Tomsich745d8de2017-11-21 23:29:40 +010083 s64 val = 0, defvalue;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010084 const char *propname;
85 const char *str;
86 int err;
87
88 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
Simon Glass0034d962021-08-07 07:24:01 -060089 str = ofnode_conf_read_str(dt_prop.partition);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010090 if (str) {
91 /* try to place the environment at end of the partition */
Patrick Delaunayeff81952020-06-15 10:38:57 +020092 err = mmc_offset_try_partition(str, copy, &val);
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +010093 if (!err)
94 return val;
95 }
96
97 defvalue = CONFIG_ENV_OFFSET;
98 propname = dt_prop.offset;
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020099
100#if defined(CONFIG_ENV_OFFSET_REDUND)
101 if (copy) {
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200102 defvalue = CONFIG_ENV_OFFSET_REDUND;
Jorge Ramirez-Ortiz03ab46c2017-11-06 14:16:37 +0100103 propname = dt_prop.offset_redund;
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200104 }
105#endif
Simon Glass0034d962021-08-07 07:24:01 -0600106 return ofnode_conf_read_int(propname, defvalue);
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200107}
108#else
109static inline s64 mmc_offset(int copy)
Mingkai Hua55fce32011-01-24 17:09:55 +0000110{
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200111 s64 offset = CONFIG_ENV_OFFSET;
Stephen Warren24dc2032013-06-11 15:14:02 -0600112
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200113#if defined(CONFIG_ENV_OFFSET_REDUND)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000114 if (copy)
Stephen Warren24dc2032013-06-11 15:14:02 -0600115 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000116#endif
Philipp Tomsich8ab507b2017-05-16 00:16:31 +0200117 return offset;
118}
119#endif
120
121__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
122{
123 s64 offset = mmc_offset(copy);
Stephen Warren24dc2032013-06-11 15:14:02 -0600124
125 if (offset < 0)
126 offset += mmc->capacity;
127
128 *env_addr = offset;
129
Mingkai Hua55fce32011-01-24 17:09:55 +0000130 return 0;
131}
Mingkai Hua55fce32011-01-24 17:09:55 +0000132
Tom Rinic929f822014-03-28 12:03:34 -0400133#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300134__weak uint mmc_get_env_part(struct mmc *mmc)
135{
136 return CONFIG_SYS_MMC_ENV_PART;
137}
138
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700139static unsigned char env_mmc_orig_hwpart;
140
Marek Vasut509f36e2021-10-17 19:23:36 +0200141static int mmc_set_env_part(struct mmc *mmc, uint part)
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300142{
Clemens Gruberf7b61442016-01-20 15:43:37 +0100143 int dev = mmc_get_env_dev();
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300144 int ret = 0;
Tom Rinic929f822014-03-28 12:03:34 -0400145
Simon Glassdbfa32c2022-08-11 19:34:59 -0600146 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700147 if (ret)
148 puts("MMC partition switch failed\n");
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300149
150 return ret;
151}
152#else
Marek Vasut509f36e2021-10-17 19:23:36 +0200153static inline int mmc_set_env_part(struct mmc *mmc, uint part) {return 0; };
Tom Rinic929f822014-03-28 12:03:34 -0400154#endif
155
Tim Harveyeda75c32015-05-08 14:52:09 -0700156static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300157{
Tim Harveyeda75c32015-05-08 14:52:09 -0700158 if (!mmc)
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300159 return "No MMC card found";
Terry Lvb4eceac2010-05-17 10:57:01 +0800160
Sjoerd Simons5e4546d2018-03-22 22:53:50 +0100161#if CONFIG_IS_ENABLED(BLK)
Simon Glass1d9907c2017-05-27 11:37:18 -0600162 struct udevice *dev;
163
164 if (blk_get_from_parent(mmc->dev, &dev))
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300165 return "No block device";
Simon Glass1d9907c2017-05-27 11:37:18 -0600166#else
Tim Harveyeda75c32015-05-08 14:52:09 -0700167 if (mmc_init(mmc))
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300168 return "MMC init failed";
Simon Glass86d78c72017-04-23 20:02:04 -0600169#endif
Marek Vasut509f36e2021-10-17 19:23:36 +0200170 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
171 if (mmc_set_env_part(mmc, mmc_get_env_part(mmc)))
Yaniv Levinskyb2bb9bc2018-06-24 19:16:57 +0300172 return "MMC partition switch failed";
Tim Harveyeda75c32015-05-08 14:52:09 -0700173
174 return NULL;
Terry Lvb4eceac2010-05-17 10:57:01 +0800175}
176
Stephen Warren27ee0f32012-07-30 10:55:44 +0000177static void fini_mmc_for_env(struct mmc *mmc)
178{
179#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Gruberf7b61442016-01-20 15:43:37 +0100180 int dev = mmc_get_env_dev();
Tom Rinic929f822014-03-28 12:03:34 -0400181
Simon Glassdbfa32c2022-08-11 19:34:59 -0600182 blk_select_hwpart_devnum(UCLASS_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000183#endif
184}
185
Simon Glass082af922017-08-03 12:22:01 -0600186#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
Igor Grinbergecbfb622011-11-07 01:14:05 +0000187static inline int write_env(struct mmc *mmc, unsigned long size,
188 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800189{
190 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600191 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800192
Igor Grinbergecbfb622011-11-07 01:14:05 +0000193 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
194 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800195
Simon Glassda898922016-05-14 14:03:03 -0600196 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800197
198 return (n == blk_cnt) ? 0 : -1;
199}
200
Simon Glass082af922017-08-03 12:22:01 -0600201static int env_mmc_save(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800202{
Tom Rinia840b8e2013-04-05 14:55:21 -0400203 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Gruberf7b61442016-01-20 15:43:37 +0100204 int dev = mmc_get_env_dev();
205 struct mmc *mmc = find_mmc_device(dev);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000206 u32 offset;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000207 int ret, copy = 0;
Tim Harveyeda75c32015-05-08 14:52:09 -0700208 const char *errmsg;
Terry Lvb4eceac2010-05-17 10:57:01 +0800209
Tim Harveyeda75c32015-05-08 14:52:09 -0700210 errmsg = init_mmc_for_env(mmc);
211 if (errmsg) {
212 printf("%s\n", errmsg);
Terry Lvb4eceac2010-05-17 10:57:01 +0800213 return 1;
Tim Harveyeda75c32015-05-08 14:52:09 -0700214 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800215
Marek Vasutd73c1292014-03-05 19:59:50 +0100216 ret = env_export(env_new);
217 if (ret)
Stephen Warren27ee0f32012-07-30 10:55:44 +0000218 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000219
220#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600221 if (gd->env_valid == ENV_VALID)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000222 copy = 1;
Marek Vasut509f36e2021-10-17 19:23:36 +0200223
224#ifdef ENV_MMC_HWPART_REDUND
225 ret = mmc_set_env_part(mmc, copy + 1);
226 if (ret)
227 goto fini;
228#endif
229
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000230#endif
231
232 if (mmc_get_env_addr(mmc, copy, &offset)) {
233 ret = 1;
234 goto fini;
235 }
236
Clemens Gruberf7b61442016-01-20 15:43:37 +0100237 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren42a3c442012-05-24 11:38:33 +0000238 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800239 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000240 ret = 1;
241 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800242 }
243
Stephen Warren27ee0f32012-07-30 10:55:44 +0000244 ret = 0;
245
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000246#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600247 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000248#endif
249
Stephen Warren27ee0f32012-07-30 10:55:44 +0000250fini:
251 fini_mmc_for_env(mmc);
252 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800253}
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200254
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200255static inline int erase_env(struct mmc *mmc, unsigned long size,
256 unsigned long offset)
257{
258 uint blk_start, blk_cnt, n;
259 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Patrick Delaunayab5ea4d2022-02-15 16:23:23 +0100260 u32 erase_size;
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200261
Patrick Delaunayab5ea4d2022-02-15 16:23:23 +0100262 erase_size = mmc->erase_grp_size * desc->blksz;
263 blk_start = ALIGN_DOWN(offset, erase_size) / desc->blksz;
264 blk_cnt = ALIGN(size, erase_size) / desc->blksz;
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200265
266 n = blk_derase(desc, blk_start, blk_cnt);
Patrick Delaunayab5ea4d2022-02-15 16:23:23 +0100267 printf("%d blocks erased at 0x%x: %s\n", n, blk_start,
268 (n == blk_cnt) ? "OK" : "ERROR");
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200269
270 return (n == blk_cnt) ? 0 : 1;
271}
272
273static int env_mmc_erase(void)
274{
275 int dev = mmc_get_env_dev();
276 struct mmc *mmc = find_mmc_device(dev);
277 int ret, copy = 0;
278 u32 offset;
279 const char *errmsg;
280
281 errmsg = init_mmc_for_env(mmc);
282 if (errmsg) {
283 printf("%s\n", errmsg);
284 return 1;
285 }
286
Marek Vasutd07a6e32021-10-06 18:29:53 +0200287 if (mmc_get_env_addr(mmc, copy, &offset)) {
288 ret = CMD_RET_FAILURE;
289 goto fini;
290 }
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200291
Patrick Delaunayab5ea4d2022-02-15 16:23:23 +0100292 printf("\n");
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200293 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
294
295#ifdef CONFIG_ENV_OFFSET_REDUND
296 copy = 1;
297
Marek Vasut509f36e2021-10-17 19:23:36 +0200298#ifdef ENV_MMC_HWPART_REDUND
299 ret = mmc_set_env_part(mmc, copy + 1);
300 if (ret)
301 goto fini;
302#endif
303
Marek Vasutd07a6e32021-10-06 18:29:53 +0200304 if (mmc_get_env_addr(mmc, copy, &offset)) {
305 ret = CMD_RET_FAILURE;
306 goto fini;
307 }
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200308
309 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
310#endif
311
Marek Vasutd07a6e32021-10-06 18:29:53 +0200312fini:
313 fini_mmc_for_env(mmc);
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200314 return ret;
315}
Simon Glass082af922017-08-03 12:22:01 -0600316#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lvb4eceac2010-05-17 10:57:01 +0800317
Igor Grinbergecbfb622011-11-07 01:14:05 +0000318static inline int read_env(struct mmc *mmc, unsigned long size,
319 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800320{
321 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600322 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800323
Igor Grinbergecbfb622011-11-07 01:14:05 +0000324 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
325 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800326
Simon Glassda898922016-05-14 14:03:03 -0600327 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800328
329 return (n == blk_cnt) ? 0 : -1;
330}
331
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000332#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass99778492017-08-03 12:22:17 -0600333static int env_mmc_load(void)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000334{
335#if !defined(ENV_IS_EMBEDDED)
Tom Rinic929f822014-03-28 12:03:34 -0400336 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000337 u32 offset1, offset2;
338 int read1_fail = 0, read2_fail = 0;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000339 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100340 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700341 const char *errmsg = NULL;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000342
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200343 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
344 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
345
Faiz Abbasaae518f2018-02-12 19:24:31 +0530346 mmc_initialize(NULL);
347
Tom Rinic929f822014-03-28 12:03:34 -0400348 mmc = find_mmc_device(dev);
349
Tim Harveyeda75c32015-05-08 14:52:09 -0700350 errmsg = init_mmc_for_env(mmc);
351 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600352 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000353 goto err;
354 }
355
356 if (mmc_get_env_addr(mmc, 0, &offset1) ||
357 mmc_get_env_addr(mmc, 1, &offset2)) {
Simon Glass99778492017-08-03 12:22:17 -0600358 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000359 goto fini;
360 }
361
Marek Vasut509f36e2021-10-17 19:23:36 +0200362#ifdef ENV_MMC_HWPART_REDUND
363 ret = mmc_set_env_part(mmc, 1);
364 if (ret)
365 goto fini;
366#endif
367
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000368 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
Marek Vasut509f36e2021-10-17 19:23:36 +0200369
370#ifdef ENV_MMC_HWPART_REDUND
371 ret = mmc_set_env_part(mmc, 2);
372 if (ret)
373 goto fini;
374#endif
375
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000376 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
377
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100378 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200379 read2_fail, H_EXTERNAL);
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000380
381fini:
382 fini_mmc_for_env(mmc);
383err:
384 if (ret)
Simon Glass97385862019-08-01 09:47:00 -0600385 env_set_default(errmsg, 0);
Simon Glass99778492017-08-03 12:22:17 -0600386
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000387#endif
Simon Glass99778492017-08-03 12:22:17 -0600388 return ret;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000389}
390#else /* ! CONFIG_ENV_OFFSET_REDUND */
Simon Glass99778492017-08-03 12:22:17 -0600391static int env_mmc_load(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800392{
393#if !defined(ENV_IS_EMBEDDED)
Tom Rinia840b8e2013-04-05 14:55:21 -0400394 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400395 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000396 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000397 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100398 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700399 const char *errmsg;
Pankit Gargc2342f62019-11-19 09:49:31 +0000400 env_t *ep = NULL;
Tom Rinic929f822014-03-28 12:03:34 -0400401
Tom Rinic929f822014-03-28 12:03:34 -0400402 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000403
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;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000407 goto err;
408 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800409
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000410 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glass99778492017-08-03 12:22:17 -0600411 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000412 goto fini;
413 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800414
Tom Rinia840b8e2013-04-05 14:55:21 -0400415 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700416 errmsg = "!read failed";
Simon Glass99778492017-08-03 12:22:17 -0600417 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000418 goto fini;
419 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800420
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200421 ret = env_import(buf, 1, H_EXTERNAL);
Pankit Gargc2342f62019-11-19 09:49:31 +0000422 if (!ret) {
423 ep = (env_t *)buf;
424 gd->env_addr = (ulong)&ep->data;
425 }
Stephen Warren27ee0f32012-07-30 10:55:44 +0000426
427fini:
428 fini_mmc_for_env(mmc);
429err:
430 if (ret)
Simon Glass97385862019-08-01 09:47:00 -0600431 env_set_default(errmsg, 0);
Terry Lvb4eceac2010-05-17 10:57:01 +0800432#endif
Simon Glass99778492017-08-03 12:22:17 -0600433 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800434}
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000435#endif /* CONFIG_ENV_OFFSET_REDUND */
Simon Glassc10a88e2017-08-03 12:21:58 -0600436
437U_BOOT_ENV_LOCATION(mmc) = {
438 .location = ENVL_MMC,
Simon Glassd8273ed2017-08-03 12:22:03 -0600439 ENV_NAME("MMC")
Simon Glass082af922017-08-03 12:22:01 -0600440 .load = env_mmc_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600441#ifndef CONFIG_SPL_BUILD
Simon Glass082af922017-08-03 12:22:01 -0600442 .save = env_save_ptr(env_mmc_save),
Patrick Delaunay50f6f112021-02-09 11:48:50 +0100443 .erase = ENV_ERASE_PTR(env_mmc_erase)
Simon Glassc10a88e2017-08-03 12:21:58 -0600444#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600445};