1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
12 #include <env_internal.h>
14 #include <linux/stddef.h>
23 #define STR(X) __STR(X)
25 DECLARE_GLOBAL_DATA_PTR;
27 #if CONFIG_IS_ENABLED(OF_CONTROL)
28 static inline int mmc_offset_try_partition(const char *str, s64 *val)
30 disk_partition_t info;
31 struct blk_desc *desc;
34 ret = blk_get_device_by_str("mmc", STR(CONFIG_SYS_MMC_ENV_DEV), &desc);
39 ret = part_get_info(desc, i, &info);
43 if (!strncmp((const char *)info.name, str, sizeof(str)))
47 /* round up to info.blksz */
48 len = (CONFIG_ENV_SIZE + info.blksz - 1) & ~(info.blksz - 1);
50 /* use the top of the partion for the environment */
51 *val = (info.start + info.size - 1) - len / info.blksz;
56 static inline s64 mmc_offset(int copy)
59 const char *offset_redund;
60 const char *partition;
63 .offset_redund = "u-boot,mmc-env-offset-redundant",
64 .partition = "u-boot,mmc-env-partition",
65 .offset = "u-boot,mmc-env-offset",
67 s64 val = 0, defvalue;
72 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
73 str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition);
75 /* try to place the environment at end of the partition */
76 err = mmc_offset_try_partition(str, &val);
81 defvalue = CONFIG_ENV_OFFSET;
82 propname = dt_prop.offset;
84 #if defined(CONFIG_ENV_OFFSET_REDUND)
86 defvalue = CONFIG_ENV_OFFSET_REDUND;
87 propname = dt_prop.offset_redund;
90 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
93 static inline s64 mmc_offset(int copy)
95 s64 offset = CONFIG_ENV_OFFSET;
97 #if defined(CONFIG_ENV_OFFSET_REDUND)
99 offset = CONFIG_ENV_OFFSET_REDUND;
105 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
107 s64 offset = mmc_offset(copy);
110 offset += mmc->capacity;
117 __weak int mmc_get_env_dev(void)
119 return CONFIG_SYS_MMC_ENV_DEV;
122 #ifdef CONFIG_SYS_MMC_ENV_PART
123 __weak uint mmc_get_env_part(struct mmc *mmc)
125 return CONFIG_SYS_MMC_ENV_PART;
128 static unsigned char env_mmc_orig_hwpart;
130 static int mmc_set_env_part(struct mmc *mmc)
132 uint part = mmc_get_env_part(mmc);
133 int dev = mmc_get_env_dev();
136 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
137 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
139 puts("MMC partition switch failed\n");
144 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
147 static const char *init_mmc_for_env(struct mmc *mmc)
150 return "No MMC card found";
152 #if CONFIG_IS_ENABLED(BLK)
155 if (blk_get_from_parent(mmc->dev, &dev))
156 return "No block device";
159 return "MMC init failed";
161 if (mmc_set_env_part(mmc))
162 return "MMC partition switch failed";
167 static void fini_mmc_for_env(struct mmc *mmc)
169 #ifdef CONFIG_SYS_MMC_ENV_PART
170 int dev = mmc_get_env_dev();
172 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
176 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
177 static inline int write_env(struct mmc *mmc, unsigned long size,
178 unsigned long offset, const void *buffer)
180 uint blk_start, blk_cnt, n;
181 struct blk_desc *desc = mmc_get_blk_desc(mmc);
183 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
184 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
186 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
188 return (n == blk_cnt) ? 0 : -1;
191 static int env_mmc_save(void)
193 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
194 int dev = mmc_get_env_dev();
195 struct mmc *mmc = find_mmc_device(dev);
200 errmsg = init_mmc_for_env(mmc);
202 printf("%s\n", errmsg);
206 ret = env_export(env_new);
210 #ifdef CONFIG_ENV_OFFSET_REDUND
211 if (gd->env_valid == ENV_VALID)
215 if (mmc_get_env_addr(mmc, copy, &offset)) {
220 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
221 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
229 #ifdef CONFIG_ENV_OFFSET_REDUND
230 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
234 fini_mmc_for_env(mmc);
238 #if defined(CONFIG_CMD_ERASEENV)
239 static inline int erase_env(struct mmc *mmc, unsigned long size,
240 unsigned long offset)
242 uint blk_start, blk_cnt, n;
243 struct blk_desc *desc = mmc_get_blk_desc(mmc);
245 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
246 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
248 n = blk_derase(desc, blk_start, blk_cnt);
249 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
251 return (n == blk_cnt) ? 0 : 1;
254 static int env_mmc_erase(void)
256 int dev = mmc_get_env_dev();
257 struct mmc *mmc = find_mmc_device(dev);
262 errmsg = init_mmc_for_env(mmc);
264 printf("%s\n", errmsg);
268 if (mmc_get_env_addr(mmc, copy, &offset))
269 return CMD_RET_FAILURE;
271 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
273 #ifdef CONFIG_ENV_OFFSET_REDUND
276 if (mmc_get_env_addr(mmc, copy, &offset))
277 return CMD_RET_FAILURE;
279 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
284 #endif /* CONFIG_CMD_ERASEENV */
285 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
287 static inline int read_env(struct mmc *mmc, unsigned long size,
288 unsigned long offset, const void *buffer)
290 uint blk_start, blk_cnt, n;
291 struct blk_desc *desc = mmc_get_blk_desc(mmc);
293 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
294 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
296 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
298 return (n == blk_cnt) ? 0 : -1;
301 #ifdef CONFIG_ENV_OFFSET_REDUND
302 static int env_mmc_load(void)
304 #if !defined(ENV_IS_EMBEDDED)
306 u32 offset1, offset2;
307 int read1_fail = 0, read2_fail = 0;
309 int dev = mmc_get_env_dev();
310 const char *errmsg = NULL;
312 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
313 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
315 mmc_initialize(NULL);
317 mmc = find_mmc_device(dev);
319 errmsg = init_mmc_for_env(mmc);
325 if (mmc_get_env_addr(mmc, 0, &offset1) ||
326 mmc_get_env_addr(mmc, 1, &offset2)) {
331 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
332 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
334 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
338 fini_mmc_for_env(mmc);
341 env_set_default(errmsg, 0);
346 #else /* ! CONFIG_ENV_OFFSET_REDUND */
347 static int env_mmc_load(void)
349 #if !defined(ENV_IS_EMBEDDED)
350 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
354 int dev = mmc_get_env_dev();
358 mmc = find_mmc_device(dev);
360 errmsg = init_mmc_for_env(mmc);
366 if (mmc_get_env_addr(mmc, 0, &offset)) {
371 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
372 errmsg = "!read failed";
377 ret = env_import(buf, 1);
380 gd->env_addr = (ulong)&ep->data;
384 fini_mmc_for_env(mmc);
387 env_set_default(errmsg, 0);
391 #endif /* CONFIG_ENV_OFFSET_REDUND */
393 U_BOOT_ENV_LOCATION(mmc) = {
394 .location = ENVL_MMC,
396 .load = env_mmc_load,
397 #ifndef CONFIG_SPL_BUILD
398 .save = env_save_ptr(env_mmc_save),
399 #if defined(CONFIG_CMD_ERASEENV)
400 .erase = env_mmc_erase,