blob: 465b104559b4eb587fcf7edbd2e9cdda1c4c348d [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);
260
261 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
262 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
263
264 n = blk_derase(desc, blk_start, blk_cnt);
265 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
266
267 return (n == blk_cnt) ? 0 : 1;
268}
269
270static int env_mmc_erase(void)
271{
272 int dev = mmc_get_env_dev();
273 struct mmc *mmc = find_mmc_device(dev);
274 int ret, copy = 0;
275 u32 offset;
276 const char *errmsg;
277
278 errmsg = init_mmc_for_env(mmc);
279 if (errmsg) {
280 printf("%s\n", errmsg);
281 return 1;
282 }
283
Marek Vasutf47f87f2021-10-06 18:29:53 +0200284 if (mmc_get_env_addr(mmc, copy, &offset)) {
285 ret = CMD_RET_FAILURE;
286 goto fini;
287 }
Frank Wunderlich34853922019-06-29 11:36:20 +0200288
289 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
290
291#ifdef CONFIG_ENV_OFFSET_REDUND
292 copy = 1;
293
Marek Vasutd11d1be2021-10-17 19:23:36 +0200294#ifdef ENV_MMC_HWPART_REDUND
295 ret = mmc_set_env_part(mmc, copy + 1);
296 if (ret)
297 goto fini;
298#endif
299
Marek Vasutf47f87f2021-10-06 18:29:53 +0200300 if (mmc_get_env_addr(mmc, copy, &offset)) {
301 ret = CMD_RET_FAILURE;
302 goto fini;
303 }
Frank Wunderlich34853922019-06-29 11:36:20 +0200304
305 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
306#endif
307
Marek Vasutf47f87f2021-10-06 18:29:53 +0200308fini:
309 fini_mmc_for_env(mmc);
Frank Wunderlich34853922019-06-29 11:36:20 +0200310 return ret;
311}
Simon Glasse5bce242017-08-03 12:22:01 -0600312#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lva8060352010-05-17 10:57:01 +0800313
Igor Grinberge8db8f72011-11-07 01:14:05 +0000314static inline int read_env(struct mmc *mmc, unsigned long size,
315 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800316{
317 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600318 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800319
Igor Grinberge8db8f72011-11-07 01:14:05 +0000320 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
321 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800322
Simon Glass5461acb2016-05-14 14:03:03 -0600323 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800324
325 return (n == blk_cnt) ? 0 : -1;
326}
327
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000328#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassc5951992017-08-03 12:22:17 -0600329static int env_mmc_load(void)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000330{
331#if !defined(ENV_IS_EMBEDDED)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400332 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000333 u32 offset1, offset2;
334 int read1_fail = 0, read2_fail = 0;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000335 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100336 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700337 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000338
Markus Niebel452a2722013-10-04 15:48:03 +0200339 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
340 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
341
Faiz Abbas26862b42018-02-12 19:24:31 +0530342 mmc_initialize(NULL);
343
Tom Rinib9c8cca2014-03-28 12:03:34 -0400344 mmc = find_mmc_device(dev);
345
Tim Harveyc75648d2015-05-08 14:52:09 -0700346 errmsg = init_mmc_for_env(mmc);
347 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600348 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000349 goto err;
350 }
351
352 if (mmc_get_env_addr(mmc, 0, &offset1) ||
353 mmc_get_env_addr(mmc, 1, &offset2)) {
Simon Glassc5951992017-08-03 12:22:17 -0600354 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000355 goto fini;
356 }
357
Marek Vasutd11d1be2021-10-17 19:23:36 +0200358#ifdef ENV_MMC_HWPART_REDUND
359 ret = mmc_set_env_part(mmc, 1);
360 if (ret)
361 goto fini;
362#endif
363
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000364 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
Marek Vasutd11d1be2021-10-17 19:23:36 +0200365
366#ifdef ENV_MMC_HWPART_REDUND
367 ret = mmc_set_env_part(mmc, 2);
368 if (ret)
369 goto fini;
370#endif
371
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000372 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
373
Simon Goldschmidt31f044b2018-01-31 14:47:11 +0100374 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasut890feec2020-07-07 20:51:35 +0200375 read2_fail, H_EXTERNAL);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000376
377fini:
378 fini_mmc_for_env(mmc);
379err:
380 if (ret)
Simon Glass0ac7d722019-08-01 09:47:00 -0600381 env_set_default(errmsg, 0);
Simon Glassc5951992017-08-03 12:22:17 -0600382
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000383#endif
Simon Glassc5951992017-08-03 12:22:17 -0600384 return ret;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000385}
386#else /* ! CONFIG_ENV_OFFSET_REDUND */
Simon Glassc5951992017-08-03 12:22:17 -0600387static int env_mmc_load(void)
Terry Lva8060352010-05-17 10:57:01 +0800388{
389#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400390 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400391 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000392 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +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;
Pankit Garg0536b442019-11-19 09:49:31 +0000396 env_t *ep = NULL;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400397
Tom Rinib9c8cca2014-03-28 12:03:34 -0400398 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800399
Tim Harveyc75648d2015-05-08 14:52:09 -0700400 errmsg = init_mmc_for_env(mmc);
401 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600402 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000403 goto err;
404 }
Terry Lva8060352010-05-17 10:57:01 +0800405
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000406 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glassc5951992017-08-03 12:22:17 -0600407 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000408 goto fini;
409 }
410
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400411 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700412 errmsg = "!read failed";
Simon Glassc5951992017-08-03 12:22:17 -0600413 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000414 goto fini;
415 }
Terry Lva8060352010-05-17 10:57:01 +0800416
Marek Vasut890feec2020-07-07 20:51:35 +0200417 ret = env_import(buf, 1, H_EXTERNAL);
Pankit Garg0536b442019-11-19 09:49:31 +0000418 if (!ret) {
419 ep = (env_t *)buf;
420 gd->env_addr = (ulong)&ep->data;
421 }
Stephen Warren9404a5f2012-07-30 10:55:44 +0000422
423fini:
424 fini_mmc_for_env(mmc);
425err:
426 if (ret)
Simon Glass0ac7d722019-08-01 09:47:00 -0600427 env_set_default(errmsg, 0);
Terry Lva8060352010-05-17 10:57:01 +0800428#endif
Simon Glassc5951992017-08-03 12:22:17 -0600429 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800430}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000431#endif /* CONFIG_ENV_OFFSET_REDUND */
Simon Glass4415f1d2017-08-03 12:21:58 -0600432
433U_BOOT_ENV_LOCATION(mmc) = {
434 .location = ENVL_MMC,
Simon Glassac358be2017-08-03 12:22:03 -0600435 ENV_NAME("MMC")
Simon Glasse5bce242017-08-03 12:22:01 -0600436 .load = env_mmc_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600437#ifndef CONFIG_SPL_BUILD
Simon Glasse5bce242017-08-03 12:22:01 -0600438 .save = env_save_ptr(env_mmc_save),
Patrick Delaunay1af031a2021-02-09 11:48:50 +0100439 .erase = ENV_ERASE_PTR(env_mmc_erase)
Simon Glass4415f1d2017-08-03 12:21:58 -0600440#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600441};