blob: 7da10e61afbba4733fddf5bbbcd11f0006a396b0 [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
Igor Grinbergecbfb622011-11-07 01:14:05 +000065static int init_mmc_for_env(struct mmc *mmc)
Terry Lvb4eceac2010-05-17 10:57:01 +080066{
Tom Rinic929f822014-03-28 12:03:34 -040067#ifdef CONFIG_SYS_MMC_ENV_PART
68 int dev = CONFIG_SYS_MMC_ENV_DEV;
69
70#ifdef CONFIG_SPL_BUILD
71 dev = 0;
72#endif
73#endif
74
Terry Lvb4eceac2010-05-17 10:57:01 +080075 if (!mmc) {
76 puts("No MMC card found\n");
77 return -1;
78 }
79
80 if (mmc_init(mmc)) {
81 puts("MMC init failed\n");
Igor Grinbergecbfb622011-11-07 01:14:05 +000082 return -1;
Terry Lvb4eceac2010-05-17 10:57:01 +080083 }
84
Stephen Warren27ee0f32012-07-30 10:55:44 +000085#ifdef CONFIG_SYS_MMC_ENV_PART
86 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
Tom Rinic929f822014-03-28 12:03:34 -040087 if (mmc_switch_part(dev, CONFIG_SYS_MMC_ENV_PART)) {
Stephen Warren27ee0f32012-07-30 10:55:44 +000088 puts("MMC partition switch failed\n");
89 return -1;
90 }
91 }
92#endif
93
Terry Lvb4eceac2010-05-17 10:57:01 +080094 return 0;
95}
96
Stephen Warren27ee0f32012-07-30 10:55:44 +000097static void fini_mmc_for_env(struct mmc *mmc)
98{
99#ifdef CONFIG_SYS_MMC_ENV_PART
Tom Rinic929f822014-03-28 12:03:34 -0400100 int dev = CONFIG_SYS_MMC_ENV_DEV;
101
102#ifdef CONFIG_SPL_BUILD
103 dev = 0;
104#endif
Stephen Warren27ee0f32012-07-30 10:55:44 +0000105 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
Tom Rinic929f822014-03-28 12:03:34 -0400106 mmc_switch_part(dev, mmc->part_num);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000107#endif
108}
109
Terry Lvb4eceac2010-05-17 10:57:01 +0800110#ifdef CONFIG_CMD_SAVEENV
Igor Grinbergecbfb622011-11-07 01:14:05 +0000111static inline int write_env(struct mmc *mmc, unsigned long size,
112 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800113{
114 uint blk_start, blk_cnt, n;
115
Igor Grinbergecbfb622011-11-07 01:14:05 +0000116 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
117 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800118
119 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
120 blk_cnt, (u_char *)buffer);
121
122 return (n == blk_cnt) ? 0 : -1;
123}
124
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000125#ifdef CONFIG_ENV_OFFSET_REDUND
126static unsigned char env_flags;
127#endif
128
Terry Lvb4eceac2010-05-17 10:57:01 +0800129int saveenv(void)
130{
Tom Rinia840b8e2013-04-05 14:55:21 -0400131 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Terry Lvb4eceac2010-05-17 10:57:01 +0800132 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000133 u32 offset;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000134 int ret, copy = 0;
Terry Lvb4eceac2010-05-17 10:57:01 +0800135
Stephen Warren27ee0f32012-07-30 10:55:44 +0000136 if (init_mmc_for_env(mmc))
Terry Lvb4eceac2010-05-17 10:57:01 +0800137 return 1;
138
Marek Vasutd73c1292014-03-05 19:59:50 +0100139 ret = env_export(env_new);
140 if (ret)
Stephen Warren27ee0f32012-07-30 10:55:44 +0000141 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000142
143#ifdef CONFIG_ENV_OFFSET_REDUND
144 env_new->flags = ++env_flags; /* increase the serial */
145
146 if (gd->env_valid == 1)
147 copy = 1;
148#endif
149
150 if (mmc_get_env_addr(mmc, copy, &offset)) {
151 ret = 1;
152 goto fini;
153 }
154
155 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
156 CONFIG_SYS_MMC_ENV_DEV);
Stephen Warren42a3c442012-05-24 11:38:33 +0000157 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800158 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000159 ret = 1;
160 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800161 }
162
163 puts("done\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000164 ret = 0;
165
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000166#ifdef CONFIG_ENV_OFFSET_REDUND
167 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
168#endif
169
Stephen Warren27ee0f32012-07-30 10:55:44 +0000170fini:
171 fini_mmc_for_env(mmc);
172 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800173}
174#endif /* CONFIG_CMD_SAVEENV */
175
Igor Grinbergecbfb622011-11-07 01:14:05 +0000176static inline int read_env(struct mmc *mmc, unsigned long size,
177 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800178{
179 uint blk_start, blk_cnt, n;
Tom Rinic929f822014-03-28 12:03:34 -0400180 int dev = CONFIG_SYS_MMC_ENV_DEV;
181
182#ifdef CONFIG_SPL_BUILD
183 dev = 0;
184#endif
Terry Lvb4eceac2010-05-17 10:57:01 +0800185
Igor Grinbergecbfb622011-11-07 01:14:05 +0000186 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
187 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800188
Tom Rinic929f822014-03-28 12:03:34 -0400189 n = mmc->block_dev.block_read(dev, blk_start, blk_cnt, (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800190
191 return (n == blk_cnt) ? 0 : -1;
192}
193
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000194#ifdef CONFIG_ENV_OFFSET_REDUND
195void env_relocate_spec(void)
196{
197#if !defined(ENV_IS_EMBEDDED)
Tom Rinic929f822014-03-28 12:03:34 -0400198 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000199 u32 offset1, offset2;
200 int read1_fail = 0, read2_fail = 0;
201 int crc1_ok = 0, crc2_ok = 0;
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200202 env_t *ep;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000203 int ret;
Tom Rinic929f822014-03-28 12:03:34 -0400204 int dev = CONFIG_SYS_MMC_ENV_DEV;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000205
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200206 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
207 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
208
Tom Rinic929f822014-03-28 12:03:34 -0400209#ifdef CONFIG_SPL_BUILD
210 dev = 0;
211#endif
212
213 mmc = find_mmc_device(dev);
214
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000215 if (init_mmc_for_env(mmc)) {
216 ret = 1;
217 goto err;
218 }
219
220 if (mmc_get_env_addr(mmc, 0, &offset1) ||
221 mmc_get_env_addr(mmc, 1, &offset2)) {
222 ret = 1;
223 goto fini;
224 }
225
226 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
227 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
228
229 if (read1_fail && read2_fail)
230 puts("*** Error - No Valid Environment Area found\n");
231 else if (read1_fail || read2_fail)
232 puts("*** Warning - some problems detected "
233 "reading environment; recovered successfully\n");
234
235 crc1_ok = !read1_fail &&
236 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
237 crc2_ok = !read2_fail &&
238 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
239
240 if (!crc1_ok && !crc2_ok) {
241 ret = 1;
242 goto fini;
243 } else if (crc1_ok && !crc2_ok) {
244 gd->env_valid = 1;
245 } else if (!crc1_ok && crc2_ok) {
246 gd->env_valid = 2;
247 } else {
248 /* both ok - check serial */
249 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
250 gd->env_valid = 2;
251 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
252 gd->env_valid = 1;
253 else if (tmp_env1->flags > tmp_env2->flags)
254 gd->env_valid = 1;
255 else if (tmp_env2->flags > tmp_env1->flags)
256 gd->env_valid = 2;
257 else /* flags are equal - almost impossible */
258 gd->env_valid = 1;
259 }
260
261 free(env_ptr);
262
263 if (gd->env_valid == 1)
264 ep = tmp_env1;
265 else
266 ep = tmp_env2;
267
268 env_flags = ep->flags;
269 env_import((char *)ep, 0);
270 ret = 0;
271
272fini:
273 fini_mmc_for_env(mmc);
274err:
275 if (ret)
276 set_default_env(NULL);
277
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000278#endif
279}
280#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lvb4eceac2010-05-17 10:57:01 +0800281void env_relocate_spec(void)
282{
283#if !defined(ENV_IS_EMBEDDED)
Tom Rinia840b8e2013-04-05 14:55:21 -0400284 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400285 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000286 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000287 int ret;
Tom Rinic929f822014-03-28 12:03:34 -0400288 int dev = CONFIG_SYS_MMC_ENV_DEV;
289
290#ifdef CONFIG_SPL_BUILD
291 dev = 0;
292#endif
293
294 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000295
296 if (init_mmc_for_env(mmc)) {
297 ret = 1;
298 goto err;
299 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800300
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000301 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren27ee0f32012-07-30 10:55:44 +0000302 ret = 1;
303 goto fini;
304 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800305
Tom Rinia840b8e2013-04-05 14:55:21 -0400306 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Stephen Warren27ee0f32012-07-30 10:55:44 +0000307 ret = 1;
308 goto fini;
309 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800310
Tom Rinia840b8e2013-04-05 14:55:21 -0400311 env_import(buf, 1);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000312 ret = 0;
313
314fini:
315 fini_mmc_for_env(mmc);
316err:
317 if (ret)
318 set_default_env(NULL);
Terry Lvb4eceac2010-05-17 10:57:01 +0800319#endif
320}
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000321#endif /* CONFIG_ENV_OFFSET_REDUND */