blob: 465b104559b4eb587fcf7edbd2e9cdda1c4c348d [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 Glass11f2bb62016-05-01 13:52:29 -0600146 ret = blk_select_hwpart_devnum(IF_TYPE_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 Glass11f2bb62016-05-01 13:52:29 -0600182 blk_select_hwpart_devnum(IF_TYPE_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);
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 Vasutd07a6e32021-10-06 18:29:53 +0200284 if (mmc_get_env_addr(mmc, copy, &offset)) {
285 ret = CMD_RET_FAILURE;
286 goto fini;
287 }
Frank Wunderlich81f087e2019-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 Vasut509f36e2021-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 Vasutd07a6e32021-10-06 18:29:53 +0200300 if (mmc_get_env_addr(mmc, copy, &offset)) {
301 ret = CMD_RET_FAILURE;
302 goto fini;
303 }
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200304
305 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
306#endif
307
Marek Vasutd07a6e32021-10-06 18:29:53 +0200308fini:
309 fini_mmc_for_env(mmc);
Frank Wunderlich81f087e2019-06-29 11:36:20 +0200310 return ret;
311}
Simon Glass082af922017-08-03 12:22:01 -0600312#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lvb4eceac2010-05-17 10:57:01 +0800313
Igor Grinbergecbfb622011-11-07 01:14:05 +0000314static inline int read_env(struct mmc *mmc, unsigned long size,
315 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800316{
317 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600318 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800319
Igor Grinbergecbfb622011-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 Lvb4eceac2010-05-17 10:57:01 +0800322
Simon Glassda898922016-05-14 14:03:03 -0600323 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800324
325 return (n == blk_cnt) ? 0 : -1;
326}
327
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000328#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass99778492017-08-03 12:22:17 -0600329static int env_mmc_load(void)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000330{
331#if !defined(ENV_IS_EMBEDDED)
Tom Rinic929f822014-03-28 12:03:34 -0400332 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000333 u32 offset1, offset2;
334 int read1_fail = 0, read2_fail = 0;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000335 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100336 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700337 const char *errmsg = NULL;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000338
Markus Niebel6ef41fd2013-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 Abbasaae518f2018-02-12 19:24:31 +0530342 mmc_initialize(NULL);
343
Tom Rinic929f822014-03-28 12:03:34 -0400344 mmc = find_mmc_device(dev);
345
Tim Harveyeda75c32015-05-08 14:52:09 -0700346 errmsg = init_mmc_for_env(mmc);
347 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600348 ret = -EIO;
Michael Heimpolda1d25a32013-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 Glass99778492017-08-03 12:22:17 -0600354 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000355 goto fini;
356 }
357
Marek Vasut509f36e2021-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 Heimpolda1d25a32013-04-10 10:36:19 +0000364 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
Marek Vasut509f36e2021-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 Heimpolda1d25a32013-04-10 10:36:19 +0000372 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
373
Simon Goldschmidte07096e2018-01-31 14:47:11 +0100374 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200375 read2_fail, H_EXTERNAL);
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000376
377fini:
378 fini_mmc_for_env(mmc);
379err:
380 if (ret)
Simon Glass97385862019-08-01 09:47:00 -0600381 env_set_default(errmsg, 0);
Simon Glass99778492017-08-03 12:22:17 -0600382
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000383#endif
Simon Glass99778492017-08-03 12:22:17 -0600384 return ret;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000385}
386#else /* ! CONFIG_ENV_OFFSET_REDUND */
Simon Glass99778492017-08-03 12:22:17 -0600387static int env_mmc_load(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800388{
389#if !defined(ENV_IS_EMBEDDED)
Tom Rinia840b8e2013-04-05 14:55:21 -0400390 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400391 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000392 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +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;
Pankit Gargc2342f62019-11-19 09:49:31 +0000396 env_t *ep = NULL;
Tom Rinic929f822014-03-28 12:03:34 -0400397
Tom Rinic929f822014-03-28 12:03:34 -0400398 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000399
Tim Harveyeda75c32015-05-08 14:52:09 -0700400 errmsg = init_mmc_for_env(mmc);
401 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600402 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000403 goto err;
404 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800405
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000406 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glass99778492017-08-03 12:22:17 -0600407 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000408 goto fini;
409 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800410
Tom Rinia840b8e2013-04-05 14:55:21 -0400411 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700412 errmsg = "!read failed";
Simon Glass99778492017-08-03 12:22:17 -0600413 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000414 goto fini;
415 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800416
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200417 ret = env_import(buf, 1, H_EXTERNAL);
Pankit Gargc2342f62019-11-19 09:49:31 +0000418 if (!ret) {
419 ep = (env_t *)buf;
420 gd->env_addr = (ulong)&ep->data;
421 }
Stephen Warren27ee0f32012-07-30 10:55:44 +0000422
423fini:
424 fini_mmc_for_env(mmc);
425err:
426 if (ret)
Simon Glass97385862019-08-01 09:47:00 -0600427 env_set_default(errmsg, 0);
Terry Lvb4eceac2010-05-17 10:57:01 +0800428#endif
Simon Glass99778492017-08-03 12:22:17 -0600429 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800430}
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000431#endif /* CONFIG_ENV_OFFSET_REDUND */
Simon Glassc10a88e2017-08-03 12:21:58 -0600432
433U_BOOT_ENV_LOCATION(mmc) = {
434 .location = ENVL_MMC,
Simon Glassd8273ed2017-08-03 12:22:03 -0600435 ENV_NAME("MMC")
Simon Glass082af922017-08-03 12:22:01 -0600436 .load = env_mmc_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600437#ifndef CONFIG_SPL_BUILD
Simon Glass082af922017-08-03 12:22:01 -0600438 .save = env_save_ptr(env_mmc_save),
Patrick Delaunay50f6f112021-02-09 11:48:50 +0100439 .erase = ENV_ERASE_PTR(env_mmc_erase)
Simon Glassc10a88e2017-08-03 12:21:58 -0600440#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600441};