blob: 3f3092d975607a48161e3e74819d98a67a16ad64 [file] [log] [blame]
Terry Lva8060352010-05-17 10:57:01 +08001/*
Mingkai Hu97039ab2011-01-24 17:09:55 +00002 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
Terry Lva8060352010-05-17 10:57:01 +08003 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02004 * SPDX-License-Identifier: GPL-2.0+
Terry Lva8060352010-05-17 10:57:01 +08005 */
6
7/* #define DEBUG */
8
9#include <common.h>
10
11#include <command.h>
12#include <environment.h>
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020013#include <fdtdec.h>
Terry Lva8060352010-05-17 10:57:01 +080014#include <linux/stddef.h>
15#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060016#include <memalign.h>
Terry Lva8060352010-05-17 10:57:01 +080017#include <mmc.h>
Lei Wen6d1d51b2010-11-10 07:39:23 +080018#include <search.h>
Lei Wene79f4832010-10-13 11:07:21 +080019#include <errno.h>
Terry Lva8060352010-05-17 10:57:01 +080020
Michael Heimpoldd196bd82013-04-10 10:36:19 +000021#if defined(CONFIG_ENV_SIZE_REDUND) && \
22 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
23#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
24#endif
25
Terry Lva8060352010-05-17 10:57:01 +080026DECLARE_GLOBAL_DATA_PTR;
27
Mingkai Hu97039ab2011-01-24 17:09:55 +000028#if !defined(CONFIG_ENV_OFFSET)
29#define CONFIG_ENV_OFFSET 0
30#endif
31
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020032#if CONFIG_IS_ENABLED(OF_CONTROL)
33static inline s64 mmc_offset(int copy)
Mingkai Hu97039ab2011-01-24 17:09:55 +000034{
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020035 const char *propname = "u-boot,mmc-env-offset";
36 s64 defvalue = CONFIG_ENV_OFFSET;
Stephen Warren5c088ee2013-06-11 15:14:02 -060037
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020038#if defined(CONFIG_ENV_OFFSET_REDUND)
39 if (copy) {
40 propname = "u-boot,mmc-env-offset-redundant";
41 defvalue = CONFIG_ENV_OFFSET_REDUND;
42 }
43#endif
44
45 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
46}
47#else
48static inline s64 mmc_offset(int copy)
49{
50 s64 offset = CONFIG_ENV_OFFSET;
51
52#if defined(CONFIG_ENV_OFFSET_REDUND)
Michael Heimpoldd196bd82013-04-10 10:36:19 +000053 if (copy)
Stephen Warren5c088ee2013-06-11 15:14:02 -060054 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +000055#endif
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020056 return offset;
57}
58#endif
59
60__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
61{
62 s64 offset = mmc_offset(copy);
Stephen Warren5c088ee2013-06-11 15:14:02 -060063
64 if (offset < 0)
65 offset += mmc->capacity;
66
67 *env_addr = offset;
68
Mingkai Hu97039ab2011-01-24 17:09:55 +000069 return 0;
70}
Mingkai Hu97039ab2011-01-24 17:09:55 +000071
Clemens Grubere92029c2016-01-20 15:43:37 +010072__weak int mmc_get_env_dev(void)
73{
74 return CONFIG_SYS_MMC_ENV_DEV;
75}
76
Tom Rinib9c8cca2014-03-28 12:03:34 -040077#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030078__weak uint mmc_get_env_part(struct mmc *mmc)
79{
80 return CONFIG_SYS_MMC_ENV_PART;
81}
82
Stephen Warren873cc1d2015-12-07 11:38:49 -070083static unsigned char env_mmc_orig_hwpart;
84
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030085static int mmc_set_env_part(struct mmc *mmc)
86{
87 uint part = mmc_get_env_part(mmc);
Clemens Grubere92029c2016-01-20 15:43:37 +010088 int dev = mmc_get_env_dev();
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030089 int ret = 0;
Tom Rinib9c8cca2014-03-28 12:03:34 -040090
Simon Glass69f45cd2016-05-01 13:52:29 -060091 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
92 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren873cc1d2015-12-07 11:38:49 -070093 if (ret)
94 puts("MMC partition switch failed\n");
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030095
96 return ret;
97}
98#else
99static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
Tom Rinib9c8cca2014-03-28 12:03:34 -0400100#endif
101
Tim Harveyc75648d2015-05-08 14:52:09 -0700102static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300103{
Tim Harveyc75648d2015-05-08 14:52:09 -0700104 if (!mmc)
Hans de Goedea85da212015-08-15 20:05:01 +0200105 return "!No MMC card found";
Terry Lva8060352010-05-17 10:57:01 +0800106
Simon Glass01b73fe2017-05-27 11:37:18 -0600107#ifdef CONFIG_BLK
108 struct udevice *dev;
109
110 if (blk_get_from_parent(mmc->dev, &dev))
111 return "!No block device";
112#else
Tim Harveyc75648d2015-05-08 14:52:09 -0700113 if (mmc_init(mmc))
Hans de Goedea85da212015-08-15 20:05:01 +0200114 return "!MMC init failed";
Simon Glasse7017a32017-04-23 20:02:04 -0600115#endif
Tim Harveyc75648d2015-05-08 14:52:09 -0700116 if (mmc_set_env_part(mmc))
Hans de Goedea85da212015-08-15 20:05:01 +0200117 return "!MMC partition switch failed";
Tim Harveyc75648d2015-05-08 14:52:09 -0700118
119 return NULL;
Terry Lva8060352010-05-17 10:57:01 +0800120}
121
Stephen Warren9404a5f2012-07-30 10:55:44 +0000122static void fini_mmc_for_env(struct mmc *mmc)
123{
124#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Grubere92029c2016-01-20 15:43:37 +0100125 int dev = mmc_get_env_dev();
Tom Rinib9c8cca2014-03-28 12:03:34 -0400126
Simon Glass69f45cd2016-05-01 13:52:29 -0600127 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000128#endif
129}
130
Simon Glasse5bce242017-08-03 12:22:01 -0600131#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
Igor Grinberge8db8f72011-11-07 01:14:05 +0000132static inline int write_env(struct mmc *mmc, unsigned long size,
133 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800134{
135 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600136 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800137
Igor Grinberge8db8f72011-11-07 01:14:05 +0000138 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
139 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800140
Simon Glass5461acb2016-05-14 14:03:03 -0600141 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800142
143 return (n == blk_cnt) ? 0 : -1;
144}
145
Simon Glasse5bce242017-08-03 12:22:01 -0600146static int env_mmc_save(void)
Terry Lva8060352010-05-17 10:57:01 +0800147{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400148 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Grubere92029c2016-01-20 15:43:37 +0100149 int dev = mmc_get_env_dev();
150 struct mmc *mmc = find_mmc_device(dev);
Igor Grinberge8db8f72011-11-07 01:14:05 +0000151 u32 offset;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000152 int ret, copy = 0;
Tim Harveyc75648d2015-05-08 14:52:09 -0700153 const char *errmsg;
Terry Lva8060352010-05-17 10:57:01 +0800154
Tim Harveyc75648d2015-05-08 14:52:09 -0700155 errmsg = init_mmc_for_env(mmc);
156 if (errmsg) {
157 printf("%s\n", errmsg);
Mingkai Hu97039ab2011-01-24 17:09:55 +0000158 return 1;
Tim Harveyc75648d2015-05-08 14:52:09 -0700159 }
Mingkai Hu97039ab2011-01-24 17:09:55 +0000160
Marek Vasut7ce15262014-03-05 19:59:50 +0100161 ret = env_export(env_new);
162 if (ret)
Stephen Warren9404a5f2012-07-30 10:55:44 +0000163 goto fini;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000164
165#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600166 if (gd->env_valid == ENV_VALID)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000167 copy = 1;
168#endif
169
170 if (mmc_get_env_addr(mmc, copy, &offset)) {
171 ret = 1;
172 goto fini;
173 }
174
Clemens Grubere92029c2016-01-20 15:43:37 +0100175 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren4036b632012-05-24 11:38:33 +0000176 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800177 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000178 ret = 1;
179 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800180 }
181
182 puts("done\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000183 ret = 0;
184
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000185#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600186 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000187#endif
188
Stephen Warren9404a5f2012-07-30 10:55:44 +0000189fini:
190 fini_mmc_for_env(mmc);
191 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800192}
Simon Glasse5bce242017-08-03 12:22:01 -0600193#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lva8060352010-05-17 10:57:01 +0800194
Igor Grinberge8db8f72011-11-07 01:14:05 +0000195static inline int read_env(struct mmc *mmc, unsigned long size,
196 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800197{
198 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600199 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800200
Igor Grinberge8db8f72011-11-07 01:14:05 +0000201 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
202 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800203
Simon Glass5461acb2016-05-14 14:03:03 -0600204 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800205
206 return (n == blk_cnt) ? 0 : -1;
207}
208
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000209#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassc5951992017-08-03 12:22:17 -0600210static int env_mmc_load(void)
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000211{
212#if !defined(ENV_IS_EMBEDDED)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400213 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000214 u32 offset1, offset2;
215 int read1_fail = 0, read2_fail = 0;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000216 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100217 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700218 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000219
Markus Niebel452a2722013-10-04 15:48:03 +0200220 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
221 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
222
Tom Rinib9c8cca2014-03-28 12:03:34 -0400223 mmc = find_mmc_device(dev);
224
Tim Harveyc75648d2015-05-08 14:52:09 -0700225 errmsg = init_mmc_for_env(mmc);
226 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600227 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000228 goto err;
229 }
230
231 if (mmc_get_env_addr(mmc, 0, &offset1) ||
232 mmc_get_env_addr(mmc, 1, &offset2)) {
Simon Glassc5951992017-08-03 12:22:17 -0600233 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000234 goto fini;
235 }
236
237 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
238 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
239
240 if (read1_fail && read2_fail)
241 puts("*** Error - No Valid Environment Area found\n");
242 else if (read1_fail || read2_fail)
243 puts("*** Warning - some problems detected "
244 "reading environment; recovered successfully\n");
245
Fiach Antaw9d364af2017-01-25 18:53:12 +1000246 if (read1_fail && read2_fail) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700247 errmsg = "!bad CRC";
Simon Glassc5951992017-08-03 12:22:17 -0600248 ret = -EIO;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000249 goto fini;
Fiach Antaw9d364af2017-01-25 18:53:12 +1000250 } else if (!read1_fail && read2_fail) {
Simon Glass203e94f2017-08-03 12:21:56 -0600251 gd->env_valid = ENV_VALID;
Fiach Antaw9d364af2017-01-25 18:53:12 +1000252 env_import((char *)tmp_env1, 1);
253 } else if (read1_fail && !read2_fail) {
Simon Glass203e94f2017-08-03 12:21:56 -0600254 gd->env_valid = ENV_REDUND;
Fiach Antaw9d364af2017-01-25 18:53:12 +1000255 env_import((char *)tmp_env2, 1);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000256 } else {
Fiach Antaw9d364af2017-01-25 18:53:12 +1000257 env_import_redund((char *)tmp_env1, (char *)tmp_env2);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000258 }
259
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000260 ret = 0;
261
262fini:
263 fini_mmc_for_env(mmc);
264err:
265 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700266 set_default_env(errmsg);
Simon Glassc5951992017-08-03 12:22:17 -0600267
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000268#endif
Simon Glassc5951992017-08-03 12:22:17 -0600269 return ret;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000270}
271#else /* ! CONFIG_ENV_OFFSET_REDUND */
Simon Glassc5951992017-08-03 12:22:17 -0600272static int env_mmc_load(void)
Terry Lva8060352010-05-17 10:57:01 +0800273{
274#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400275 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400276 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000277 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000278 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100279 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700280 const char *errmsg;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400281
Tom Rinib9c8cca2014-03-28 12:03:34 -0400282 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800283
Tim Harveyc75648d2015-05-08 14:52:09 -0700284 errmsg = init_mmc_for_env(mmc);
285 if (errmsg) {
Simon Glassc5951992017-08-03 12:22:17 -0600286 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000287 goto err;
288 }
Terry Lva8060352010-05-17 10:57:01 +0800289
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000290 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glassc5951992017-08-03 12:22:17 -0600291 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000292 goto fini;
293 }
294
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400295 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700296 errmsg = "!read failed";
Simon Glassc5951992017-08-03 12:22:17 -0600297 ret = -EIO;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000298 goto fini;
299 }
Terry Lva8060352010-05-17 10:57:01 +0800300
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400301 env_import(buf, 1);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000302 ret = 0;
303
304fini:
305 fini_mmc_for_env(mmc);
306err:
307 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700308 set_default_env(errmsg);
Terry Lva8060352010-05-17 10:57:01 +0800309#endif
Simon Glassc5951992017-08-03 12:22:17 -0600310 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800311}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000312#endif /* CONFIG_ENV_OFFSET_REDUND */
Simon Glass4415f1d2017-08-03 12:21:58 -0600313
314U_BOOT_ENV_LOCATION(mmc) = {
315 .location = ENVL_MMC,
Simon Glassac358be2017-08-03 12:22:03 -0600316 ENV_NAME("MMC")
Simon Glasse5bce242017-08-03 12:22:01 -0600317 .load = env_mmc_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600318#ifndef CONFIG_SPL_BUILD
Simon Glasse5bce242017-08-03 12:22:01 -0600319 .save = env_save_ptr(env_mmc_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600320#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600321};