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 #ifdef CONFIG_SYS_MMC_ENV_PART
66 __weak uint mmc_get_env_part(struct mmc *mmc)
68 return CONFIG_SYS_MMC_ENV_PART;
71 static int mmc_set_env_part(struct mmc *mmc)
73 uint part = mmc_get_env_part(mmc);
74 int dev = CONFIG_SYS_MMC_ENV_DEV;
77 #ifdef CONFIG_SPL_BUILD
81 if (part != mmc->part_num) {
82 ret = mmc_switch_part(dev, part);
84 puts("MMC partition switch failed\n");
90 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
93 static int init_mmc_for_env(struct mmc *mmc)
96 puts("No MMC card found\n");
101 puts("MMC init failed\n");
105 return mmc_set_env_part(mmc);
108 static void fini_mmc_for_env(struct mmc *mmc)
110 #ifdef CONFIG_SYS_MMC_ENV_PART
111 int dev = CONFIG_SYS_MMC_ENV_DEV;
113 #ifdef CONFIG_SPL_BUILD
116 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
117 mmc_switch_part(dev, mmc->part_num);
121 #ifdef CONFIG_CMD_SAVEENV
122 static inline int write_env(struct mmc *mmc, unsigned long size,
123 unsigned long offset, const void *buffer)
125 uint blk_start, blk_cnt, n;
127 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
128 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
130 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
131 blk_cnt, (u_char *)buffer);
133 return (n == blk_cnt) ? 0 : -1;
136 #ifdef CONFIG_ENV_OFFSET_REDUND
137 static unsigned char env_flags;
142 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
143 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
147 if (init_mmc_for_env(mmc))
150 ret = env_export(env_new);
154 #ifdef CONFIG_ENV_OFFSET_REDUND
155 env_new->flags = ++env_flags; /* increase the serial */
157 if (gd->env_valid == 1)
161 if (mmc_get_env_addr(mmc, copy, &offset)) {
166 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
167 CONFIG_SYS_MMC_ENV_DEV);
168 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
177 #ifdef CONFIG_ENV_OFFSET_REDUND
178 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
182 fini_mmc_for_env(mmc);
185 #endif /* CONFIG_CMD_SAVEENV */
187 static inline int read_env(struct mmc *mmc, unsigned long size,
188 unsigned long offset, const void *buffer)
190 uint blk_start, blk_cnt, n;
191 int dev = CONFIG_SYS_MMC_ENV_DEV;
193 #ifdef CONFIG_SPL_BUILD
197 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
198 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
200 n = mmc->block_dev.block_read(dev, blk_start, blk_cnt, (uchar *)buffer);
202 return (n == blk_cnt) ? 0 : -1;
205 #ifdef CONFIG_ENV_OFFSET_REDUND
206 void env_relocate_spec(void)
208 #if !defined(ENV_IS_EMBEDDED)
210 u32 offset1, offset2;
211 int read1_fail = 0, read2_fail = 0;
212 int crc1_ok = 0, crc2_ok = 0;
215 int dev = CONFIG_SYS_MMC_ENV_DEV;
217 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
218 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
220 #ifdef CONFIG_SPL_BUILD
224 mmc = find_mmc_device(dev);
226 if (init_mmc_for_env(mmc)) {
231 if (mmc_get_env_addr(mmc, 0, &offset1) ||
232 mmc_get_env_addr(mmc, 1, &offset2)) {
237 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
238 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
240 if (read1_fail && read2_fail)
241 puts("*** Error - No Valid Environment Area found\n");
242 else if (read1_fail || read2_fail)
243 puts("*** Warning - some problems detected "
244 "reading environment; recovered successfully\n");
246 crc1_ok = !read1_fail &&
247 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
248 crc2_ok = !read2_fail &&
249 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
251 if (!crc1_ok && !crc2_ok) {
254 } else if (crc1_ok && !crc2_ok) {
256 } else if (!crc1_ok && crc2_ok) {
259 /* both ok - check serial */
260 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
262 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
264 else if (tmp_env1->flags > tmp_env2->flags)
266 else if (tmp_env2->flags > tmp_env1->flags)
268 else /* flags are equal - almost impossible */
274 if (gd->env_valid == 1)
279 env_flags = ep->flags;
280 env_import((char *)ep, 0);
284 fini_mmc_for_env(mmc);
287 set_default_env(NULL);
291 #else /* ! CONFIG_ENV_OFFSET_REDUND */
292 void env_relocate_spec(void)
294 #if !defined(ENV_IS_EMBEDDED)
295 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
299 int dev = CONFIG_SYS_MMC_ENV_DEV;
301 #ifdef CONFIG_SPL_BUILD
305 mmc = find_mmc_device(dev);
307 if (init_mmc_for_env(mmc)) {
312 if (mmc_get_env_addr(mmc, 0, &offset)) {
317 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
326 fini_mmc_for_env(mmc);
329 set_default_env(NULL);
332 #endif /* CONFIG_ENV_OFFSET_REDUND */