2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
13 #include <linux/stddef.h>
20 #if defined(CONFIG_ENV_SIZE_REDUND) && \
21 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
22 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
25 char *env_name_spec = "MMC";
27 #ifdef ENV_IS_EMBEDDED
28 env_t *env_ptr = &environment;
29 #else /* ! ENV_IS_EMBEDDED */
31 #endif /* ENV_IS_EMBEDDED */
33 DECLARE_GLOBAL_DATA_PTR;
35 #if !defined(CONFIG_ENV_OFFSET)
36 #define CONFIG_ENV_OFFSET 0
39 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
43 offset = CONFIG_ENV_OFFSET;
44 #ifdef CONFIG_ENV_OFFSET_REDUND
46 offset = CONFIG_ENV_OFFSET_REDUND;
50 offset += mmc->capacity;
60 gd->env_addr = (ulong)&default_environment[0];
66 #ifdef CONFIG_SYS_MMC_ENV_PART
67 __weak uint mmc_get_env_part(struct mmc *mmc)
69 return CONFIG_SYS_MMC_ENV_PART;
72 static int mmc_set_env_part(struct mmc *mmc)
74 uint part = mmc_get_env_part(mmc);
75 int dev = CONFIG_SYS_MMC_ENV_DEV;
78 #ifdef CONFIG_SPL_BUILD
82 if (part != mmc->part_num) {
83 ret = mmc_switch_part(dev, part);
85 puts("MMC partition switch failed\n");
91 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
94 static const char *init_mmc_for_env(struct mmc *mmc)
97 return "!No MMC card found";
100 return "!MMC init failed";
102 if (mmc_set_env_part(mmc))
103 return "!MMC partition switch failed";
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 (mmc_get_env_part(mmc) != 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(&mmc->block_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);
148 errmsg = init_mmc_for_env(mmc);
150 printf("%s\n", errmsg);
154 ret = env_export(env_new);
158 #ifdef CONFIG_ENV_OFFSET_REDUND
159 env_new->flags = ++env_flags; /* increase the serial */
161 if (gd->env_valid == 1)
165 if (mmc_get_env_addr(mmc, copy, &offset)) {
170 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
171 CONFIG_SYS_MMC_ENV_DEV);
172 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
181 #ifdef CONFIG_ENV_OFFSET_REDUND
182 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
186 fini_mmc_for_env(mmc);
189 #endif /* CONFIG_CMD_SAVEENV */
191 static inline int read_env(struct mmc *mmc, unsigned long size,
192 unsigned long offset, const void *buffer)
194 uint blk_start, blk_cnt, n;
196 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
197 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
199 n = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
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;
216 const char *errmsg = NULL;
218 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
219 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
221 #ifdef CONFIG_SPL_BUILD
225 mmc = find_mmc_device(dev);
227 errmsg = init_mmc_for_env(mmc);
233 if (mmc_get_env_addr(mmc, 0, &offset1) ||
234 mmc_get_env_addr(mmc, 1, &offset2)) {
239 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
240 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
242 if (read1_fail && read2_fail)
243 puts("*** Error - No Valid Environment Area found\n");
244 else if (read1_fail || read2_fail)
245 puts("*** Warning - some problems detected "
246 "reading environment; recovered successfully\n");
248 crc1_ok = !read1_fail &&
249 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
250 crc2_ok = !read2_fail &&
251 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
253 if (!crc1_ok && !crc2_ok) {
257 } else if (crc1_ok && !crc2_ok) {
259 } else if (!crc1_ok && crc2_ok) {
262 /* both ok - check serial */
263 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
265 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
267 else if (tmp_env1->flags > tmp_env2->flags)
269 else if (tmp_env2->flags > tmp_env1->flags)
271 else /* flags are equal - almost impossible */
277 if (gd->env_valid == 1)
282 env_flags = ep->flags;
283 env_import((char *)ep, 0);
287 fini_mmc_for_env(mmc);
290 set_default_env(errmsg);
293 #else /* ! CONFIG_ENV_OFFSET_REDUND */
294 void env_relocate_spec(void)
296 #if !defined(ENV_IS_EMBEDDED)
297 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
301 int dev = CONFIG_SYS_MMC_ENV_DEV;
304 #ifdef CONFIG_SPL_BUILD
308 mmc = find_mmc_device(dev);
310 errmsg = init_mmc_for_env(mmc);
316 if (mmc_get_env_addr(mmc, 0, &offset)) {
321 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
322 errmsg = "!read failed";
331 fini_mmc_for_env(mmc);
334 set_default_env(errmsg);
337 #endif /* CONFIG_ENV_OFFSET_REDUND */