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 static int init_mmc_for_env(struct mmc *mmc)
68 puts("No MMC card found\n");
73 puts("MMC init failed\n");
77 #ifdef CONFIG_SYS_MMC_ENV_PART
78 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
79 if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
80 CONFIG_SYS_MMC_ENV_PART)) {
81 puts("MMC partition switch failed\n");
90 static void fini_mmc_for_env(struct mmc *mmc)
92 #ifdef CONFIG_SYS_MMC_ENV_PART
93 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
94 mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
99 #ifdef CONFIG_CMD_SAVEENV
100 static inline int write_env(struct mmc *mmc, unsigned long size,
101 unsigned long offset, const void *buffer)
103 uint blk_start, blk_cnt, n;
105 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
106 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
108 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
109 blk_cnt, (u_char *)buffer);
111 return (n == blk_cnt) ? 0 : -1;
114 #ifdef CONFIG_ENV_OFFSET_REDUND
115 static unsigned char env_flags;
120 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
123 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
127 if (init_mmc_for_env(mmc))
130 res = (char *)&env_new->data;
131 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
133 error("Cannot export environment: errno = %d\n", errno);
138 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
140 #ifdef CONFIG_ENV_OFFSET_REDUND
141 env_new->flags = ++env_flags; /* increase the serial */
143 if (gd->env_valid == 1)
147 if (mmc_get_env_addr(mmc, copy, &offset)) {
152 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
153 CONFIG_SYS_MMC_ENV_DEV);
154 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
163 #ifdef CONFIG_ENV_OFFSET_REDUND
164 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
168 fini_mmc_for_env(mmc);
171 #endif /* CONFIG_CMD_SAVEENV */
173 static inline int read_env(struct mmc *mmc, unsigned long size,
174 unsigned long offset, const void *buffer)
176 uint blk_start, blk_cnt, n;
178 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
179 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
181 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
182 blk_cnt, (uchar *)buffer);
184 return (n == blk_cnt) ? 0 : -1;
187 #ifdef CONFIG_ENV_OFFSET_REDUND
188 void env_relocate_spec(void)
190 #if !defined(ENV_IS_EMBEDDED)
191 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
192 u32 offset1, offset2;
193 int read1_fail = 0, read2_fail = 0;
194 int crc1_ok = 0, crc2_ok = 0;
195 env_t *ep, *tmp_env1, *tmp_env2;
198 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
199 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
200 if (tmp_env1 == NULL || tmp_env2 == NULL) {
201 puts("Can't allocate buffers for environment\n");
206 if (init_mmc_for_env(mmc)) {
211 if (mmc_get_env_addr(mmc, 0, &offset1) ||
212 mmc_get_env_addr(mmc, 1, &offset2)) {
217 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
218 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
220 if (read1_fail && read2_fail)
221 puts("*** Error - No Valid Environment Area found\n");
222 else if (read1_fail || read2_fail)
223 puts("*** Warning - some problems detected "
224 "reading environment; recovered successfully\n");
226 crc1_ok = !read1_fail &&
227 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
228 crc2_ok = !read2_fail &&
229 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
231 if (!crc1_ok && !crc2_ok) {
234 } else if (crc1_ok && !crc2_ok) {
236 } else if (!crc1_ok && crc2_ok) {
239 /* both ok - check serial */
240 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
242 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
244 else if (tmp_env1->flags > tmp_env2->flags)
246 else if (tmp_env2->flags > tmp_env1->flags)
248 else /* flags are equal - almost impossible */
254 if (gd->env_valid == 1)
259 env_flags = ep->flags;
260 env_import((char *)ep, 0);
264 fini_mmc_for_env(mmc);
267 set_default_env(NULL);
273 #else /* ! CONFIG_ENV_OFFSET_REDUND */
274 void env_relocate_spec(void)
276 #if !defined(ENV_IS_EMBEDDED)
277 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
278 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
282 if (init_mmc_for_env(mmc)) {
287 if (mmc_get_env_addr(mmc, 0, &offset)) {
292 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
301 fini_mmc_for_env(mmc);
304 set_default_env(NULL);
307 #endif /* CONFIG_ENV_OFFSET_REDUND */