blob: a5d14d448c8f56fff3afcc64d405ab4486fbe031 [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>
13#include <linux/stddef.h>
14#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060015#include <memalign.h>
Terry Lva8060352010-05-17 10:57:01 +080016#include <mmc.h>
Lei Wen6d1d51b2010-11-10 07:39:23 +080017#include <search.h>
Lei Wene79f4832010-10-13 11:07:21 +080018#include <errno.h>
Terry Lva8060352010-05-17 10:57:01 +080019
Michael Heimpoldd196bd82013-04-10 10:36:19 +000020#if defined(CONFIG_ENV_SIZE_REDUND) && \
21 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
22#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
23#endif
24
Terry Lva8060352010-05-17 10:57:01 +080025char *env_name_spec = "MMC";
26
27#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000028env_t *env_ptr = &environment;
Terry Lva8060352010-05-17 10:57:01 +080029#else /* ! ENV_IS_EMBEDDED */
Igor Grinberge8db8f72011-11-07 01:14:05 +000030env_t *env_ptr;
Terry Lva8060352010-05-17 10:57:01 +080031#endif /* ENV_IS_EMBEDDED */
32
Terry Lva8060352010-05-17 10:57:01 +080033DECLARE_GLOBAL_DATA_PTR;
34
Mingkai Hu97039ab2011-01-24 17:09:55 +000035#if !defined(CONFIG_ENV_OFFSET)
36#define CONFIG_ENV_OFFSET 0
37#endif
38
Michael Heimpoldd196bd82013-04-10 10:36:19 +000039__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
Mingkai Hu97039ab2011-01-24 17:09:55 +000040{
Stephen Warren5c088ee2013-06-11 15:14:02 -060041 s64 offset;
42
43 offset = CONFIG_ENV_OFFSET;
Michael Heimpoldd196bd82013-04-10 10:36:19 +000044#ifdef CONFIG_ENV_OFFSET_REDUND
45 if (copy)
Stephen Warren5c088ee2013-06-11 15:14:02 -060046 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +000047#endif
Stephen Warren5c088ee2013-06-11 15:14:02 -060048
49 if (offset < 0)
50 offset += mmc->capacity;
51
52 *env_addr = offset;
53
Mingkai Hu97039ab2011-01-24 17:09:55 +000054 return 0;
55}
Mingkai Hu97039ab2011-01-24 17:09:55 +000056
Clemens Grubere92029c2016-01-20 15:43:37 +010057__weak int mmc_get_env_dev(void)
58{
59 return CONFIG_SYS_MMC_ENV_DEV;
60}
61
Terry Lva8060352010-05-17 10:57:01 +080062int env_init(void)
63{
64 /* use default */
Igor Grinberge8db8f72011-11-07 01:14:05 +000065 gd->env_addr = (ulong)&default_environment[0];
66 gd->env_valid = 1;
Terry Lva8060352010-05-17 10:57:01 +080067
68 return 0;
69}
70
Tom Rinib9c8cca2014-03-28 12:03:34 -040071#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030072__weak uint mmc_get_env_part(struct mmc *mmc)
73{
74 return CONFIG_SYS_MMC_ENV_PART;
75}
76
Stephen Warren873cc1d2015-12-07 11:38:49 -070077static unsigned char env_mmc_orig_hwpart;
78
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030079static int mmc_set_env_part(struct mmc *mmc)
80{
81 uint part = mmc_get_env_part(mmc);
Clemens Grubere92029c2016-01-20 15:43:37 +010082 int dev = mmc_get_env_dev();
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030083 int ret = 0;
Tom Rinib9c8cca2014-03-28 12:03:34 -040084
Simon Glass69f45cd2016-05-01 13:52:29 -060085 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
86 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren873cc1d2015-12-07 11:38:49 -070087 if (ret)
88 puts("MMC partition switch failed\n");
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030089
90 return ret;
91}
92#else
93static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
Tom Rinib9c8cca2014-03-28 12:03:34 -040094#endif
95
Tim Harveyc75648d2015-05-08 14:52:09 -070096static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030097{
Tim Harveyc75648d2015-05-08 14:52:09 -070098 if (!mmc)
Hans de Goedea85da212015-08-15 20:05:01 +020099 return "!No MMC card found";
Terry Lva8060352010-05-17 10:57:01 +0800100
Tim Harveyc75648d2015-05-08 14:52:09 -0700101 if (mmc_init(mmc))
Hans de Goedea85da212015-08-15 20:05:01 +0200102 return "!MMC init failed";
Terry Lva8060352010-05-17 10:57:01 +0800103
Tim Harveyc75648d2015-05-08 14:52:09 -0700104 if (mmc_set_env_part(mmc))
Hans de Goedea85da212015-08-15 20:05:01 +0200105 return "!MMC partition switch failed";
Tim Harveyc75648d2015-05-08 14:52:09 -0700106
107 return NULL;
Terry Lva8060352010-05-17 10:57:01 +0800108}
109
Stephen Warren9404a5f2012-07-30 10:55:44 +0000110static void fini_mmc_for_env(struct mmc *mmc)
111{
112#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Grubere92029c2016-01-20 15:43:37 +0100113 int dev = mmc_get_env_dev();
Tom Rinib9c8cca2014-03-28 12:03:34 -0400114
Simon Glass69f45cd2016-05-01 13:52:29 -0600115 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000116#endif
117}
118
Terry Lva8060352010-05-17 10:57:01 +0800119#ifdef CONFIG_CMD_SAVEENV
Igor Grinberge8db8f72011-11-07 01:14:05 +0000120static inline int write_env(struct mmc *mmc, unsigned long size,
121 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800122{
123 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600124 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800125
Igor Grinberge8db8f72011-11-07 01:14:05 +0000126 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
127 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800128
Simon Glass5461acb2016-05-14 14:03:03 -0600129 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800130
131 return (n == blk_cnt) ? 0 : -1;
132}
133
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000134#ifdef CONFIG_ENV_OFFSET_REDUND
135static unsigned char env_flags;
136#endif
137
Terry Lva8060352010-05-17 10:57:01 +0800138int saveenv(void)
139{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400140 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Grubere92029c2016-01-20 15:43:37 +0100141 int dev = mmc_get_env_dev();
142 struct mmc *mmc = find_mmc_device(dev);
Igor Grinberge8db8f72011-11-07 01:14:05 +0000143 u32 offset;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000144 int ret, copy = 0;
Tim Harveyc75648d2015-05-08 14:52:09 -0700145 const char *errmsg;
Terry Lva8060352010-05-17 10:57:01 +0800146
Tim Harveyc75648d2015-05-08 14:52:09 -0700147 errmsg = init_mmc_for_env(mmc);
148 if (errmsg) {
149 printf("%s\n", errmsg);
Mingkai Hu97039ab2011-01-24 17:09:55 +0000150 return 1;
Tim Harveyc75648d2015-05-08 14:52:09 -0700151 }
Mingkai Hu97039ab2011-01-24 17:09:55 +0000152
Marek Vasut7ce15262014-03-05 19:59:50 +0100153 ret = env_export(env_new);
154 if (ret)
Stephen Warren9404a5f2012-07-30 10:55:44 +0000155 goto fini;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000156
157#ifdef CONFIG_ENV_OFFSET_REDUND
158 env_new->flags = ++env_flags; /* increase the serial */
159
160 if (gd->env_valid == 1)
161 copy = 1;
162#endif
163
164 if (mmc_get_env_addr(mmc, copy, &offset)) {
165 ret = 1;
166 goto fini;
167 }
168
Clemens Grubere92029c2016-01-20 15:43:37 +0100169 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren4036b632012-05-24 11:38:33 +0000170 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800171 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000172 ret = 1;
173 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800174 }
175
176 puts("done\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000177 ret = 0;
178
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000179#ifdef CONFIG_ENV_OFFSET_REDUND
180 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
181#endif
182
Stephen Warren9404a5f2012-07-30 10:55:44 +0000183fini:
184 fini_mmc_for_env(mmc);
185 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800186}
187#endif /* CONFIG_CMD_SAVEENV */
188
Igor Grinberge8db8f72011-11-07 01:14:05 +0000189static inline int read_env(struct mmc *mmc, unsigned long size,
190 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800191{
192 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600193 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800194
Igor Grinberge8db8f72011-11-07 01:14:05 +0000195 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
196 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800197
Simon Glass5461acb2016-05-14 14:03:03 -0600198 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800199
200 return (n == blk_cnt) ? 0 : -1;
201}
202
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000203#ifdef CONFIG_ENV_OFFSET_REDUND
204void env_relocate_spec(void)
205{
206#if !defined(ENV_IS_EMBEDDED)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400207 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000208 u32 offset1, offset2;
209 int read1_fail = 0, read2_fail = 0;
210 int crc1_ok = 0, crc2_ok = 0;
Markus Niebel452a2722013-10-04 15:48:03 +0200211 env_t *ep;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000212 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100213 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700214 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000215
Markus Niebel452a2722013-10-04 15:48:03 +0200216 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
217 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
218
Tom Rinib9c8cca2014-03-28 12:03:34 -0400219 mmc = find_mmc_device(dev);
220
Tim Harveyc75648d2015-05-08 14:52:09 -0700221 errmsg = init_mmc_for_env(mmc);
222 if (errmsg) {
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000223 ret = 1;
224 goto err;
225 }
226
227 if (mmc_get_env_addr(mmc, 0, &offset1) ||
228 mmc_get_env_addr(mmc, 1, &offset2)) {
229 ret = 1;
230 goto fini;
231 }
232
233 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
234 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
235
236 if (read1_fail && read2_fail)
237 puts("*** Error - No Valid Environment Area found\n");
238 else if (read1_fail || read2_fail)
239 puts("*** Warning - some problems detected "
240 "reading environment; recovered successfully\n");
241
242 crc1_ok = !read1_fail &&
243 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
244 crc2_ok = !read2_fail &&
245 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
246
247 if (!crc1_ok && !crc2_ok) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700248 errmsg = "!bad CRC";
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000249 ret = 1;
250 goto fini;
251 } else if (crc1_ok && !crc2_ok) {
252 gd->env_valid = 1;
253 } else if (!crc1_ok && crc2_ok) {
254 gd->env_valid = 2;
255 } else {
256 /* both ok - check serial */
257 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
258 gd->env_valid = 2;
259 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
260 gd->env_valid = 1;
261 else if (tmp_env1->flags > tmp_env2->flags)
262 gd->env_valid = 1;
263 else if (tmp_env2->flags > tmp_env1->flags)
264 gd->env_valid = 2;
265 else /* flags are equal - almost impossible */
266 gd->env_valid = 1;
267 }
268
269 free(env_ptr);
270
271 if (gd->env_valid == 1)
272 ep = tmp_env1;
273 else
274 ep = tmp_env2;
275
276 env_flags = ep->flags;
277 env_import((char *)ep, 0);
278 ret = 0;
279
280fini:
281 fini_mmc_for_env(mmc);
282err:
283 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700284 set_default_env(errmsg);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000285#endif
286}
287#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lva8060352010-05-17 10:57:01 +0800288void env_relocate_spec(void)
289{
290#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400291 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400292 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000293 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000294 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100295 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700296 const char *errmsg;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400297
Tom Rinib9c8cca2014-03-28 12:03:34 -0400298 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800299
Tim Harveyc75648d2015-05-08 14:52:09 -0700300 errmsg = init_mmc_for_env(mmc);
301 if (errmsg) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000302 ret = 1;
303 goto err;
304 }
Terry Lva8060352010-05-17 10:57:01 +0800305
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000306 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000307 ret = 1;
308 goto fini;
309 }
310
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400311 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700312 errmsg = "!read failed";
Stephen Warren9404a5f2012-07-30 10:55:44 +0000313 ret = 1;
314 goto fini;
315 }
Terry Lva8060352010-05-17 10:57:01 +0800316
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400317 env_import(buf, 1);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000318 ret = 0;
319
320fini:
321 fini_mmc_for_env(mmc);
322err:
323 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700324 set_default_env(errmsg);
Terry Lva8060352010-05-17 10:57:01 +0800325#endif
326}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000327#endif /* CONFIG_ENV_OFFSET_REDUND */