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 const char *init_mmc_for_env(struct mmc *mmc)
96 return "!No MMC card found";
99 return "!MMC init failed";
101 if (mmc_set_env_part(mmc))
102 return "!MMC partition switch failed";
107 static void fini_mmc_for_env(struct mmc *mmc)
109 #ifdef CONFIG_SYS_MMC_ENV_PART
110 int dev = CONFIG_SYS_MMC_ENV_DEV;
112 #ifdef CONFIG_SPL_BUILD
115 if (mmc_get_env_part(mmc) != mmc->part_num)
116 mmc_switch_part(dev, mmc->part_num);
120 #ifdef CONFIG_CMD_SAVEENV
121 static inline int write_env(struct mmc *mmc, unsigned long size,
122 unsigned long offset, const void *buffer)
124 uint blk_start, blk_cnt, n;
126 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
127 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
129 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
130 blk_cnt, (u_char *)buffer);
132 return (n == blk_cnt) ? 0 : -1;
135 #ifdef CONFIG_ENV_OFFSET_REDUND
136 static unsigned char env_flags;
141 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
142 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
147 errmsg = init_mmc_for_env(mmc);
149 printf("%s\n", errmsg);
153 ret = env_export(env_new);
157 #ifdef CONFIG_ENV_OFFSET_REDUND
158 env_new->flags = ++env_flags; /* increase the serial */
160 if (gd->env_valid == 1)
164 if (mmc_get_env_addr(mmc, copy, &offset)) {
169 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
170 CONFIG_SYS_MMC_ENV_DEV);
171 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
180 #ifdef CONFIG_ENV_OFFSET_REDUND
181 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
185 fini_mmc_for_env(mmc);
188 #endif /* CONFIG_CMD_SAVEENV */
190 static inline int read_env(struct mmc *mmc, unsigned long size,
191 unsigned long offset, const void *buffer)
193 uint blk_start, blk_cnt, n;
194 int dev = CONFIG_SYS_MMC_ENV_DEV;
196 #ifdef CONFIG_SPL_BUILD
200 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
201 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
203 n = mmc->block_dev.block_read(dev, blk_start, blk_cnt, (uchar *)buffer);
205 return (n == blk_cnt) ? 0 : -1;
208 #ifdef CONFIG_ENV_OFFSET_REDUND
209 void env_relocate_spec(void)
211 #if !defined(ENV_IS_EMBEDDED)
213 u32 offset1, offset2;
214 int read1_fail = 0, read2_fail = 0;
215 int crc1_ok = 0, crc2_ok = 0;
218 int dev = CONFIG_SYS_MMC_ENV_DEV;
219 const char *errmsg = NULL;
221 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
222 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
224 #ifdef CONFIG_SPL_BUILD
228 mmc = find_mmc_device(dev);
230 errmsg = init_mmc_for_env(mmc);
236 if (mmc_get_env_addr(mmc, 0, &offset1) ||
237 mmc_get_env_addr(mmc, 1, &offset2)) {
242 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
243 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
245 if (read1_fail && read2_fail)
246 puts("*** Error - No Valid Environment Area found\n");
247 else if (read1_fail || read2_fail)
248 puts("*** Warning - some problems detected "
249 "reading environment; recovered successfully\n");
251 crc1_ok = !read1_fail &&
252 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
253 crc2_ok = !read2_fail &&
254 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
256 if (!crc1_ok && !crc2_ok) {
260 } else if (crc1_ok && !crc2_ok) {
262 } else if (!crc1_ok && crc2_ok) {
265 /* both ok - check serial */
266 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
268 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
270 else if (tmp_env1->flags > tmp_env2->flags)
272 else if (tmp_env2->flags > tmp_env1->flags)
274 else /* flags are equal - almost impossible */
280 if (gd->env_valid == 1)
285 env_flags = ep->flags;
286 env_import((char *)ep, 0);
290 fini_mmc_for_env(mmc);
293 set_default_env(errmsg);
296 #else /* ! CONFIG_ENV_OFFSET_REDUND */
297 void env_relocate_spec(void)
299 #if !defined(ENV_IS_EMBEDDED)
300 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
304 int dev = CONFIG_SYS_MMC_ENV_DEV;
307 #ifdef CONFIG_SPL_BUILD
311 mmc = find_mmc_device(dev);
313 errmsg = init_mmc_for_env(mmc);
319 if (mmc_get_env_addr(mmc, 0, &offset)) {
324 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
325 errmsg = "!read failed";
334 fini_mmc_for_env(mmc);
337 set_default_env(errmsg);
340 #endif /* CONFIG_ENV_OFFSET_REDUND */