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 !defined(CONFIG_SYS_MMC_ENV_DEV)
28 #define CONFIG_SYS_MMC_ENV_DEV 0
31 __weak int mmc_get_env_dev(void)
33 return CONFIG_SYS_MMC_ENV_DEV;
36 #if CONFIG_IS_ENABLED(OF_CONTROL)
37 static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
39 struct disk_partition info;
40 struct blk_desc *desc;
44 snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev());
45 ret = blk_get_device_by_str("mmc", dev_str, &desc);
50 ret = part_get_info(desc, i, &info);
54 if (!strncmp((const char *)info.name, str, sizeof(str)))
58 /* round up to info.blksz */
59 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
61 /* use the top of the partion for the environment */
62 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
67 static inline s64 mmc_offset(int copy)
70 const char *offset_redund;
71 const char *partition;
74 .offset_redund = "u-boot,mmc-env-offset-redundant",
75 .partition = "u-boot,mmc-env-partition",
76 .offset = "u-boot,mmc-env-offset",
78 s64 val = 0, defvalue;
83 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
84 str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition);
86 /* try to place the environment at end of the partition */
87 err = mmc_offset_try_partition(str, copy, &val);
92 defvalue = CONFIG_ENV_OFFSET;
93 propname = dt_prop.offset;
95 #if defined(CONFIG_ENV_OFFSET_REDUND)
97 defvalue = CONFIG_ENV_OFFSET_REDUND;
98 propname = dt_prop.offset_redund;
101 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
104 static inline s64 mmc_offset(int copy)
106 s64 offset = CONFIG_ENV_OFFSET;
108 #if defined(CONFIG_ENV_OFFSET_REDUND)
110 offset = CONFIG_ENV_OFFSET_REDUND;
116 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
118 s64 offset = mmc_offset(copy);
121 offset += mmc->capacity;
128 #ifdef CONFIG_SYS_MMC_ENV_PART
129 __weak uint mmc_get_env_part(struct mmc *mmc)
131 return CONFIG_SYS_MMC_ENV_PART;
134 static unsigned char env_mmc_orig_hwpart;
136 static int mmc_set_env_part(struct mmc *mmc)
138 uint part = mmc_get_env_part(mmc);
139 int dev = mmc_get_env_dev();
142 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
143 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
145 puts("MMC partition switch failed\n");
150 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
153 static const char *init_mmc_for_env(struct mmc *mmc)
156 return "No MMC card found";
158 #if CONFIG_IS_ENABLED(BLK)
161 if (blk_get_from_parent(mmc->dev, &dev))
162 return "No block device";
165 return "MMC init failed";
167 if (mmc_set_env_part(mmc))
168 return "MMC partition switch failed";
173 static void fini_mmc_for_env(struct mmc *mmc)
175 #ifdef CONFIG_SYS_MMC_ENV_PART
176 int dev = mmc_get_env_dev();
178 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
182 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
183 static inline int write_env(struct mmc *mmc, unsigned long size,
184 unsigned long offset, const void *buffer)
186 uint blk_start, blk_cnt, n;
187 struct blk_desc *desc = mmc_get_blk_desc(mmc);
189 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
190 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
192 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
194 return (n == blk_cnt) ? 0 : -1;
197 static int env_mmc_save(void)
199 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
200 int dev = mmc_get_env_dev();
201 struct mmc *mmc = find_mmc_device(dev);
206 errmsg = init_mmc_for_env(mmc);
208 printf("%s\n", errmsg);
212 ret = env_export(env_new);
216 #ifdef CONFIG_ENV_OFFSET_REDUND
217 if (gd->env_valid == ENV_VALID)
221 if (mmc_get_env_addr(mmc, copy, &offset)) {
226 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
227 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
235 #ifdef CONFIG_ENV_OFFSET_REDUND
236 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
240 fini_mmc_for_env(mmc);
244 #if defined(CONFIG_CMD_ERASEENV)
245 static inline int erase_env(struct mmc *mmc, unsigned long size,
246 unsigned long offset)
248 uint blk_start, blk_cnt, n;
249 struct blk_desc *desc = mmc_get_blk_desc(mmc);
251 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
252 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
254 n = blk_derase(desc, blk_start, blk_cnt);
255 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
257 return (n == blk_cnt) ? 0 : 1;
260 static int env_mmc_erase(void)
262 int dev = mmc_get_env_dev();
263 struct mmc *mmc = find_mmc_device(dev);
268 errmsg = init_mmc_for_env(mmc);
270 printf("%s\n", errmsg);
274 if (mmc_get_env_addr(mmc, copy, &offset))
275 return CMD_RET_FAILURE;
277 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
279 #ifdef CONFIG_ENV_OFFSET_REDUND
282 if (mmc_get_env_addr(mmc, copy, &offset))
283 return CMD_RET_FAILURE;
285 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
290 #endif /* CONFIG_CMD_ERASEENV */
291 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
293 static inline int read_env(struct mmc *mmc, unsigned long size,
294 unsigned long offset, const void *buffer)
296 uint blk_start, blk_cnt, n;
297 struct blk_desc *desc = mmc_get_blk_desc(mmc);
299 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
300 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
302 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
304 return (n == blk_cnt) ? 0 : -1;
307 #ifdef CONFIG_ENV_OFFSET_REDUND
308 static int env_mmc_load(void)
310 #if !defined(ENV_IS_EMBEDDED)
312 u32 offset1, offset2;
313 int read1_fail = 0, read2_fail = 0;
315 int dev = mmc_get_env_dev();
316 const char *errmsg = NULL;
318 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
319 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
321 mmc_initialize(NULL);
323 mmc = find_mmc_device(dev);
325 errmsg = init_mmc_for_env(mmc);
331 if (mmc_get_env_addr(mmc, 0, &offset1) ||
332 mmc_get_env_addr(mmc, 1, &offset2)) {
337 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
338 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
340 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
344 fini_mmc_for_env(mmc);
347 env_set_default(errmsg, 0);
352 #else /* ! CONFIG_ENV_OFFSET_REDUND */
353 static int env_mmc_load(void)
355 #if !defined(ENV_IS_EMBEDDED)
356 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
360 int dev = mmc_get_env_dev();
364 mmc = find_mmc_device(dev);
366 errmsg = init_mmc_for_env(mmc);
372 if (mmc_get_env_addr(mmc, 0, &offset)) {
377 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
378 errmsg = "!read failed";
383 ret = env_import(buf, 1);
386 gd->env_addr = (ulong)&ep->data;
390 fini_mmc_for_env(mmc);
393 env_set_default(errmsg, 0);
397 #endif /* CONFIG_ENV_OFFSET_REDUND */
399 U_BOOT_ENV_LOCATION(mmc) = {
400 .location = ENVL_MMC,
402 .load = env_mmc_load,
403 #ifndef CONFIG_SPL_BUILD
404 .save = env_save_ptr(env_mmc_save),
405 #if defined(CONFIG_CMD_ERASEENV)
406 .erase = env_mmc_erase,