blob: 0c498d9a46b51b9661327a5c9cef1b61a006ae4d [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
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010024#define __STR(X) #X
25#define STR(X) __STR(X)
26
Terry Lva8060352010-05-17 10:57:01 +080027DECLARE_GLOBAL_DATA_PTR;
28
Marek Vasutd11d1be2021-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 Tomsichf8b8a552017-05-16 00:16:31 +020041#if CONFIG_IS_ENABLED(OF_CONTROL)
Patrick Delaunay5d4f7b42020-06-15 10:38:57 +020042static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010043{
Simon Glass05289792020-05-10 11:39:57 -060044 struct disk_partition info;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010045 struct blk_desc *desc;
46 int len, i, ret;
Patrick Delaunay2b2f7272020-06-15 10:38:55 +020047 char dev_str[4];
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010048
Patrick Delaunay2b2f7272020-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-Ortizc9e87ba2017-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 Kim2a0a5772020-11-17 12:32:08 +090059 if (!strncmp((const char *)info.name, str, sizeof(info.name)))
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010060 break;
61 }
62
63 /* round up to info.blksz */
Patrick Delaunay76b640c2020-06-15 10:38:56 +020064 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010065
66 /* use the top of the partion for the environment */
Patrick Delaunay5d4f7b42020-06-15 10:38:57 +020067 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010068
69 return 0;
70}
71
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020072static inline s64 mmc_offset(int copy)
Mingkai Hu97039ab2011-01-24 17:09:55 +000073{
Jorge Ramirez-Ortizc9e87ba2017-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 Tomsichfd374662017-11-21 23:29:40 +010083 s64 val = 0, defvalue;
Jorge Ramirez-Ortizc9e87ba2017-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 Glass7de8bd02021-08-07 07:24:01 -060089 str = ofnode_conf_read_str(dt_prop.partition);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010090 if (str) {
91 /* try to place the environment at end of the partition */
Patrick Delaunay5d4f7b42020-06-15 10:38:57 +020092 err = mmc_offset_try_partition(str, copy, &val);
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +010093 if (!err)
94 return val;
95 }
96
97 defvalue = CONFIG_ENV_OFFSET;
98 propname = dt_prop.offset;
Stephen Warren5c088ee2013-06-11 15:14:02 -060099
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200100#if defined(CONFIG_ENV_OFFSET_REDUND)
101 if (copy) {
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200102 defvalue = CONFIG_ENV_OFFSET_REDUND;
Jorge Ramirez-Ortizc9e87ba2017-11-06 14:16:37 +0100103 propname = dt_prop.offset_redund;
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200104 }
105#endif
Simon Glass7de8bd02021-08-07 07:24:01 -0600106 return ofnode_conf_read_int(propname, defvalue);
Philipp Tomsichf8b8a552017-05-16 00:16:31 +0200107}
108#else
109static inline s64 mmc_offset(int copy)
110{
111 s64 offset = CONFIG_ENV_OFFSET;
112
113#if defined(CONFIG_ENV_OFFSET_REDUND)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000114 if (copy)
Stephen Warren5c088ee2013-06-11 15:14:02 -0600115 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000116#endif
Philipp Tomsichf8b8a552017-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 Warren5c088ee2013-06-11 15:14:02 -0600124
125 if (offset < 0)
126 offset += mmc->capacity;
127
128 *env_addr = offset;
129
Mingkai Hu97039ab2011-01-24 17:09:55 +0000130 return 0;
131}
Mingkai Hu97039ab2011-01-24 17:09:55 +0000132
Tom Rinib9c8cca2014-03-28 12:03:34 -0400133#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz6e7b7df2014-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 Warren873cc1d2015-12-07 11:38:49 -0700139static unsigned char env_mmc_orig_hwpart;
140
Marek Vasutd11d1be2021-10-17 19:23:36 +0200141static int mmc_set_env_part(struct mmc *mmc, uint part)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300142{
Clemens Grubere92029c2016-01-20 15:43:37 +0100143 int dev = mmc_get_env_dev();
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300144 int ret = 0;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400145
Simon Glass69f45cd2016-05-01 13:52:29 -0600146 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren873cc1d2015-12-07 11:38:49 -0700147 if (ret)
148 puts("MMC partition switch failed\n");
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300149
150 return ret;
151}
152#else
Marek Vasutd11d1be2021-10-17 19:23:36 +0200153static inline int mmc_set_env_part(struct mmc *mmc, uint part) {return 0; };
Tom Rinib9c8cca2014-03-28 12:03:34 -0400154#endif
155
Tim Harveyc75648d2015-05-08 14:52:09 -0700156static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300157{
Tim Harveyc75648d2015-05-08 14:52:09 -0700158 if (!mmc)
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300159 return "No MMC card found";
Terry Lva8060352010-05-17 10:57:01 +0800160
Sjoerd Simonsd48b8d12018-03-22 22:53:50 +0100161#if CONFIG_IS_ENABLED(BLK)
Simon Glass01b73fe2017-05-27 11:37:18 -0600162 struct udevice *dev;
163
164 if (blk_get_from_parent(mmc->dev, &dev))
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300165 return "No block device";
Simon Glass01b73fe2017-05-27 11:37:18 -0600166#else
Tim Harveyc75648d2015-05-08 14:52:09 -0700167 if (mmc_init(mmc))
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300168 return "MMC init failed";
Simon Glasse7017a32017-04-23 20:02:04 -0600169#endif
Marek Vasutd11d1be2021-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 Levinskyc5d548a2018-06-24 19:16:57 +0300172 return "MMC partition switch failed";
Tim Harveyc75648d2015-05-08 14:52:09 -0700173
174 return NULL;
Terry Lva8060352010-05-17 10:57:01 +0800175}
176
Stephen Warren9404a5f2012-07-30 10:55:44 +0000177static void fini_mmc_for_env(struct mmc *mmc)
178{
179#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Grubere92029c2016-01-20 15:43:37 +0100180 int dev = mmc_get_env_dev();
Tom Rinib9c8cca2014-03-28 12:03:34 -0400181
Simon Glass69f45cd2016-05-01 13:52:29 -0600182 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000183#endif
184}
185
Simon Glasse5bce242017-08-03 12:22:01 -0600186#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
Igor Grinberge8db8f72011-11-07 01:14:05 +0000187static inline int write_env(struct mmc *mmc, unsigned long size,
188 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800189{
190 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600191 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800192
Igor Grinberge8db8f72011-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 Lva8060352010-05-17 10:57:01 +0800195
Simon Glass5461acb2016-05-14 14:03:03 -0600196 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800197
198 return (n == blk_cnt) ? 0 : -1;
199}
200
Simon Glasse5bce242017-08-03 12:22:01 -0600201static int env_mmc_save(void)
Terry Lva8060352010-05-17 10:57:01 +0800202{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400203 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Grubere92029c2016-01-20 15:43:37 +0100204 int dev = mmc_get_env_dev();
205 struct mmc *mmc = find_mmc_device(dev);
Igor Grinberge8db8f72011-11-07 01:14:05 +0000206 u32 offset;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000207 int ret, copy = 0;
Tim Harveyc75648d2015-05-08 14:52:09 -0700208 const char *errmsg;
Terry Lva8060352010-05-17 10:57:01 +0800209
Tim Harveyc75648d2015-05-08 14:52:09 -0700210 errmsg = init_mmc_for_env(mmc);
211 if (errmsg) {
212 printf("%s\n", errmsg);
Mingkai Hu97039ab2011-01-24 17:09:55 +0000213 return 1;
Tim Harveyc75648d2015-05-08 14:52:09 -0700214 }
Mingkai Hu97039ab2011-01-24 17:09:55 +0000215
Marek Vasut7ce15262014-03-05 19:59:50 +0100216 ret = env_export(env_new);
217 if (ret)
Stephen Warren9404a5f2012-07-30 10:55:44 +0000218 goto fini;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000219
220#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600221 if (gd->env_valid == ENV_VALID)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000222 copy = 1;
Marek Vasutd11d1be2021-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 Heimpoldd196bd82013-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 Grubere92029c2016-01-20 15:43:37 +0100237 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren4036b632012-05-24 11:38:33 +0000238 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800239 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000240 ret = 1;
241 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800242 }
243
Stephen Warren9404a5f2012-07-30 10:55:44 +0000244 ret = 0;
245
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000246#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600247 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000248#endif
249
Stephen Warren9404a5f2012-07-30 10:55:44 +0000250fini:
251 fini_mmc_for_env(mmc);
252 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800253}
Frank Wunderlich34853922019-06-29 11:36:20 +0200254
Frank Wunderlich34853922019-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 Delaunayd7226702022-02-15 16:23:23 +0100260 u32 erase_size;
Frank Wunderlich34853922019-06-29 11:36:20 +0200261
Patrick Delaunayd7226702022-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 Wunderlich34853922019-06-29 11:36:20 +0200265
266 n = blk_derase(desc, blk_start, blk_cnt);
Patrick Delaunayd7226702022-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 Wunderlich34853922019-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 Vasutf47f87f2021-10-06 18:29:53 +0200287 if (mmc_get_env_addr(mmc, copy, &offset)) {
288 ret = CMD_RET_FAILURE;
289 goto fini;
290 }
Frank Wunderlich34853922019-06-29 11:36:20 +0200291
Patrick Delaunayd7226702022-02-15 16:23:23 +0100292 printf("\n");
Frank Wunderlich34853922019-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 Vasutd11d1be2021-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 Vasutf47f87f2021-10-06 18:29:53 +0200304 if (mmc_get_env_addr(mmc, copy, &offset)) {
305 ret = CMD_RET_FAILURE;
306 goto fini;
307 }
Frank Wunderlich34853922019-06-29 11:36:20 +0200308
309 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
310#endif
311
Marek Vasutf47f87f2021-10-06 18:29:53 +0200312fini:
313 fini_mmc_for_env(mmc);
Frank Wunderlich34853922019-06-29 11:36:20 +0200314 return ret;
315}
Simon Glasse5bce242017-08-03 12:22:01 -0600316#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lva8060352010-05-17 10:57:01 +0800317
Igor Grinberge8db8f72011-11-07 01:14:05 +0000318static inline int read_env(struct mmc *mmc, unsigned long size,
319 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800320{
321 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600322 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800323
Igor Grinberge8db8f72011-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 Lva8060352010-05-17 10:57:01 +0800326
Simon Glass5461acb2016-05-14 14:03:03 -0600327 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800328
329 return (n == blk_cnt) ? 0 : -1;
330}
331
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000332#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassc5951992017-08-03 12:22:17 -0600333static int env_mmc_load(void)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000334{
335#if !defined(ENV_IS_EMBEDDED)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400336 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000337 u32 offset1, offset2;
338 int read1_fail = 0, read2_fail = 0;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000339 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100340 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700341 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000342
Markus Niebel452a2722013-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 Abbas26862b42018-02-12 19:24:31 +0530346 mmc_initialize(NULL);
347
Tom Rinib9c8cca2014-03-28 12:03:34 -0400348 mmc = find_mmc_device(dev);
349
Tim Harveyc75648d2015-05-08 14:52:09 -0700350 errmsg = init_mmc_for_env(mmc);
351 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600352 ret = -EIO;
Michael Heimpoldd196bd82013-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 Glassc5951992017-08-03 12:22:17 -0600358 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000359 goto fini;
360 }
361
Marek Vasutd11d1be2021-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 Heimpoldd196bd82013-04-10 10:36:19 +0000368 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
Marek Vasutd11d1be2021-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 Heimpoldd196bd82013-04-10 10:36:19 +0000376 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
377
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100378 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasut890feec2020-07-07 20:51:35 +0200379 read2_fail, H_EXTERNAL);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000380
381fini:
382 fini_mmc_for_env(mmc);
383err:
384 if (ret)
Simon Glass0ac7d722019-08-01 09:47:00 -0600385 env_set_default(errmsg, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600386
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000387#endif
Simon Glassc5951992017-08-03 12:22:17 -0600388 return ret;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000389}
390#else /* ! CONFIG_ENV_OFFSET_REDUND */
Simon Glassc5951992017-08-03 12:22:17 -0600391static int env_mmc_load(void)
Terry Lva8060352010-05-17 10:57:01 +0800392{
393#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400394 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400395 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000396 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000397 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100398 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700399 const char *errmsg;
Pankit Garg0536b442019-11-19 09:49:31 +0000400 env_t *ep = NULL;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400401
Tom Rinib9c8cca2014-03-28 12:03:34 -0400402 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800403
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;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000407 goto err;
408 }
Terry Lva8060352010-05-17 10:57:01 +0800409
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000410 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glassc5951992017-08-03 12:22:17 -0600411 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000412 goto fini;
413 }
414
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400415 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700416 errmsg = "!read failed";
Simon Glassc5951992017-08-03 12:22:17 -0600417 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000418 goto fini;
419 }
Terry Lva8060352010-05-17 10:57:01 +0800420
Marek Vasut890feec2020-07-07 20:51:35 +0200421 ret = env_import(buf, 1, H_EXTERNAL);
Pankit Garg0536b442019-11-19 09:49:31 +0000422 if (!ret) {
423 ep = (env_t *)buf;
424 gd->env_addr = (ulong)&ep->data;
425 }
Stephen Warren9404a5f2012-07-30 10:55:44 +0000426
427fini:
428 fini_mmc_for_env(mmc);
429err:
430 if (ret)
Simon Glass0ac7d722019-08-01 09:47:00 -0600431 env_set_default(errmsg, 0);
Terry Lva8060352010-05-17 10:57:01 +0800432#endif
Simon Glassc5951992017-08-03 12:22:17 -0600433 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800434}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000435#endif /* CONFIG_ENV_OFFSET_REDUND */
Simon Glass4415f1d2017-08-03 12:21:58 -0600436
437U_BOOT_ENV_LOCATION(mmc) = {
438 .location = ENVL_MMC,
Simon Glassac358be2017-08-03 12:22:03 -0600439 ENV_NAME("MMC")
Simon Glasse5bce242017-08-03 12:22:01 -0600440 .load = env_mmc_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600441#ifndef CONFIG_SPL_BUILD
Simon Glasse5bce242017-08-03 12:22:01 -0600442 .save = env_save_ptr(env_mmc_save),
Patrick Delaunay1af031a2021-02-09 11:48:50 +0100443 .erase = ENV_ERASE_PTR(env_mmc_erase)
Simon Glass4415f1d2017-08-03 12:21:58 -0600444#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600445};