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)
67 #ifdef CONFIG_SYS_MMC_ENV_PART
68 int dev = CONFIG_SYS_MMC_ENV_DEV;
70 #ifdef CONFIG_SPL_BUILD
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(dev, CONFIG_SYS_MMC_ENV_PART)) {
88 puts("MMC partition switch failed\n");
97 static void fini_mmc_for_env(struct mmc *mmc)
99 #ifdef CONFIG_SYS_MMC_ENV_PART
100 int dev = CONFIG_SYS_MMC_ENV_DEV;
102 #ifdef CONFIG_SPL_BUILD
105 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
106 mmc_switch_part(dev, mmc->part_num);
110 #ifdef CONFIG_CMD_SAVEENV
111 static inline int write_env(struct mmc *mmc, unsigned long size,
112 unsigned long offset, const void *buffer)
114 uint blk_start, blk_cnt, n;
116 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;
119 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
120 blk_cnt, (u_char *)buffer);
122 return (n == blk_cnt) ? 0 : -1;
125 #ifdef CONFIG_ENV_OFFSET_REDUND
126 static unsigned char env_flags;
131 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
134 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
138 if (init_mmc_for_env(mmc))
141 res = (char *)&env_new->data;
142 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
144 error("Cannot export environment: errno = %d\n", errno);
149 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
151 #ifdef CONFIG_ENV_OFFSET_REDUND
152 env_new->flags = ++env_flags; /* increase the serial */
154 if (gd->env_valid == 1)
158 if (mmc_get_env_addr(mmc, copy, &offset)) {
163 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
164 CONFIG_SYS_MMC_ENV_DEV);
165 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
174 #ifdef CONFIG_ENV_OFFSET_REDUND
175 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
179 fini_mmc_for_env(mmc);
182 #endif /* CONFIG_CMD_SAVEENV */
184 static inline int read_env(struct mmc *mmc, unsigned long size,
185 unsigned long offset, const void *buffer)
187 uint blk_start, blk_cnt, n;
188 int dev = CONFIG_SYS_MMC_ENV_DEV;
190 #ifdef CONFIG_SPL_BUILD
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(dev, blk_start, blk_cnt, (uchar *)buffer);
199 return (n == blk_cnt) ? 0 : -1;
202 #ifdef CONFIG_ENV_OFFSET_REDUND
203 void env_relocate_spec(void)
205 #if !defined(ENV_IS_EMBEDDED)
207 u32 offset1, offset2;
208 int read1_fail = 0, read2_fail = 0;
209 int crc1_ok = 0, crc2_ok = 0;
212 int dev = CONFIG_SYS_MMC_ENV_DEV;
214 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
215 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
217 #ifdef CONFIG_SPL_BUILD
221 mmc = find_mmc_device(dev);
223 if (init_mmc_for_env(mmc)) {
228 if (mmc_get_env_addr(mmc, 0, &offset1) ||
229 mmc_get_env_addr(mmc, 1, &offset2)) {
234 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
235 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
237 if (read1_fail && read2_fail)
238 puts("*** Error - No Valid Environment Area found\n");
239 else if (read1_fail || read2_fail)
240 puts("*** Warning - some problems detected "
241 "reading environment; recovered successfully\n");
243 crc1_ok = !read1_fail &&
244 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
245 crc2_ok = !read2_fail &&
246 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
248 if (!crc1_ok && !crc2_ok) {
251 } else if (crc1_ok && !crc2_ok) {
253 } else if (!crc1_ok && crc2_ok) {
256 /* both ok - check serial */
257 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
259 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
261 else if (tmp_env1->flags > tmp_env2->flags)
263 else if (tmp_env2->flags > tmp_env1->flags)
265 else /* flags are equal - almost impossible */
271 if (gd->env_valid == 1)
276 env_flags = ep->flags;
277 env_import((char *)ep, 0);
281 fini_mmc_for_env(mmc);
284 set_default_env(NULL);
288 #else /* ! CONFIG_ENV_OFFSET_REDUND */
289 void env_relocate_spec(void)
291 #if !defined(ENV_IS_EMBEDDED)
292 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
296 int dev = CONFIG_SYS_MMC_ENV_DEV;
298 #ifdef CONFIG_SPL_BUILD
302 mmc = find_mmc_device(dev);
304 if (init_mmc_for_env(mmc)) {
309 if (mmc_get_env_addr(mmc, 0, &offset)) {
314 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
323 fini_mmc_for_env(mmc);
326 set_default_env(NULL);
329 #endif /* CONFIG_ENV_OFFSET_REDUND */