blob: 14648e37bdf284ccb683740b5ce3d05df47e3779 [file] [log] [blame]
Terry Lvb4eceac2010-05-17 10:57:01 +08001/*
Mingkai Hua55fce32011-01-24 17:09:55 +00002 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
Terry Lvb4eceac2010-05-17 10:57:01 +08003 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02004 * SPDX-License-Identifier: GPL-2.0+
Terry Lvb4eceac2010-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 Wen9f84f212010-11-10 07:39:23 +080016#include <search.h>
Lei Wenff833ed2010-10-13 11:07:21 +080017#include <errno.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080018
Michael Heimpolda1d25a32013-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 Lvb4eceac2010-05-17 10:57:01 +080024char *env_name_spec = "MMC";
25
26#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000027env_t *env_ptr = &environment;
Terry Lvb4eceac2010-05-17 10:57:01 +080028#else /* ! ENV_IS_EMBEDDED */
Igor Grinbergecbfb622011-11-07 01:14:05 +000029env_t *env_ptr;
Terry Lvb4eceac2010-05-17 10:57:01 +080030#endif /* ENV_IS_EMBEDDED */
31
Terry Lvb4eceac2010-05-17 10:57:01 +080032DECLARE_GLOBAL_DATA_PTR;
33
Mingkai Hua55fce32011-01-24 17:09:55 +000034#if !defined(CONFIG_ENV_OFFSET)
35#define CONFIG_ENV_OFFSET 0
36#endif
37
Michael Heimpolda1d25a32013-04-10 10:36:19 +000038__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
Mingkai Hua55fce32011-01-24 17:09:55 +000039{
Stephen Warren24dc2032013-06-11 15:14:02 -060040 s64 offset;
41
42 offset = CONFIG_ENV_OFFSET;
Michael Heimpolda1d25a32013-04-10 10:36:19 +000043#ifdef CONFIG_ENV_OFFSET_REDUND
44 if (copy)
Stephen Warren24dc2032013-06-11 15:14:02 -060045 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpolda1d25a32013-04-10 10:36:19 +000046#endif
Stephen Warren24dc2032013-06-11 15:14:02 -060047
48 if (offset < 0)
49 offset += mmc->capacity;
50
51 *env_addr = offset;
52
Mingkai Hua55fce32011-01-24 17:09:55 +000053 return 0;
54}
Mingkai Hua55fce32011-01-24 17:09:55 +000055
Terry Lvb4eceac2010-05-17 10:57:01 +080056int env_init(void)
57{
58 /* use default */
Igor Grinbergecbfb622011-11-07 01:14:05 +000059 gd->env_addr = (ulong)&default_environment[0];
60 gd->env_valid = 1;
Terry Lvb4eceac2010-05-17 10:57:01 +080061
62 return 0;
63}
64
Tom Rinic929f822014-03-28 12:03:34 -040065#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz97d2fb92014-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 Rinic929f822014-03-28 12:03:34 -040074 int dev = CONFIG_SYS_MMC_ENV_DEV;
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030075 int ret = 0;
Tom Rinic929f822014-03-28 12:03:34 -040076
77#ifdef CONFIG_SPL_BUILD
78 dev = 0;
79#endif
Dmitry Lifshitz97d2fb92014-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 Rinic929f822014-03-28 12:03:34 -040091#endif
92
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030093static int init_mmc_for_env(struct mmc *mmc)
94{
Terry Lvb4eceac2010-05-17 10:57:01 +080095 if (!mmc) {
96 puts("No MMC card found\n");
97 return -1;
98 }
99
100 if (mmc_init(mmc)) {
101 puts("MMC init failed\n");
Igor Grinbergecbfb622011-11-07 01:14:05 +0000102 return -1;
Terry Lvb4eceac2010-05-17 10:57:01 +0800103 }
Stephen Warren27ee0f32012-07-30 10:55:44 +0000104
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300105 return mmc_set_env_part(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800106}
107
Stephen Warren27ee0f32012-07-30 10:55:44 +0000108static void fini_mmc_for_env(struct mmc *mmc)
109{
110#ifdef CONFIG_SYS_MMC_ENV_PART
Tom Rinic929f822014-03-28 12:03:34 -0400111 int dev = CONFIG_SYS_MMC_ENV_DEV;
112
113#ifdef CONFIG_SPL_BUILD
114 dev = 0;
115#endif
Peter Bigot3a4f6562014-09-03 12:22:43 -0500116 if (mmc_get_env_part(mmc) != mmc->part_num)
Tom Rinic929f822014-03-28 12:03:34 -0400117 mmc_switch_part(dev, mmc->part_num);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000118#endif
119}
120
Terry Lvb4eceac2010-05-17 10:57:01 +0800121#ifdef CONFIG_CMD_SAVEENV
Igor Grinbergecbfb622011-11-07 01:14:05 +0000122static inline int write_env(struct mmc *mmc, unsigned long size,
123 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800124{
125 uint blk_start, blk_cnt, n;
126
Igor Grinbergecbfb622011-11-07 01:14:05 +0000127 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
128 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800129
130 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
131 blk_cnt, (u_char *)buffer);
132
133 return (n == blk_cnt) ? 0 : -1;
134}
135
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000136#ifdef CONFIG_ENV_OFFSET_REDUND
137static unsigned char env_flags;
138#endif
139
Terry Lvb4eceac2010-05-17 10:57:01 +0800140int saveenv(void)
141{
Tom Rinia840b8e2013-04-05 14:55:21 -0400142 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Terry Lvb4eceac2010-05-17 10:57:01 +0800143 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000144 u32 offset;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000145 int ret, copy = 0;
Terry Lvb4eceac2010-05-17 10:57:01 +0800146
Stephen Warren27ee0f32012-07-30 10:55:44 +0000147 if (init_mmc_for_env(mmc))
Terry Lvb4eceac2010-05-17 10:57:01 +0800148 return 1;
149
Marek Vasutd73c1292014-03-05 19:59:50 +0100150 ret = env_export(env_new);
151 if (ret)
Stephen Warren27ee0f32012-07-30 10:55:44 +0000152 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000153
154#ifdef CONFIG_ENV_OFFSET_REDUND
155 env_new->flags = ++env_flags; /* increase the serial */
156
157 if (gd->env_valid == 1)
158 copy = 1;
159#endif
160
161 if (mmc_get_env_addr(mmc, copy, &offset)) {
162 ret = 1;
163 goto fini;
164 }
165
166 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
167 CONFIG_SYS_MMC_ENV_DEV);
Stephen Warren42a3c442012-05-24 11:38:33 +0000168 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800169 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000170 ret = 1;
171 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800172 }
173
174 puts("done\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000175 ret = 0;
176
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000177#ifdef CONFIG_ENV_OFFSET_REDUND
178 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
179#endif
180
Stephen Warren27ee0f32012-07-30 10:55:44 +0000181fini:
182 fini_mmc_for_env(mmc);
183 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800184}
185#endif /* CONFIG_CMD_SAVEENV */
186
Igor Grinbergecbfb622011-11-07 01:14:05 +0000187static inline int read_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;
Tom Rinic929f822014-03-28 12:03:34 -0400191 int dev = CONFIG_SYS_MMC_ENV_DEV;
192
193#ifdef CONFIG_SPL_BUILD
194 dev = 0;
195#endif
Terry Lvb4eceac2010-05-17 10:57:01 +0800196
Igor Grinbergecbfb622011-11-07 01:14:05 +0000197 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
198 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800199
Tom Rinic929f822014-03-28 12:03:34 -0400200 n = mmc->block_dev.block_read(dev, blk_start, blk_cnt, (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800201
202 return (n == blk_cnt) ? 0 : -1;
203}
204
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000205#ifdef CONFIG_ENV_OFFSET_REDUND
206void env_relocate_spec(void)
207{
208#if !defined(ENV_IS_EMBEDDED)
Tom Rinic929f822014-03-28 12:03:34 -0400209 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000210 u32 offset1, offset2;
211 int read1_fail = 0, read2_fail = 0;
212 int crc1_ok = 0, crc2_ok = 0;
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200213 env_t *ep;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000214 int ret;
Tom Rinic929f822014-03-28 12:03:34 -0400215 int dev = CONFIG_SYS_MMC_ENV_DEV;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000216
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200217 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
218 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
219
Tom Rinic929f822014-03-28 12:03:34 -0400220#ifdef CONFIG_SPL_BUILD
221 dev = 0;
222#endif
223
224 mmc = find_mmc_device(dev);
225
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000226 if (init_mmc_for_env(mmc)) {
227 ret = 1;
228 goto err;
229 }
230
231 if (mmc_get_env_addr(mmc, 0, &offset1) ||
232 mmc_get_env_addr(mmc, 1, &offset2)) {
233 ret = 1;
234 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
246 crc1_ok = !read1_fail &&
247 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
248 crc2_ok = !read2_fail &&
249 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
250
251 if (!crc1_ok && !crc2_ok) {
252 ret = 1;
253 goto fini;
254 } else if (crc1_ok && !crc2_ok) {
255 gd->env_valid = 1;
256 } else if (!crc1_ok && crc2_ok) {
257 gd->env_valid = 2;
258 } else {
259 /* both ok - check serial */
260 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
261 gd->env_valid = 2;
262 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
263 gd->env_valid = 1;
264 else if (tmp_env1->flags > tmp_env2->flags)
265 gd->env_valid = 1;
266 else if (tmp_env2->flags > tmp_env1->flags)
267 gd->env_valid = 2;
268 else /* flags are equal - almost impossible */
269 gd->env_valid = 1;
270 }
271
272 free(env_ptr);
273
274 if (gd->env_valid == 1)
275 ep = tmp_env1;
276 else
277 ep = tmp_env2;
278
279 env_flags = ep->flags;
280 env_import((char *)ep, 0);
281 ret = 0;
282
283fini:
284 fini_mmc_for_env(mmc);
285err:
286 if (ret)
287 set_default_env(NULL);
288
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000289#endif
290}
291#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lvb4eceac2010-05-17 10:57:01 +0800292void env_relocate_spec(void)
293{
294#if !defined(ENV_IS_EMBEDDED)
Tom Rinia840b8e2013-04-05 14:55:21 -0400295 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400296 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000297 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000298 int ret;
Tom Rinic929f822014-03-28 12:03:34 -0400299 int dev = CONFIG_SYS_MMC_ENV_DEV;
300
301#ifdef CONFIG_SPL_BUILD
302 dev = 0;
303#endif
304
305 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000306
307 if (init_mmc_for_env(mmc)) {
308 ret = 1;
309 goto err;
310 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800311
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000312 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren27ee0f32012-07-30 10:55:44 +0000313 ret = 1;
314 goto fini;
315 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800316
Tom Rinia840b8e2013-04-05 14:55:21 -0400317 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Stephen Warren27ee0f32012-07-30 10:55:44 +0000318 ret = 1;
319 goto fini;
320 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800321
Tom Rinia840b8e2013-04-05 14:55:21 -0400322 env_import(buf, 1);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000323 ret = 0;
324
325fini:
326 fini_mmc_for_env(mmc);
327err:
328 if (ret)
329 set_default_env(NULL);
Terry Lvb4eceac2010-05-17 10:57:01 +0800330#endif
331}
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000332#endif /* CONFIG_ENV_OFFSET_REDUND */