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 #ifdef CONFIG_SPL_BUILD
89 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
90 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
92 puts("MMC partition switch failed\n");
97 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
100 static const char *init_mmc_for_env(struct mmc *mmc)
103 return "!No MMC card found";
106 return "!MMC init failed";
108 if (mmc_set_env_part(mmc))
109 return "!MMC partition switch failed";
114 static void fini_mmc_for_env(struct mmc *mmc)
116 #ifdef CONFIG_SYS_MMC_ENV_PART
117 int dev = mmc_get_env_dev();
119 #ifdef CONFIG_SPL_BUILD
122 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
126 #ifdef CONFIG_CMD_SAVEENV
127 static inline int write_env(struct mmc *mmc, unsigned long size,
128 unsigned long offset, const void *buffer)
130 uint blk_start, blk_cnt, n;
131 struct blk_desc *desc = mmc_get_blk_desc(mmc);
133 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
134 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
136 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
138 return (n == blk_cnt) ? 0 : -1;
141 #ifdef CONFIG_ENV_OFFSET_REDUND
142 static unsigned char env_flags;
147 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
148 int dev = mmc_get_env_dev();
149 struct mmc *mmc = find_mmc_device(dev);
154 errmsg = init_mmc_for_env(mmc);
156 printf("%s\n", errmsg);
160 ret = env_export(env_new);
164 #ifdef CONFIG_ENV_OFFSET_REDUND
165 env_new->flags = ++env_flags; /* increase the serial */
167 if (gd->env_valid == 1)
171 if (mmc_get_env_addr(mmc, copy, &offset)) {
176 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
177 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
186 #ifdef CONFIG_ENV_OFFSET_REDUND
187 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
191 fini_mmc_for_env(mmc);
194 #endif /* CONFIG_CMD_SAVEENV */
196 static inline int read_env(struct mmc *mmc, unsigned long size,
197 unsigned long offset, const void *buffer)
199 uint blk_start, blk_cnt, n;
200 struct blk_desc *desc = mmc_get_blk_desc(mmc);
202 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
203 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
205 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
207 return (n == blk_cnt) ? 0 : -1;
210 #ifdef CONFIG_ENV_OFFSET_REDUND
211 void env_relocate_spec(void)
213 #if !defined(ENV_IS_EMBEDDED)
215 u32 offset1, offset2;
216 int read1_fail = 0, read2_fail = 0;
217 int crc1_ok = 0, crc2_ok = 0;
220 int dev = mmc_get_env_dev();
221 const char *errmsg = NULL;
223 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
224 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
226 #ifdef CONFIG_SPL_BUILD
230 mmc = find_mmc_device(dev);
232 errmsg = init_mmc_for_env(mmc);
238 if (mmc_get_env_addr(mmc, 0, &offset1) ||
239 mmc_get_env_addr(mmc, 1, &offset2)) {
244 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
245 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
247 if (read1_fail && read2_fail)
248 puts("*** Error - No Valid Environment Area found\n");
249 else if (read1_fail || read2_fail)
250 puts("*** Warning - some problems detected "
251 "reading environment; recovered successfully\n");
253 crc1_ok = !read1_fail &&
254 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
255 crc2_ok = !read2_fail &&
256 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
258 if (!crc1_ok && !crc2_ok) {
262 } else if (crc1_ok && !crc2_ok) {
264 } else if (!crc1_ok && crc2_ok) {
267 /* both ok - check serial */
268 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
270 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
272 else if (tmp_env1->flags > tmp_env2->flags)
274 else if (tmp_env2->flags > tmp_env1->flags)
276 else /* flags are equal - almost impossible */
282 if (gd->env_valid == 1)
287 env_flags = ep->flags;
288 env_import((char *)ep, 0);
292 fini_mmc_for_env(mmc);
295 set_default_env(errmsg);
298 #else /* ! CONFIG_ENV_OFFSET_REDUND */
299 void env_relocate_spec(void)
301 #if !defined(ENV_IS_EMBEDDED)
302 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
306 int dev = mmc_get_env_dev();
309 #ifdef CONFIG_SPL_BUILD
313 mmc = find_mmc_device(dev);
315 errmsg = init_mmc_for_env(mmc);
321 if (mmc_get_env_addr(mmc, 0, &offset)) {
326 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
327 errmsg = "!read failed";
336 fini_mmc_for_env(mmc);
339 set_default_env(errmsg);
342 #endif /* CONFIG_ENV_OFFSET_REDUND */