2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
14 #include <linux/stddef.h>
21 #if defined(CONFIG_ENV_SIZE_REDUND) && \
22 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
23 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
26 DECLARE_GLOBAL_DATA_PTR;
28 #if !defined(CONFIG_ENV_OFFSET)
29 #define CONFIG_ENV_OFFSET 0
32 #if CONFIG_IS_ENABLED(OF_CONTROL)
33 static inline s64 mmc_offset(int copy)
35 const char *propname = "u-boot,mmc-env-offset";
36 s64 defvalue = CONFIG_ENV_OFFSET;
38 #if defined(CONFIG_ENV_OFFSET_REDUND)
40 propname = "u-boot,mmc-env-offset-redundant";
41 defvalue = CONFIG_ENV_OFFSET_REDUND;
45 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
48 static inline s64 mmc_offset(int copy)
50 s64 offset = CONFIG_ENV_OFFSET;
52 #if defined(CONFIG_ENV_OFFSET_REDUND)
54 offset = CONFIG_ENV_OFFSET_REDUND;
60 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
62 s64 offset = mmc_offset(copy);
65 offset += mmc->capacity;
72 __weak int mmc_get_env_dev(void)
74 return CONFIG_SYS_MMC_ENV_DEV;
77 #ifdef CONFIG_SYS_MMC_ENV_PART
78 __weak uint mmc_get_env_part(struct mmc *mmc)
80 return CONFIG_SYS_MMC_ENV_PART;
83 static unsigned char env_mmc_orig_hwpart;
85 static int mmc_set_env_part(struct mmc *mmc)
87 uint part = mmc_get_env_part(mmc);
88 int dev = mmc_get_env_dev();
91 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
92 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
94 puts("MMC partition switch failed\n");
99 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
102 static const char *init_mmc_for_env(struct mmc *mmc)
105 return "!No MMC card found";
110 if (blk_get_from_parent(mmc->dev, &dev))
111 return "!No block device";
114 return "!MMC init failed";
116 if (mmc_set_env_part(mmc))
117 return "!MMC partition switch failed";
122 static void fini_mmc_for_env(struct mmc *mmc)
124 #ifdef CONFIG_SYS_MMC_ENV_PART
125 int dev = mmc_get_env_dev();
127 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
131 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
132 static inline int write_env(struct mmc *mmc, unsigned long size,
133 unsigned long offset, const void *buffer)
135 uint blk_start, blk_cnt, n;
136 struct blk_desc *desc = mmc_get_blk_desc(mmc);
138 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
139 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
141 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
143 return (n == blk_cnt) ? 0 : -1;
146 static int env_mmc_save(void)
148 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
149 int dev = mmc_get_env_dev();
150 struct mmc *mmc = find_mmc_device(dev);
155 errmsg = init_mmc_for_env(mmc);
157 printf("%s\n", errmsg);
161 ret = env_export(env_new);
165 #ifdef CONFIG_ENV_OFFSET_REDUND
166 if (gd->env_valid == ENV_VALID)
170 if (mmc_get_env_addr(mmc, copy, &offset)) {
175 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
176 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
185 #ifdef CONFIG_ENV_OFFSET_REDUND
186 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
190 fini_mmc_for_env(mmc);
193 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
195 static inline int read_env(struct mmc *mmc, unsigned long size,
196 unsigned long offset, const void *buffer)
198 uint blk_start, blk_cnt, n;
199 struct blk_desc *desc = mmc_get_blk_desc(mmc);
201 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
202 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
204 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
206 return (n == blk_cnt) ? 0 : -1;
209 #ifdef CONFIG_ENV_OFFSET_REDUND
210 static int env_mmc_load(void)
212 #if !defined(ENV_IS_EMBEDDED)
214 u32 offset1, offset2;
215 int read1_fail = 0, read2_fail = 0;
217 int dev = mmc_get_env_dev();
218 const char *errmsg = NULL;
220 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
221 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
223 mmc = find_mmc_device(dev);
225 errmsg = init_mmc_for_env(mmc);
231 if (mmc_get_env_addr(mmc, 0, &offset1) ||
232 mmc_get_env_addr(mmc, 1, &offset2)) {
237 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
238 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
240 if (read1_fail && read2_fail)
241 puts("*** Error - No Valid Environment Area found\n");
242 else if (read1_fail || read2_fail)
243 puts("*** Warning - some problems detected "
244 "reading environment; recovered successfully\n");
246 if (read1_fail && read2_fail) {
250 } else if (!read1_fail && read2_fail) {
251 gd->env_valid = ENV_VALID;
252 env_import((char *)tmp_env1, 1);
253 } else if (read1_fail && !read2_fail) {
254 gd->env_valid = ENV_REDUND;
255 env_import((char *)tmp_env2, 1);
257 env_import_redund((char *)tmp_env1, (char *)tmp_env2);
263 fini_mmc_for_env(mmc);
266 set_default_env(errmsg);
271 #else /* ! CONFIG_ENV_OFFSET_REDUND */
272 static int env_mmc_load(void)
274 #if !defined(ENV_IS_EMBEDDED)
275 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
279 int dev = mmc_get_env_dev();
282 mmc = find_mmc_device(dev);
284 errmsg = init_mmc_for_env(mmc);
290 if (mmc_get_env_addr(mmc, 0, &offset)) {
295 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
296 errmsg = "!read failed";
305 fini_mmc_for_env(mmc);
308 set_default_env(errmsg);
312 #endif /* CONFIG_ENV_OFFSET_REDUND */
314 U_BOOT_ENV_LOCATION(mmc) = {
315 .location = ENVL_MMC,
317 .load = env_mmc_load,
318 #ifndef CONFIG_SPL_BUILD
319 .save = env_save_ptr(env_mmc_save),