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;
57 __weak int mmc_get_env_dev(void)
59 return CONFIG_SYS_MMC_ENV_DEV;
65 gd->env_addr = (ulong)&default_environment[0];
71 #ifdef CONFIG_SYS_MMC_ENV_PART
72 __weak uint mmc_get_env_part(struct mmc *mmc)
74 return CONFIG_SYS_MMC_ENV_PART;
77 static unsigned char env_mmc_orig_hwpart;
79 static int mmc_set_env_part(struct mmc *mmc)
81 uint part = mmc_get_env_part(mmc);
82 int dev = mmc_get_env_dev();
85 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
86 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
88 puts("MMC partition switch failed\n");
93 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
96 static const char *init_mmc_for_env(struct mmc *mmc)
99 return "!No MMC card found";
102 return "!MMC init failed";
104 if (mmc_set_env_part(mmc))
105 return "!MMC partition switch failed";
110 static void fini_mmc_for_env(struct mmc *mmc)
112 #ifdef CONFIG_SYS_MMC_ENV_PART
113 int dev = mmc_get_env_dev();
115 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
119 #ifdef CONFIG_CMD_SAVEENV
120 static inline int write_env(struct mmc *mmc, unsigned long size,
121 unsigned long offset, const void *buffer)
123 uint blk_start, blk_cnt, n;
124 struct blk_desc *desc = mmc_get_blk_desc(mmc);
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 = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
131 return (n == blk_cnt) ? 0 : -1;
134 #ifdef CONFIG_ENV_OFFSET_REDUND
135 static unsigned char env_flags;
140 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
141 int dev = mmc_get_env_dev();
142 struct mmc *mmc = find_mmc_device(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 " : "", dev);
170 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
179 #ifdef CONFIG_ENV_OFFSET_REDUND
180 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
184 fini_mmc_for_env(mmc);
187 #endif /* CONFIG_CMD_SAVEENV */
189 static inline int read_env(struct mmc *mmc, unsigned long size,
190 unsigned long offset, const void *buffer)
192 uint blk_start, blk_cnt, n;
193 struct blk_desc *desc = mmc_get_blk_desc(mmc);
195 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
196 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
198 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
200 return (n == blk_cnt) ? 0 : -1;
203 #ifdef CONFIG_ENV_OFFSET_REDUND
204 void env_relocate_spec(void)
206 #if !defined(ENV_IS_EMBEDDED)
208 u32 offset1, offset2;
209 int read1_fail = 0, read2_fail = 0;
210 int crc1_ok = 0, crc2_ok = 0;
213 int dev = mmc_get_env_dev();
214 const char *errmsg = NULL;
216 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
217 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
219 mmc = find_mmc_device(dev);
221 errmsg = init_mmc_for_env(mmc);
227 if (mmc_get_env_addr(mmc, 0, &offset1) ||
228 mmc_get_env_addr(mmc, 1, &offset2)) {
233 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
234 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
236 if (read1_fail && read2_fail)
237 puts("*** Error - No Valid Environment Area found\n");
238 else if (read1_fail || read2_fail)
239 puts("*** Warning - some problems detected "
240 "reading environment; recovered successfully\n");
242 crc1_ok = !read1_fail &&
243 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
244 crc2_ok = !read2_fail &&
245 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
247 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(errmsg);
287 #else /* ! CONFIG_ENV_OFFSET_REDUND */
288 void env_relocate_spec(void)
290 #if !defined(ENV_IS_EMBEDDED)
291 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
295 int dev = mmc_get_env_dev();
298 mmc = find_mmc_device(dev);
300 errmsg = init_mmc_for_env(mmc);
306 if (mmc_get_env_addr(mmc, 0, &offset)) {
311 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
312 errmsg = "!read failed";
321 fini_mmc_for_env(mmc);
324 set_default_env(errmsg);
327 #endif /* CONFIG_ENV_OFFSET_REDUND */