2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
13 #include <linux/stddef.h>
19 #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
24 char *env_name_spec = "MMC";
26 #ifdef ENV_IS_EMBEDDED
27 env_t *env_ptr = &environment;
28 #else /* ! ENV_IS_EMBEDDED */
30 #endif /* ENV_IS_EMBEDDED */
32 DECLARE_GLOBAL_DATA_PTR;
34 #if !defined(CONFIG_ENV_OFFSET)
35 #define CONFIG_ENV_OFFSET 0
38 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
42 offset = CONFIG_ENV_OFFSET;
43 #ifdef CONFIG_ENV_OFFSET_REDUND
45 offset = CONFIG_ENV_OFFSET_REDUND;
49 offset += mmc->capacity;
59 gd->env_addr = (ulong)&default_environment[0];
65 static int init_mmc_for_env(struct mmc *mmc)
68 puts("No MMC card found\n");
73 puts("MMC init failed\n");
77 #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");
90 static void fini_mmc_for_env(struct mmc *mmc)
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,
99 #ifdef CONFIG_CMD_SAVEENV
100 static inline int write_env(struct mmc *mmc, unsigned long size,
101 unsigned long offset, const void *buffer)
103 uint blk_start, blk_cnt, n;
105 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;
108 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
109 blk_cnt, (u_char *)buffer);
111 return (n == blk_cnt) ? 0 : -1;
114 #ifdef CONFIG_ENV_OFFSET_REDUND
115 static unsigned char env_flags;
120 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
123 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
127 if (init_mmc_for_env(mmc))
130 res = (char *)&env_new->data;
131 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
133 error("Cannot export environment: errno = %d\n", errno);
138 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
140 #ifdef CONFIG_ENV_OFFSET_REDUND
141 env_new->flags = ++env_flags; /* increase the serial */
143 if (gd->env_valid == 1)
147 if (mmc_get_env_addr(mmc, copy, &offset)) {
152 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
153 CONFIG_SYS_MMC_ENV_DEV);
154 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
163 #ifdef CONFIG_ENV_OFFSET_REDUND
164 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
168 fini_mmc_for_env(mmc);
171 #endif /* CONFIG_CMD_SAVEENV */
173 static inline int read_env(struct mmc *mmc, unsigned long size,
174 unsigned long offset, const void *buffer)
176 uint blk_start, blk_cnt, n;
178 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;
181 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
182 blk_cnt, (uchar *)buffer);
184 return (n == blk_cnt) ? 0 : -1;
187 #ifdef CONFIG_ENV_OFFSET_REDUND
188 void env_relocate_spec(void)
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;
198 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
199 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
201 if (tmp_env1 == NULL || tmp_env2 == NULL) {
202 puts("Can't allocate buffers for environment\n");
207 if (init_mmc_for_env(mmc)) {
212 if (mmc_get_env_addr(mmc, 0, &offset1) ||
213 mmc_get_env_addr(mmc, 1, &offset2)) {
218 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
219 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
221 if (read1_fail && read2_fail)
222 puts("*** Error - No Valid Environment Area found\n");
223 else if (read1_fail || read2_fail)
224 puts("*** Warning - some problems detected "
225 "reading environment; recovered successfully\n");
227 crc1_ok = !read1_fail &&
228 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
229 crc2_ok = !read2_fail &&
230 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
232 if (!crc1_ok && !crc2_ok) {
235 } else if (crc1_ok && !crc2_ok) {
237 } else if (!crc1_ok && crc2_ok) {
240 /* both ok - check serial */
241 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
243 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
245 else if (tmp_env1->flags > tmp_env2->flags)
247 else if (tmp_env2->flags > tmp_env1->flags)
249 else /* flags are equal - almost impossible */
255 if (gd->env_valid == 1)
260 env_flags = ep->flags;
261 env_import((char *)ep, 0);
265 fini_mmc_for_env(mmc);
268 set_default_env(NULL);
272 #else /* ! CONFIG_ENV_OFFSET_REDUND */
273 void env_relocate_spec(void)
275 #if !defined(ENV_IS_EMBEDDED)
276 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
277 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
281 if (init_mmc_for_env(mmc)) {
286 if (mmc_get_env_addr(mmc, 0, &offset)) {
291 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
300 fini_mmc_for_env(mmc);
303 set_default_env(NULL);
306 #endif /* CONFIG_ENV_OFFSET_REDUND */