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)
58 offset = CONFIG_ENV_OFFSET;
59 #ifdef CONFIG_ENV_OFFSET_REDUND
61 offset = CONFIG_ENV_OFFSET_REDUND;
65 offset += mmc->capacity;
75 gd->env_addr = (ulong)&default_environment[0];
81 static int init_mmc_for_env(struct mmc *mmc)
84 puts("No MMC card found\n");
89 puts("MMC init failed\n");
93 #ifdef CONFIG_SYS_MMC_ENV_PART
94 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
95 if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
96 CONFIG_SYS_MMC_ENV_PART)) {
97 puts("MMC partition switch failed\n");
106 static void fini_mmc_for_env(struct mmc *mmc)
108 #ifdef CONFIG_SYS_MMC_ENV_PART
109 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
110 mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
115 #ifdef CONFIG_CMD_SAVEENV
116 static inline int write_env(struct mmc *mmc, unsigned long size,
117 unsigned long offset, const void *buffer)
119 uint blk_start, blk_cnt, n;
121 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
122 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
124 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
125 blk_cnt, (u_char *)buffer);
127 return (n == blk_cnt) ? 0 : -1;
130 #ifdef CONFIG_ENV_OFFSET_REDUND
131 static unsigned char env_flags;
136 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
139 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
143 if (init_mmc_for_env(mmc))
146 res = (char *)&env_new->data;
147 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
149 error("Cannot export environment: errno = %d\n", errno);
154 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
156 #ifdef CONFIG_ENV_OFFSET_REDUND
157 env_new->flags = ++env_flags; /* increase the serial */
159 if (gd->env_valid == 1)
163 if (mmc_get_env_addr(mmc, copy, &offset)) {
168 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
169 CONFIG_SYS_MMC_ENV_DEV);
170 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
179 #ifdef CONFIG_ENV_OFFSET_REDUND
180 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
184 fini_mmc_for_env(mmc);
187 #endif /* CONFIG_CMD_SAVEENV */
189 static inline int read_env(struct mmc *mmc, unsigned long size,
190 unsigned long offset, const void *buffer)
192 uint blk_start, blk_cnt, n;
194 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
195 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
197 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
198 blk_cnt, (uchar *)buffer);
200 return (n == blk_cnt) ? 0 : -1;
203 #ifdef CONFIG_ENV_OFFSET_REDUND
204 void env_relocate_spec(void)
206 #if !defined(ENV_IS_EMBEDDED)
207 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
208 u32 offset1, offset2;
209 int read1_fail = 0, read2_fail = 0;
210 int crc1_ok = 0, crc2_ok = 0;
211 env_t *ep, *tmp_env1, *tmp_env2;
214 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
215 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
216 if (tmp_env1 == NULL || tmp_env2 == NULL) {
217 puts("Can't allocate buffers for environment\n");
222 if (init_mmc_for_env(mmc)) {
227 if (mmc_get_env_addr(mmc, 0, &offset1) ||
228 mmc_get_env_addr(mmc, 1, &offset2)) {
233 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
234 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
236 if (read1_fail && read2_fail)
237 puts("*** Error - No Valid Environment Area found\n");
238 else if (read1_fail || read2_fail)
239 puts("*** Warning - some problems detected "
240 "reading environment; recovered successfully\n");
242 crc1_ok = !read1_fail &&
243 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
244 crc2_ok = !read2_fail &&
245 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
247 if (!crc1_ok && !crc2_ok) {
250 } else if (crc1_ok && !crc2_ok) {
252 } else if (!crc1_ok && crc2_ok) {
255 /* both ok - check serial */
256 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
258 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
260 else if (tmp_env1->flags > tmp_env2->flags)
262 else if (tmp_env2->flags > tmp_env1->flags)
264 else /* flags are equal - almost impossible */
270 if (gd->env_valid == 1)
275 env_flags = ep->flags;
276 env_import((char *)ep, 0);
280 fini_mmc_for_env(mmc);
283 set_default_env(NULL);
289 #else /* ! CONFIG_ENV_OFFSET_REDUND */
290 void env_relocate_spec(void)
292 #if !defined(ENV_IS_EMBEDDED)
293 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
294 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
298 if (init_mmc_for_env(mmc)) {
303 if (mmc_get_env_addr(mmc, 0, &offset)) {
308 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
317 fini_mmc_for_env(mmc);
320 set_default_env(NULL);
323 #endif /* CONFIG_ENV_OFFSET_REDUND */