2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <environment.h>
29 #include <linux/stddef.h>
35 #if defined(CONFIG_ENV_SIZE_REDUND) && \
36 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
37 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
40 char *env_name_spec = "MMC";
42 #ifdef ENV_IS_EMBEDDED
43 env_t *env_ptr = &environment;
44 #else /* ! ENV_IS_EMBEDDED */
46 #endif /* ENV_IS_EMBEDDED */
48 DECLARE_GLOBAL_DATA_PTR;
50 #if !defined(CONFIG_ENV_OFFSET)
51 #define CONFIG_ENV_OFFSET 0
54 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
56 *env_addr = CONFIG_ENV_OFFSET;
57 #ifdef CONFIG_ENV_OFFSET_REDUND
59 *env_addr = CONFIG_ENV_OFFSET_REDUND;
67 gd->env_addr = (ulong)&default_environment[0];
73 static int init_mmc_for_env(struct mmc *mmc)
76 puts("No MMC card found\n");
81 puts("MMC init failed\n");
85 #ifdef CONFIG_SYS_MMC_ENV_PART
86 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
87 if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
88 CONFIG_SYS_MMC_ENV_PART)) {
89 puts("MMC partition switch failed\n");
98 static void fini_mmc_for_env(struct mmc *mmc)
100 #ifdef CONFIG_SYS_MMC_ENV_PART
101 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
102 mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
107 #ifdef CONFIG_CMD_SAVEENV
108 static inline int write_env(struct mmc *mmc, unsigned long size,
109 unsigned long offset, const void *buffer)
111 uint blk_start, blk_cnt, n;
113 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
114 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
116 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
117 blk_cnt, (u_char *)buffer);
119 return (n == blk_cnt) ? 0 : -1;
122 #ifdef CONFIG_ENV_OFFSET_REDUND
123 static unsigned char env_flags;
128 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
131 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
135 if (init_mmc_for_env(mmc))
138 res = (char *)&env_new->data;
139 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
141 error("Cannot export environment: errno = %d\n", errno);
146 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
148 #ifdef CONFIG_ENV_OFFSET_REDUND
149 env_new->flags = ++env_flags; /* increase the serial */
151 if (gd->env_valid == 1)
155 if (mmc_get_env_addr(mmc, copy, &offset)) {
160 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
161 CONFIG_SYS_MMC_ENV_DEV);
162 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
171 #ifdef CONFIG_ENV_OFFSET_REDUND
172 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
176 fini_mmc_for_env(mmc);
179 #endif /* CONFIG_CMD_SAVEENV */
181 static inline int read_env(struct mmc *mmc, unsigned long size,
182 unsigned long offset, const void *buffer)
184 uint blk_start, blk_cnt, n;
186 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;
189 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
190 blk_cnt, (uchar *)buffer);
192 return (n == blk_cnt) ? 0 : -1;
195 #ifdef CONFIG_ENV_OFFSET_REDUND
196 void env_relocate_spec(void)
198 #if !defined(ENV_IS_EMBEDDED)
199 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
200 u32 offset1, offset2;
201 int read1_fail = 0, read2_fail = 0;
202 int crc1_ok = 0, crc2_ok = 0;
203 env_t *ep, *tmp_env1, *tmp_env2;
206 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
207 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
208 if (tmp_env1 == NULL || tmp_env2 == NULL) {
209 puts("Can't allocate buffers for environment\n");
214 if (init_mmc_for_env(mmc)) {
219 if (mmc_get_env_addr(mmc, 0, &offset1) ||
220 mmc_get_env_addr(mmc, 1, &offset2)) {
225 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
226 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
228 if (read1_fail && read2_fail)
229 puts("*** Error - No Valid Environment Area found\n");
230 else if (read1_fail || read2_fail)
231 puts("*** Warning - some problems detected "
232 "reading environment; recovered successfully\n");
234 crc1_ok = !read1_fail &&
235 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
236 crc2_ok = !read2_fail &&
237 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
239 if (!crc1_ok && !crc2_ok) {
242 } else if (crc1_ok && !crc2_ok) {
244 } else if (!crc1_ok && crc2_ok) {
247 /* both ok - check serial */
248 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
250 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
252 else if (tmp_env1->flags > tmp_env2->flags)
254 else if (tmp_env2->flags > tmp_env1->flags)
256 else /* flags are equal - almost impossible */
262 if (gd->env_valid == 1)
267 env_flags = ep->flags;
268 env_import((char *)ep, 0);
272 fini_mmc_for_env(mmc);
275 set_default_env(NULL);
281 #else /* ! CONFIG_ENV_OFFSET_REDUND */
282 void env_relocate_spec(void)
284 #if !defined(ENV_IS_EMBEDDED)
285 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
286 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
290 if (init_mmc_for_env(mmc)) {
295 if (mmc_get_env_addr(mmc, 0, &offset)) {
300 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
309 fini_mmc_for_env(mmc);
312 set_default_env(NULL);
315 #endif /* CONFIG_ENV_OFFSET_REDUND */