blob: 65aafa9cac26472abc4d9707e88d42361eadef5f [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 Denk1a459662013-07-08 09:37:19 +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
Igor Grinberge8db8f72011-11-07 01:14:05 +000065static int init_mmc_for_env(struct mmc *mmc)
Terry Lva8060352010-05-17 10:57:01 +080066{
67 if (!mmc) {
68 puts("No MMC card found\n");
69 return -1;
70 }
71
72 if (mmc_init(mmc)) {
73 puts("MMC init failed\n");
Igor Grinberge8db8f72011-11-07 01:14:05 +000074 return -1;
Terry Lva8060352010-05-17 10:57:01 +080075 }
76
Stephen Warren9404a5f2012-07-30 10:55:44 +000077#ifdef CONFIG_SYS_MMC_ENV_PART
78 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
79 if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
80 CONFIG_SYS_MMC_ENV_PART)) {
81 puts("MMC partition switch failed\n");
82 return -1;
83 }
84 }
85#endif
86
Terry Lva8060352010-05-17 10:57:01 +080087 return 0;
88}
89
Stephen Warren9404a5f2012-07-30 10:55:44 +000090static void fini_mmc_for_env(struct mmc *mmc)
91{
92#ifdef CONFIG_SYS_MMC_ENV_PART
93 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
94 mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
95 mmc->part_num);
96#endif
97}
98
Terry Lva8060352010-05-17 10:57:01 +080099#ifdef CONFIG_CMD_SAVEENV
Igor Grinberge8db8f72011-11-07 01:14:05 +0000100static inline int write_env(struct mmc *mmc, unsigned long size,
101 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800102{
103 uint blk_start, blk_cnt, n;
104
Igor Grinberge8db8f72011-11-07 01:14:05 +0000105 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
106 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800107
108 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
109 blk_cnt, (u_char *)buffer);
110
111 return (n == blk_cnt) ? 0 : -1;
112}
113
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000114#ifdef CONFIG_ENV_OFFSET_REDUND
115static unsigned char env_flags;
116#endif
117
Terry Lva8060352010-05-17 10:57:01 +0800118int saveenv(void)
119{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400120 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Lei Wene79f4832010-10-13 11:07:21 +0800121 ssize_t len;
122 char *res;
Terry Lva8060352010-05-17 10:57:01 +0800123 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Igor Grinberge8db8f72011-11-07 01:14:05 +0000124 u32 offset;
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000125 int ret, copy = 0;
Terry Lva8060352010-05-17 10:57:01 +0800126
Stephen Warren9404a5f2012-07-30 10:55:44 +0000127 if (init_mmc_for_env(mmc))
Mingkai Hu97039ab2011-01-24 17:09:55 +0000128 return 1;
129
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400130 res = (char *)&env_new->data;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600131 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Lei Wene79f4832010-10-13 11:07:21 +0800132 if (len < 0) {
133 error("Cannot export environment: errno = %d\n", errno);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000134 ret = 1;
135 goto fini;
Lei Wene79f4832010-10-13 11:07:21 +0800136 }
Igor Grinberge8db8f72011-11-07 01:14:05 +0000137
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400138 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000139
140#ifdef CONFIG_ENV_OFFSET_REDUND
141 env_new->flags = ++env_flags; /* increase the serial */
142
143 if (gd->env_valid == 1)
144 copy = 1;
145#endif
146
147 if (mmc_get_env_addr(mmc, copy, &offset)) {
148 ret = 1;
149 goto fini;
150 }
151
152 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
153 CONFIG_SYS_MMC_ENV_DEV);
Stephen Warren4036b632012-05-24 11:38:33 +0000154 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lva8060352010-05-17 10:57:01 +0800155 puts("failed\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000156 ret = 1;
157 goto fini;
Terry Lva8060352010-05-17 10:57:01 +0800158 }
159
160 puts("done\n");
Stephen Warren9404a5f2012-07-30 10:55:44 +0000161 ret = 0;
162
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000163#ifdef CONFIG_ENV_OFFSET_REDUND
164 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
165#endif
166
Stephen Warren9404a5f2012-07-30 10:55:44 +0000167fini:
168 fini_mmc_for_env(mmc);
169 return ret;
Terry Lva8060352010-05-17 10:57:01 +0800170}
171#endif /* CONFIG_CMD_SAVEENV */
172
Igor Grinberge8db8f72011-11-07 01:14:05 +0000173static inline int read_env(struct mmc *mmc, unsigned long size,
174 unsigned long offset, const void *buffer)
Terry Lva8060352010-05-17 10:57:01 +0800175{
176 uint blk_start, blk_cnt, n;
177
Igor Grinberge8db8f72011-11-07 01:14:05 +0000178 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
179 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lva8060352010-05-17 10:57:01 +0800180
181 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
182 blk_cnt, (uchar *)buffer);
183
184 return (n == blk_cnt) ? 0 : -1;
185}
186
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000187#ifdef CONFIG_ENV_OFFSET_REDUND
188void env_relocate_spec(void)
189{
190#if !defined(ENV_IS_EMBEDDED)
191 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
192 u32 offset1, offset2;
193 int read1_fail = 0, read2_fail = 0;
194 int crc1_ok = 0, crc2_ok = 0;
195 env_t *ep, *tmp_env1, *tmp_env2;
196 int ret;
197
198 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
199 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
200 if (tmp_env1 == NULL || tmp_env2 == NULL) {
201 puts("Can't allocate buffers for environment\n");
202 ret = 1;
203 goto err;
204 }
205
206 if (init_mmc_for_env(mmc)) {
207 ret = 1;
208 goto err;
209 }
210
211 if (mmc_get_env_addr(mmc, 0, &offset1) ||
212 mmc_get_env_addr(mmc, 1, &offset2)) {
213 ret = 1;
214 goto fini;
215 }
216
217 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
218 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
219
220 if (read1_fail && read2_fail)
221 puts("*** Error - No Valid Environment Area found\n");
222 else if (read1_fail || read2_fail)
223 puts("*** Warning - some problems detected "
224 "reading environment; recovered successfully\n");
225
226 crc1_ok = !read1_fail &&
227 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
228 crc2_ok = !read2_fail &&
229 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
230
231 if (!crc1_ok && !crc2_ok) {
232 ret = 1;
233 goto fini;
234 } else if (crc1_ok && !crc2_ok) {
235 gd->env_valid = 1;
236 } else if (!crc1_ok && crc2_ok) {
237 gd->env_valid = 2;
238 } else {
239 /* both ok - check serial */
240 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
241 gd->env_valid = 2;
242 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
243 gd->env_valid = 1;
244 else if (tmp_env1->flags > tmp_env2->flags)
245 gd->env_valid = 1;
246 else if (tmp_env2->flags > tmp_env1->flags)
247 gd->env_valid = 2;
248 else /* flags are equal - almost impossible */
249 gd->env_valid = 1;
250 }
251
252 free(env_ptr);
253
254 if (gd->env_valid == 1)
255 ep = tmp_env1;
256 else
257 ep = tmp_env2;
258
259 env_flags = ep->flags;
260 env_import((char *)ep, 0);
261 ret = 0;
262
263fini:
264 fini_mmc_for_env(mmc);
265err:
266 if (ret)
267 set_default_env(NULL);
268
269 free(tmp_env1);
270 free(tmp_env2);
271#endif
272}
273#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lva8060352010-05-17 10:57:01 +0800274void env_relocate_spec(void)
275{
276#if !defined(ENV_IS_EMBEDDED)
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400277 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Terry Lva8060352010-05-17 10:57:01 +0800278 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Mingkai Hu97039ab2011-01-24 17:09:55 +0000279 u32 offset;
Stephen Warren9404a5f2012-07-30 10:55:44 +0000280 int ret;
Terry Lva8060352010-05-17 10:57:01 +0800281
Stephen Warren9404a5f2012-07-30 10:55:44 +0000282 if (init_mmc_for_env(mmc)) {
283 ret = 1;
284 goto err;
285 }
Terry Lva8060352010-05-17 10:57:01 +0800286
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000287 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000288 ret = 1;
289 goto fini;
290 }
291
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400292 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Stephen Warren9404a5f2012-07-30 10:55:44 +0000293 ret = 1;
294 goto fini;
295 }
Terry Lva8060352010-05-17 10:57:01 +0800296
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400297 env_import(buf, 1);
Stephen Warren9404a5f2012-07-30 10:55:44 +0000298 ret = 0;
299
300fini:
301 fini_mmc_for_env(mmc);
302err:
303 if (ret)
304 set_default_env(NULL);
Terry Lva8060352010-05-17 10:57:01 +0800305#endif
306}
Michael Heimpoldd196bd82013-04-10 10:36:19 +0000307#endif /* CONFIG_ENV_OFFSET_REDUND */