blob: 6c4ce2f05d83aff57e72c20ab86e198f7ce4b871 [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>
15#include <mmc.h>
Lei Wen6d1d51b2010-11-10 07:39:23 +080016#include <search.h>
Lei Wene79f4832010-10-13 11:07:21 +080017#include <errno.h>
Terry Lva8060352010-05-17 10:57:01 +080018
Michael Heimpoldd196bd82013-04-10 10:36:19 +000019#if defined(CONFIG_ENV_SIZE_REDUND) && \
20 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
21#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
22#endif
23
Terry Lva8060352010-05-17 10:57:01 +080024char *env_name_spec = "MMC";
25
26#ifdef ENV_IS_EMBEDDED
Igor Grinberg994bc672011-11-17 06:07:23 +000027env_t *env_ptr = &environment;
Terry Lva8060352010-05-17 10:57:01 +080028#else /* ! ENV_IS_EMBEDDED */
Igor Grinberge8db8f72011-11-07 01:14:05 +000029env_t *env_ptr;
Terry Lva8060352010-05-17 10:57:01 +080030#endif /* ENV_IS_EMBEDDED */
31
Terry Lva8060352010-05-17 10:57:01 +080032DECLARE_GLOBAL_DATA_PTR;
33
Mingkai Hu97039ab2011-01-24 17:09:55 +000034#if !defined(CONFIG_ENV_OFFSET)
35#define CONFIG_ENV_OFFSET 0
36#endif
37
Michael Heimpoldd196bd82013-04-10 10:36:19 +000038__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
Mingkai Hu97039ab2011-01-24 17:09:55 +000039{
Stephen Warren5c088ee2013-06-11 15:14:02 -060040 s64 offset;
41
42 offset = CONFIG_ENV_OFFSET;
Michael Heimpoldd196bd82013-04-10 10:36:19 +000043#ifdef CONFIG_ENV_OFFSET_REDUND
44 if (copy)
Stephen Warren5c088ee2013-06-11 15:14:02 -060045 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpoldd196bd82013-04-10 10:36:19 +000046#endif
Stephen Warren5c088ee2013-06-11 15:14:02 -060047
48 if (offset < 0)
49 offset += mmc->capacity;
50
51 *env_addr = offset;
52
Mingkai Hu97039ab2011-01-24 17:09:55 +000053 return 0;
54}
Mingkai Hu97039ab2011-01-24 17:09:55 +000055
Terry Lva8060352010-05-17 10:57:01 +080056int env_init(void)
57{
58 /* use default */
Igor Grinberge8db8f72011-11-07 01:14:05 +000059 gd->env_addr = (ulong)&default_environment[0];
60 gd->env_valid = 1;
Terry Lva8060352010-05-17 10:57:01 +080061
62 return 0;
63}
64
Tom Rinib9c8cca2014-03-28 12:03:34 -040065#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030066__weak uint mmc_get_env_part(struct mmc *mmc)
67{
68 return CONFIG_SYS_MMC_ENV_PART;
69}
70
71static int mmc_set_env_part(struct mmc *mmc)
72{
73 uint part = mmc_get_env_part(mmc);
Tom Rinib9c8cca2014-03-28 12:03:34 -040074 int dev = CONFIG_SYS_MMC_ENV_DEV;
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030075 int ret = 0;
Tom Rinib9c8cca2014-03-28 12:03:34 -040076
77#ifdef CONFIG_SPL_BUILD
78 dev = 0;
79#endif
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030080
81 if (part != mmc->part_num) {
82 ret = mmc_switch_part(dev, part);
83 if (ret)
84 puts("MMC partition switch failed\n");
85 }
86
87 return ret;
88}
89#else
90static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
Tom Rinib9c8cca2014-03-28 12:03:34 -040091#endif
92
Tim Harveyc75648d2015-05-08 14:52:09 -070093static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz6e7b7df2014-07-30 13:19:06 +030094{
Tim Harveyc75648d2015-05-08 14:52:09 -070095 if (!mmc)
96 return "No MMC card found";
Terry Lva8060352010-05-17 10:57:01 +080097
Tim Harveyc75648d2015-05-08 14:52:09 -070098 if (mmc_init(mmc))
99 return "MMC init failed";
Terry Lva8060352010-05-17 10:57:01 +0800100
Tim Harveyc75648d2015-05-08 14:52:09 -0700101 if (mmc_set_env_part(mmc))
102 return "MMC partition switch failed";
103
104 return NULL;
Terry Lva8060352010-05-17 10:57:01 +0800105}
106
Stephen Warren9404a5f2012-07-30 10:55:44 +0000107static void fini_mmc_for_env(struct mmc *mmc)
108{
109#ifdef CONFIG_SYS_MMC_ENV_PART
Tom Rinib9c8cca2014-03-28 12:03:34 -0400110 int dev = CONFIG_SYS_MMC_ENV_DEV;
111
112#ifdef CONFIG_SPL_BUILD
113 dev = 0;
114#endif
Peter Bigot19345d72014-09-03 12:22:43 -0500115 if (mmc_get_env_part(mmc) != mmc->part_num)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400116 mmc_switch_part(dev, mmc->part_num);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000117#endif
118}
119
Terry Lva8060352010-05-17 10:57:01 +0800120#ifdef CONFIG_CMD_SAVEENV
Igor Grinberge8db8f72011-11-07 01:14:05 +0000121static inline int write_env(struct mmc *mmc, unsigned long size,
122 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800123{
124 uint blk_start, blk_cnt, n;
125
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
129 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
130 blk_cnt, (u_char *)buffer);
131
132 return (n == blk_cnt) ? 0 : -1;
133}
134
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000135#ifdef CONFIG_ENV_OFFSET_REDUND
136static unsigned char env_flags;
137#endif
138
Terry Lva8060352010-05-17 10:57:01 +0800139int saveenv(void)
140{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400141 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Terry Lva8060352010-05-17 10:57:01 +0800142 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_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
169 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
170 CONFIG_SYS_MMC_ENV_DEV);
Stephen Warren4036b632012-05-24 11:38:33 +0000171 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800172 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000173 ret = 1;
174 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800175 }
176
177 puts("done\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000178 ret = 0;
179
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000180#ifdef CONFIG_ENV_OFFSET_REDUND
181 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
182#endif
183
Stephen Warren9404a5f2012-07-30 10:55:44 +0000184fini:
185 fini_mmc_for_env(mmc);
186 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800187}
188#endif /* CONFIG_CMD_SAVEENV */
189
Igor Grinberge8db8f72011-11-07 01:14:05 +0000190static inline int read_env(struct mmc *mmc, unsigned long size,
191 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800192{
193 uint blk_start, blk_cnt, n;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400194 int dev = CONFIG_SYS_MMC_ENV_DEV;
195
196#ifdef CONFIG_SPL_BUILD
197 dev = 0;
198#endif
Terry Lva8060352010-05-17 10:57:01 +0800199
Igor Grinberge8db8f72011-11-07 01:14:05 +0000200 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
201 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800202
Tom Rinib9c8cca2014-03-28 12:03:34 -0400203 n = mmc->block_dev.block_read(dev, blk_start, blk_cnt, (uchar *)buffer);
Terry Lva8060352010-05-17 10:57:01 +0800204
205 return (n == blk_cnt) ? 0 : -1;
206}
207
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000208#ifdef CONFIG_ENV_OFFSET_REDUND
209void env_relocate_spec(void)
210{
211#if !defined(ENV_IS_EMBEDDED)
Tom Rinib9c8cca2014-03-28 12:03:34 -0400212 struct mmc *mmc;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000213 u32 offset1, offset2;
214 int read1_fail = 0, read2_fail = 0;
215 int crc1_ok = 0, crc2_ok = 0;
Markus Niebel452a2722013-10-04 15:48:03 +0200216 env_t *ep;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000217 int ret;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400218 int dev = CONFIG_SYS_MMC_ENV_DEV;
Tim Harveyc75648d2015-05-08 14:52:09 -0700219 const char *errmsg = NULL;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000220
Markus Niebel452a2722013-10-04 15:48:03 +0200221 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
222 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
223
Tom Rinib9c8cca2014-03-28 12:03:34 -0400224#ifdef CONFIG_SPL_BUILD
225 dev = 0;
226#endif
227
228 mmc = find_mmc_device(dev);
229
Tim Harveyc75648d2015-05-08 14:52:09 -0700230 errmsg = init_mmc_for_env(mmc);
231 if (errmsg) {
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000232 ret = 1;
233 goto err;
234 }
235
236 if (mmc_get_env_addr(mmc, 0, &offset1) ||
237 mmc_get_env_addr(mmc, 1, &offset2)) {
238 ret = 1;
239 goto fini;
240 }
241
242 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
243 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
244
245 if (read1_fail && read2_fail)
246 puts("*** Error - No Valid Environment Area found\n");
247 else if (read1_fail || read2_fail)
248 puts("*** Warning - some problems detected "
249 "reading environment; recovered successfully\n");
250
251 crc1_ok = !read1_fail &&
252 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
253 crc2_ok = !read2_fail &&
254 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
255
256 if (!crc1_ok && !crc2_ok) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700257 errmsg = "!bad CRC";
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000258 ret = 1;
259 goto fini;
260 } else if (crc1_ok && !crc2_ok) {
261 gd->env_valid = 1;
262 } else if (!crc1_ok && crc2_ok) {
263 gd->env_valid = 2;
264 } else {
265 /* both ok - check serial */
266 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
267 gd->env_valid = 2;
268 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
269 gd->env_valid = 1;
270 else if (tmp_env1->flags > tmp_env2->flags)
271 gd->env_valid = 1;
272 else if (tmp_env2->flags > tmp_env1->flags)
273 gd->env_valid = 2;
274 else /* flags are equal - almost impossible */
275 gd->env_valid = 1;
276 }
277
278 free(env_ptr);
279
280 if (gd->env_valid == 1)
281 ep = tmp_env1;
282 else
283 ep = tmp_env2;
284
285 env_flags = ep->flags;
286 env_import((char *)ep, 0);
287 ret = 0;
288
289fini:
290 fini_mmc_for_env(mmc);
291err:
292 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700293 set_default_env(errmsg);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000294#endif
295}
296#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lva8060352010-05-17 10:57:01 +0800297void env_relocate_spec(void)
298{
299#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400300 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinib9c8cca2014-03-28 12:03:34 -0400301 struct mmc *mmc;
Mingkai Hu97039ab2011-01-24 17:09:55 +0000302 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000303 int ret;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400304 int dev = CONFIG_SYS_MMC_ENV_DEV;
Tim Harveyc75648d2015-05-08 14:52:09 -0700305 const char *errmsg;
Tom Rinib9c8cca2014-03-28 12:03:34 -0400306
307#ifdef CONFIG_SPL_BUILD
308 dev = 0;
309#endif
310
311 mmc = find_mmc_device(dev);
Terry Lva8060352010-05-17 10:57:01 +0800312
Tim Harveyc75648d2015-05-08 14:52:09 -0700313 errmsg = init_mmc_for_env(mmc);
314 if (errmsg) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000315 ret = 1;
316 goto err;
317 }
Terry Lva8060352010-05-17 10:57:01 +0800318
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000319 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000320 ret = 1;
321 goto fini;
322 }
323
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400324 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyc75648d2015-05-08 14:52:09 -0700325 errmsg = "!read failed";
Stephen Warren9404a5f2012-07-30 10:55:44 +0000326 ret = 1;
327 goto fini;
328 }
Terry Lva8060352010-05-17 10:57:01 +0800329
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400330 env_import(buf, 1);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000331 ret = 0;
332
333fini:
334 fini_mmc_for_env(mmc);
335err:
336 if (ret)
Tim Harveyc75648d2015-05-08 14:52:09 -0700337 set_default_env(errmsg);
Terry Lva8060352010-05-17 10:57:01 +0800338#endif
339}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000340#endif /* CONFIG_ENV_OFFSET_REDUND */