2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
14 #include <linux/stddef.h>
23 #define STR(X) __STR(X)
25 #if defined(CONFIG_ENV_SIZE_REDUND) && \
26 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
27 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
30 DECLARE_GLOBAL_DATA_PTR;
32 #if !defined(CONFIG_ENV_OFFSET)
33 #define CONFIG_ENV_OFFSET 0
36 #if CONFIG_IS_ENABLED(OF_CONTROL)
37 static inline int mmc_offset_try_partition(const char *str, s64 *val)
39 disk_partition_t info;
40 struct blk_desc *desc;
43 ret = blk_get_device_by_str("mmc", STR(CONFIG_SYS_MMC_ENV_DEV), &desc);
48 ret = part_get_info(desc, i, &info);
52 if (!strncmp((const char *)info.name, str, sizeof(str)))
56 /* round up to info.blksz */
57 len = (CONFIG_ENV_SIZE + info.blksz - 1) & ~(info.blksz - 1);
59 /* use the top of the partion for the environment */
60 *val = (info.start + info.size - 1) - len / info.blksz;
65 static inline s64 mmc_offset(int copy)
68 const char *offset_redund;
69 const char *partition;
72 .offset_redund = "u-boot,mmc-env-offset-redundant",
73 .partition = "u-boot,mmc-env-partition",
74 .offset = "u-boot,mmc-env-offset",
76 s64 val = 0, defvalue;
81 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
82 str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition);
84 /* try to place the environment at end of the partition */
85 err = mmc_offset_try_partition(str, &val);
90 defvalue = CONFIG_ENV_OFFSET;
91 propname = dt_prop.offset;
93 #if defined(CONFIG_ENV_OFFSET_REDUND)
95 defvalue = CONFIG_ENV_OFFSET_REDUND;
96 propname = dt_prop.offset_redund;
99 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
102 static inline s64 mmc_offset(int copy)
104 s64 offset = CONFIG_ENV_OFFSET;
106 #if defined(CONFIG_ENV_OFFSET_REDUND)
108 offset = CONFIG_ENV_OFFSET_REDUND;
114 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
116 s64 offset = mmc_offset(copy);
119 offset += mmc->capacity;
126 __weak int mmc_get_env_dev(void)
128 return CONFIG_SYS_MMC_ENV_DEV;
131 #ifdef CONFIG_SYS_MMC_ENV_PART
132 __weak uint mmc_get_env_part(struct mmc *mmc)
134 return CONFIG_SYS_MMC_ENV_PART;
137 static unsigned char env_mmc_orig_hwpart;
139 static int mmc_set_env_part(struct mmc *mmc)
141 uint part = mmc_get_env_part(mmc);
142 int dev = mmc_get_env_dev();
145 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
146 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
148 puts("MMC partition switch failed\n");
153 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
156 static const char *init_mmc_for_env(struct mmc *mmc)
159 return "!No MMC card found";
164 if (blk_get_from_parent(mmc->dev, &dev))
165 return "!No block device";
168 return "!MMC init failed";
170 if (mmc_set_env_part(mmc))
171 return "!MMC partition switch failed";
176 static void fini_mmc_for_env(struct mmc *mmc)
178 #ifdef CONFIG_SYS_MMC_ENV_PART
179 int dev = mmc_get_env_dev();
181 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
185 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
186 static inline int write_env(struct mmc *mmc, unsigned long size,
187 unsigned long offset, const void *buffer)
189 uint blk_start, blk_cnt, n;
190 struct blk_desc *desc = mmc_get_blk_desc(mmc);
192 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
193 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
195 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
197 return (n == blk_cnt) ? 0 : -1;
200 static int env_mmc_save(void)
202 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
203 int dev = mmc_get_env_dev();
204 struct mmc *mmc = find_mmc_device(dev);
209 errmsg = init_mmc_for_env(mmc);
211 printf("%s\n", errmsg);
215 ret = env_export(env_new);
219 #ifdef CONFIG_ENV_OFFSET_REDUND
220 if (gd->env_valid == ENV_VALID)
224 if (mmc_get_env_addr(mmc, copy, &offset)) {
229 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
230 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
238 #ifdef CONFIG_ENV_OFFSET_REDUND
239 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
243 fini_mmc_for_env(mmc);
246 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
248 static inline int read_env(struct mmc *mmc, unsigned long size,
249 unsigned long offset, const void *buffer)
251 uint blk_start, blk_cnt, n;
252 struct blk_desc *desc = mmc_get_blk_desc(mmc);
254 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
255 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
257 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
259 return (n == blk_cnt) ? 0 : -1;
262 #ifdef CONFIG_ENV_OFFSET_REDUND
263 static int env_mmc_load(void)
265 #if !defined(ENV_IS_EMBEDDED)
267 u32 offset1, offset2;
268 int read1_fail = 0, read2_fail = 0;
270 int dev = mmc_get_env_dev();
271 const char *errmsg = NULL;
273 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
274 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
276 mmc_initialize(NULL);
278 mmc = find_mmc_device(dev);
280 errmsg = init_mmc_for_env(mmc);
286 if (mmc_get_env_addr(mmc, 0, &offset1) ||
287 mmc_get_env_addr(mmc, 1, &offset2)) {
292 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
293 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
295 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
299 fini_mmc_for_env(mmc);
302 set_default_env(errmsg);
307 #else /* ! CONFIG_ENV_OFFSET_REDUND */
308 static int env_mmc_load(void)
310 #if !defined(ENV_IS_EMBEDDED)
311 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
315 int dev = mmc_get_env_dev();
318 mmc = find_mmc_device(dev);
320 errmsg = init_mmc_for_env(mmc);
326 if (mmc_get_env_addr(mmc, 0, &offset)) {
331 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
332 errmsg = "!read failed";
337 ret = env_import(buf, 1);
340 fini_mmc_for_env(mmc);
343 set_default_env(errmsg);
347 #endif /* CONFIG_ENV_OFFSET_REDUND */
349 U_BOOT_ENV_LOCATION(mmc) = {
350 .location = ENVL_MMC,
352 .load = env_mmc_load,
353 #ifndef CONFIG_SPL_BUILD
354 .save = env_save_ptr(env_mmc_save),