blob: 88b043ec35b1bd644dd5ae85aa42ab88ba30f090 [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 +080026char *env_name_spec = "MMC";
27
28#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000029env_t *env_ptr = &environment;
Terry Lva8060352010-05-17 10:57:01 +080030#else /* ! ENV_IS_EMBEDDED */
Igor Grinberge8db8f72011-11-07 01:14:05 +000031env_t *env_ptr;
Terry Lva8060352010-05-17 10:57:01 +080032#endif /* ENV_IS_EMBEDDED */
33
Terry Lva8060352010-05-17 10:57:01 +080034DECLARE_GLOBAL_DATA_PTR;
35
Mingkai Hu97039ab2011-01-24 17:09:55 +000036#if !defined(CONFIG_ENV_OFFSET)
37#define CONFIG_ENV_OFFSET 0
38#endif
39
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020040#if CONFIG_IS_ENABLED(OF_CONTROL)
41static inline s64 mmc_offset(int copy)
Mingkai Hu97039ab2011-01-24 17:09:55 +000042{
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020043 const char *propname = "u-boot,mmc-env-offset";
44 s64 defvalue = CONFIG_ENV_OFFSET;
Stephen Warren5c088ee2013-06-11 15:14:02 -060045
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020046#if defined(CONFIG_ENV_OFFSET_REDUND)
47 if (copy) {
48 propname = "u-boot,mmc-env-offset-redundant";
49 defvalue = CONFIG_ENV_OFFSET_REDUND;
50 }
51#endif
52
53 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
54}
55#else
56static inline s64 mmc_offset(int copy)
57{
58 s64 offset = CONFIG_ENV_OFFSET;
59
60#if defined(CONFIG_ENV_OFFSET_REDUND)
Michael Heimpoldd196bd82013-04-10 10:36:19 +000061 if (copy)
Stephen Warren5c088ee2013-06-11 15:14:02 -060062 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +000063#endif
Philipp Tomsichf8b8a552017-05-16 00:16:31 +020064 return offset;
65}
66#endif
67
68__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
69{
70 s64 offset = mmc_offset(copy);
Stephen Warren5c088ee2013-06-11 15:14:02 -060071
72 if (offset < 0)
73 offset += mmc->capacity;
74
75 *env_addr = offset;
76
Mingkai Hu97039ab2011-01-24 17:09:55 +000077 return 0;
78}
Mingkai Hu97039ab2011-01-24 17:09:55 +000079
Clemens Grubere92029c2016-01-20 15:43:37 +010080__weak int mmc_get_env_dev(void)
81{
82 return CONFIG_SYS_MMC_ENV_DEV;
83}
84
Terry Lva8060352010-05-17 10:57:01 +080085int env_init(void)
86{
87 /* use default */
Igor Grinberge8db8f72011-11-07 01:14:05 +000088 gd->env_addr = (ulong)&default_environment[0];
89 gd->env_valid = 1;
Terry Lva8060352010-05-17 10:57:01 +080090
91 return 0;
92}
93
Tom Rinib9c8cca2014-03-28 12:03:34 -040094#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030095__weak uint mmc_get_env_part(struct mmc *mmc)
96{
97 return CONFIG_SYS_MMC_ENV_PART;
98}
99
Stephen Warren873cc1d2015-12-07 11:38:49 -0700100static unsigned char env_mmc_orig_hwpart;
101
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300102static int mmc_set_env_part(struct mmc *mmc)
103{
104 uint part = mmc_get_env_part(mmc);
Clemens Grubere92029c2016-01-20 15:43:37 +0100105 int dev = mmc_get_env_dev();
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300106 int ret = 0;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400107
Simon Glass69f45cd2016-05-01 13:52:29 -0600108 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
109 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren873cc1d2015-12-07 11:38:49 -0700110 if (ret)
111 puts("MMC partition switch failed\n");
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300112
113 return ret;
114}
115#else
116static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
Tom Rinib9c8cca2014-03-28 12:03:34 -0400117#endif
118
Tim Harveyc75648d2015-05-08 14:52:09 -0700119static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +0300120{
Tim Harveyc75648d2015-05-08 14:52:09 -0700121 if (!mmc)
Hans de Goedea85da212015-08-15 20:05:01 +0200122 return "!No MMC card found";
Terry Lva8060352010-05-17 10:57:01 +0800123
Simon Glass01b73fe2017-05-27 11:37:18 -0600124#ifdef CONFIG_BLK
125 struct udevice *dev;
126
127 if (blk_get_from_parent(mmc->dev, &dev))
128 return "!No block device";
129#else
Tim Harveyc75648d2015-05-08 14:52:09 -0700130 if (mmc_init(mmc))
Hans de Goedea85da212015-08-15 20:05:01 +0200131 return "!MMC init failed";
Simon Glasse7017a32017-04-23 20:02:04 -0600132#endif
Tim Harveyc75648d2015-05-08 14:52:09 -0700133 if (mmc_set_env_part(mmc))
Hans de Goedea85da212015-08-15 20:05:01 +0200134 return "!MMC partition switch failed";
Tim Harveyc75648d2015-05-08 14:52:09 -0700135
136 return NULL;
Terry Lva8060352010-05-17 10:57:01 +0800137}
138
Stephen Warren9404a5f2012-07-30 10:55:44 +0000139static void fini_mmc_for_env(struct mmc *mmc)
140{
141#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Grubere92029c2016-01-20 15:43:37 +0100142 int dev = mmc_get_env_dev();
Tom Rinib9c8cca2014-03-28 12:03:34 -0400143
Simon Glass69f45cd2016-05-01 13:52:29 -0600144 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000145#endif
146}
147
Terry Lva8060352010-05-17 10:57:01 +0800148#ifdef CONFIG_CMD_SAVEENV
Igor Grinberge8db8f72011-11-07 01:14:05 +0000149static inline int write_env(struct mmc *mmc, unsigned long size,
150 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800151{
152 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600153 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800154
Igor Grinberge8db8f72011-11-07 01:14:05 +0000155 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
156 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800157
Simon Glass5461acb2016-05-14 14:03:03 -0600158 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800159
160 return (n == blk_cnt) ? 0 : -1;
161}
162
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000163#ifdef CONFIG_ENV_OFFSET_REDUND
164static unsigned char env_flags;
165#endif
166
Terry Lva8060352010-05-17 10:57:01 +0800167int saveenv(void)
168{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400169 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Grubere92029c2016-01-20 15:43:37 +0100170 int dev = mmc_get_env_dev();
171 struct mmc *mmc = find_mmc_device(dev);
Igor Grinberge8db8f72011-11-07 01:14:05 +0000172 u32 offset;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000173 int ret, copy = 0;
Tim Harveyc75648d2015-05-08 14:52:09 -0700174 const char *errmsg;
Terry Lva8060352010-05-17 10:57:01 +0800175
Tim Harveyc75648d2015-05-08 14:52:09 -0700176 errmsg = init_mmc_for_env(mmc);
177 if (errmsg) {
178 printf("%s\n", errmsg);
Mingkai Hu97039ab2011-01-24 17:09:55 +0000179 return 1;
Tim Harveyc75648d2015-05-08 14:52:09 -0700180 }
Mingkai Hu97039ab2011-01-24 17:09:55 +0000181
Marek Vasut7ce15262014-03-05 19:59:50 +0100182 ret = env_export(env_new);
183 if (ret)
Stephen Warren9404a5f2012-07-30 10:55:44 +0000184 goto fini;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000185
186#ifdef CONFIG_ENV_OFFSET_REDUND
187 env_new->flags = ++env_flags; /* increase the serial */
188
189 if (gd->env_valid == 1)
190 copy = 1;
191#endif
192
193 if (mmc_get_env_addr(mmc, copy, &offset)) {
194 ret = 1;
195 goto fini;
196 }
197
Clemens Grubere92029c2016-01-20 15:43:37 +0100198 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren4036b632012-05-24 11:38:33 +0000199 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800200 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000201 ret = 1;
202 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800203 }
204
205 puts("done\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000206 ret = 0;
207
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000208#ifdef CONFIG_ENV_OFFSET_REDUND
209 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
210#endif
211
Stephen Warren9404a5f2012-07-30 10:55:44 +0000212fini:
213 fini_mmc_for_env(mmc);
214 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800215}
216#endif /* CONFIG_CMD_SAVEENV */
217
Igor Grinberge8db8f72011-11-07 01:14:05 +0000218static inline int read_env(struct mmc *mmc, unsigned long size,
219 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800220{
221 uint blk_start, blk_cnt, n;
Simon Glass5461acb2016-05-14 14:03:03 -0600222 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lva8060352010-05-17 10:57:01 +0800223
Igor Grinberge8db8f72011-11-07 01:14:05 +0000224 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
225 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800226
Simon Glass5461acb2016-05-14 14:03:03 -0600227 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800228
229 return (n == blk_cnt) ? 0 : -1;
230}
231
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000232#ifdef CONFIG_ENV_OFFSET_REDUND
233void env_relocate_spec(void)
234{
235#if !defined(ENV_IS_EMBEDDED)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400236 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000237 u32 offset1, offset2;
238 int read1_fail = 0, read2_fail = 0;
239 int crc1_ok = 0, crc2_ok = 0;
Markus Niebel452a2722013-10-04 15:48:03 +0200240 env_t *ep;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000241 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100242 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700243 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000244
Markus Niebel452a2722013-10-04 15:48:03 +0200245 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
246 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
247
Tom Rinib9c8cca2014-03-28 12:03:34 -0400248 mmc = find_mmc_device(dev);
249
Tim Harveyc75648d2015-05-08 14:52:09 -0700250 errmsg = init_mmc_for_env(mmc);
251 if (errmsg) {
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000252 ret = 1;
253 goto err;
254 }
255
256 if (mmc_get_env_addr(mmc, 0, &offset1) ||
257 mmc_get_env_addr(mmc, 1, &offset2)) {
258 ret = 1;
259 goto fini;
260 }
261
262 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
263 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
264
265 if (read1_fail && read2_fail)
266 puts("*** Error - No Valid Environment Area found\n");
267 else if (read1_fail || read2_fail)
268 puts("*** Warning - some problems detected "
269 "reading environment; recovered successfully\n");
270
271 crc1_ok = !read1_fail &&
272 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
273 crc2_ok = !read2_fail &&
274 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
275
276 if (!crc1_ok && !crc2_ok) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700277 errmsg = "!bad CRC";
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000278 ret = 1;
279 goto fini;
280 } else if (crc1_ok && !crc2_ok) {
281 gd->env_valid = 1;
282 } else if (!crc1_ok && crc2_ok) {
283 gd->env_valid = 2;
284 } else {
285 /* both ok - check serial */
286 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
287 gd->env_valid = 2;
288 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
289 gd->env_valid = 1;
290 else if (tmp_env1->flags > tmp_env2->flags)
291 gd->env_valid = 1;
292 else if (tmp_env2->flags > tmp_env1->flags)
293 gd->env_valid = 2;
294 else /* flags are equal - almost impossible */
295 gd->env_valid = 1;
296 }
297
298 free(env_ptr);
299
300 if (gd->env_valid == 1)
301 ep = tmp_env1;
302 else
303 ep = tmp_env2;
304
305 env_flags = ep->flags;
306 env_import((char *)ep, 0);
307 ret = 0;
308
309fini:
310 fini_mmc_for_env(mmc);
311err:
312 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700313 set_default_env(errmsg);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000314#endif
315}
316#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lva8060352010-05-17 10:57:01 +0800317void env_relocate_spec(void)
318{
319#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400320 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400321 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000322 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000323 int ret;
Clemens Grubere92029c2016-01-20 15:43:37 +0100324 int dev = mmc_get_env_dev();
Tim Harveyc75648d2015-05-08 14:52:09 -0700325 const char *errmsg;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400326
Tom Rinib9c8cca2014-03-28 12:03:34 -0400327 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800328
Tim Harveyc75648d2015-05-08 14:52:09 -0700329 errmsg = init_mmc_for_env(mmc);
330 if (errmsg) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000331 ret = 1;
332 goto err;
333 }
Terry Lva8060352010-05-17 10:57:01 +0800334
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000335 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000336 ret = 1;
337 goto fini;
338 }
339
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400340 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700341 errmsg = "!read failed";
Stephen Warren9404a5f2012-07-30 10:55:44 +0000342 ret = 1;
343 goto fini;
344 }
Terry Lva8060352010-05-17 10:57:01 +0800345
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400346 env_import(buf, 1);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000347 ret = 0;
348
349fini:
350 fini_mmc_for_env(mmc);
351err:
352 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700353 set_default_env(errmsg);
Terry Lva8060352010-05-17 10:57:01 +0800354#endif
355}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000356#endif /* CONFIG_ENV_OFFSET_REDUND */