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, int copy, s64 *val)
30 struct disk_partition info;
31 struct blk_desc *desc;
35 snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev());
36 ret = blk_get_device_by_str("mmc", dev_str, &desc);
41 ret = part_get_info(desc, i, &info);
45 if (!strncmp((const char *)info.name, str, sizeof(info.name)))
49 /* round up to info.blksz */
50 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
52 /* use the top of the partion for the environment */
53 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
58 static inline s64 mmc_offset(int copy)
61 const char *offset_redund;
62 const char *partition;
65 .offset_redund = "u-boot,mmc-env-offset-redundant",
66 .partition = "u-boot,mmc-env-partition",
67 .offset = "u-boot,mmc-env-offset",
69 s64 val = 0, defvalue;
74 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
75 str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition);
77 /* try to place the environment at end of the partition */
78 err = mmc_offset_try_partition(str, copy, &val);
83 defvalue = CONFIG_ENV_OFFSET;
84 propname = dt_prop.offset;
86 #if defined(CONFIG_ENV_OFFSET_REDUND)
88 defvalue = CONFIG_ENV_OFFSET_REDUND;
89 propname = dt_prop.offset_redund;
92 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
95 static inline s64 mmc_offset(int copy)
97 s64 offset = CONFIG_ENV_OFFSET;
99 #if defined(CONFIG_ENV_OFFSET_REDUND)
101 offset = CONFIG_ENV_OFFSET_REDUND;
107 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
109 s64 offset = mmc_offset(copy);
112 offset += mmc->capacity;
119 #ifdef CONFIG_SYS_MMC_ENV_PART
120 __weak uint mmc_get_env_part(struct mmc *mmc)
122 return CONFIG_SYS_MMC_ENV_PART;
125 static unsigned char env_mmc_orig_hwpart;
127 static int mmc_set_env_part(struct mmc *mmc)
129 uint part = mmc_get_env_part(mmc);
130 int dev = mmc_get_env_dev();
133 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
134 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
136 puts("MMC partition switch failed\n");
141 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
144 static const char *init_mmc_for_env(struct mmc *mmc)
147 return "No MMC card found";
149 #if CONFIG_IS_ENABLED(BLK)
152 if (blk_get_from_parent(mmc->dev, &dev))
153 return "No block device";
156 return "MMC init failed";
158 if (mmc_set_env_part(mmc))
159 return "MMC partition switch failed";
164 static void fini_mmc_for_env(struct mmc *mmc)
166 #ifdef CONFIG_SYS_MMC_ENV_PART
167 int dev = mmc_get_env_dev();
169 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
173 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
174 static inline int write_env(struct mmc *mmc, unsigned long size,
175 unsigned long offset, const void *buffer)
177 uint blk_start, blk_cnt, n;
178 struct blk_desc *desc = mmc_get_blk_desc(mmc);
180 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
181 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
183 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
185 return (n == blk_cnt) ? 0 : -1;
188 static int env_mmc_save(void)
190 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
191 int dev = mmc_get_env_dev();
192 struct mmc *mmc = find_mmc_device(dev);
197 errmsg = init_mmc_for_env(mmc);
199 printf("%s\n", errmsg);
203 ret = env_export(env_new);
207 #ifdef CONFIG_ENV_OFFSET_REDUND
208 if (gd->env_valid == ENV_VALID)
212 if (mmc_get_env_addr(mmc, copy, &offset)) {
217 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
218 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
226 #ifdef CONFIG_ENV_OFFSET_REDUND
227 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
231 fini_mmc_for_env(mmc);
235 #if defined(CONFIG_CMD_ERASEENV)
236 static inline int erase_env(struct mmc *mmc, unsigned long size,
237 unsigned long offset)
239 uint blk_start, blk_cnt, n;
240 struct blk_desc *desc = mmc_get_blk_desc(mmc);
242 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
243 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
245 n = blk_derase(desc, blk_start, blk_cnt);
246 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
248 return (n == blk_cnt) ? 0 : 1;
251 static int env_mmc_erase(void)
253 int dev = mmc_get_env_dev();
254 struct mmc *mmc = find_mmc_device(dev);
259 errmsg = init_mmc_for_env(mmc);
261 printf("%s\n", errmsg);
265 if (mmc_get_env_addr(mmc, copy, &offset))
266 return CMD_RET_FAILURE;
268 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
270 #ifdef CONFIG_ENV_OFFSET_REDUND
273 if (mmc_get_env_addr(mmc, copy, &offset))
274 return CMD_RET_FAILURE;
276 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
281 #endif /* CONFIG_CMD_ERASEENV */
282 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
284 static inline int read_env(struct mmc *mmc, unsigned long size,
285 unsigned long offset, const void *buffer)
287 uint blk_start, blk_cnt, n;
288 struct blk_desc *desc = mmc_get_blk_desc(mmc);
290 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
291 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
293 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
295 return (n == blk_cnt) ? 0 : -1;
298 #ifdef CONFIG_ENV_OFFSET_REDUND
299 static int env_mmc_load(void)
301 #if !defined(ENV_IS_EMBEDDED)
303 u32 offset1, offset2;
304 int read1_fail = 0, read2_fail = 0;
306 int dev = mmc_get_env_dev();
307 const char *errmsg = NULL;
309 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
310 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
312 mmc_initialize(NULL);
314 mmc = find_mmc_device(dev);
316 errmsg = init_mmc_for_env(mmc);
322 if (mmc_get_env_addr(mmc, 0, &offset1) ||
323 mmc_get_env_addr(mmc, 1, &offset2)) {
328 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
329 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
331 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
332 read2_fail, H_EXTERNAL);
335 fini_mmc_for_env(mmc);
338 env_set_default(errmsg, 0);
343 #else /* ! CONFIG_ENV_OFFSET_REDUND */
344 static int env_mmc_load(void)
346 #if !defined(ENV_IS_EMBEDDED)
347 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
351 int dev = mmc_get_env_dev();
355 mmc = find_mmc_device(dev);
357 errmsg = init_mmc_for_env(mmc);
363 if (mmc_get_env_addr(mmc, 0, &offset)) {
368 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
369 errmsg = "!read failed";
374 ret = env_import(buf, 1, H_EXTERNAL);
377 gd->env_addr = (ulong)&ep->data;
381 fini_mmc_for_env(mmc);
384 env_set_default(errmsg, 0);
388 #endif /* CONFIG_ENV_OFFSET_REDUND */
390 U_BOOT_ENV_LOCATION(mmc) = {
391 .location = ENVL_MMC,
393 .load = env_mmc_load,
394 #ifndef CONFIG_SPL_BUILD
395 .save = env_save_ptr(env_mmc_save),
396 #if defined(CONFIG_CMD_ERASEENV)
397 .erase = env_mmc_erase,