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_ENV_OFFSET)
28 #define CONFIG_ENV_OFFSET 0
31 #if CONFIG_IS_ENABLED(OF_CONTROL)
32 static inline int mmc_offset_try_partition(const char *str, s64 *val)
34 disk_partition_t info;
35 struct blk_desc *desc;
38 ret = blk_get_device_by_str("mmc", STR(CONFIG_SYS_MMC_ENV_DEV), &desc);
43 ret = part_get_info(desc, i, &info);
47 if (!strncmp((const char *)info.name, str, sizeof(str)))
51 /* round up to info.blksz */
52 len = (CONFIG_ENV_SIZE + info.blksz - 1) & ~(info.blksz - 1);
54 /* use the top of the partion for the environment */
55 *val = (info.start + info.size - 1) - len / info.blksz;
60 static inline s64 mmc_offset(int copy)
63 const char *offset_redund;
64 const char *partition;
67 .offset_redund = "u-boot,mmc-env-offset-redundant",
68 .partition = "u-boot,mmc-env-partition",
69 .offset = "u-boot,mmc-env-offset",
71 s64 val = 0, defvalue;
76 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
77 str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition);
79 /* try to place the environment at end of the partition */
80 err = mmc_offset_try_partition(str, &val);
85 defvalue = CONFIG_ENV_OFFSET;
86 propname = dt_prop.offset;
88 #if defined(CONFIG_ENV_OFFSET_REDUND)
90 defvalue = CONFIG_ENV_OFFSET_REDUND;
91 propname = dt_prop.offset_redund;
94 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
97 static inline s64 mmc_offset(int copy)
99 s64 offset = CONFIG_ENV_OFFSET;
101 #if defined(CONFIG_ENV_OFFSET_REDUND)
103 offset = CONFIG_ENV_OFFSET_REDUND;
109 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
111 s64 offset = mmc_offset(copy);
114 offset += mmc->capacity;
121 __weak int mmc_get_env_dev(void)
123 return CONFIG_SYS_MMC_ENV_DEV;
126 #ifdef CONFIG_SYS_MMC_ENV_PART
127 __weak uint mmc_get_env_part(struct mmc *mmc)
129 return CONFIG_SYS_MMC_ENV_PART;
132 static unsigned char env_mmc_orig_hwpart;
134 static int mmc_set_env_part(struct mmc *mmc)
136 uint part = mmc_get_env_part(mmc);
137 int dev = mmc_get_env_dev();
140 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
141 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
143 puts("MMC partition switch failed\n");
148 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
151 static const char *init_mmc_for_env(struct mmc *mmc)
154 return "No MMC card found";
156 #if CONFIG_IS_ENABLED(BLK)
159 if (blk_get_from_parent(mmc->dev, &dev))
160 return "No block device";
163 return "MMC init failed";
165 if (mmc_set_env_part(mmc))
166 return "MMC partition switch failed";
171 static void fini_mmc_for_env(struct mmc *mmc)
173 #ifdef CONFIG_SYS_MMC_ENV_PART
174 int dev = mmc_get_env_dev();
176 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
180 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
181 static inline int write_env(struct mmc *mmc, unsigned long size,
182 unsigned long offset, const void *buffer)
184 uint blk_start, blk_cnt, n;
185 struct blk_desc *desc = mmc_get_blk_desc(mmc);
187 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
188 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
190 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
192 return (n == blk_cnt) ? 0 : -1;
195 static int env_mmc_save(void)
197 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
198 int dev = mmc_get_env_dev();
199 struct mmc *mmc = find_mmc_device(dev);
204 errmsg = init_mmc_for_env(mmc);
206 printf("%s\n", errmsg);
210 ret = env_export(env_new);
214 #ifdef CONFIG_ENV_OFFSET_REDUND
215 if (gd->env_valid == ENV_VALID)
219 if (mmc_get_env_addr(mmc, copy, &offset)) {
224 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
225 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
233 #ifdef CONFIG_ENV_OFFSET_REDUND
234 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
238 fini_mmc_for_env(mmc);
242 #if defined(CONFIG_CMD_ERASEENV)
243 static inline int erase_env(struct mmc *mmc, unsigned long size,
244 unsigned long offset)
246 uint blk_start, blk_cnt, n;
247 struct blk_desc *desc = mmc_get_blk_desc(mmc);
249 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
250 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
252 n = blk_derase(desc, blk_start, blk_cnt);
253 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
255 return (n == blk_cnt) ? 0 : 1;
258 static int env_mmc_erase(void)
260 int dev = mmc_get_env_dev();
261 struct mmc *mmc = find_mmc_device(dev);
266 errmsg = init_mmc_for_env(mmc);
268 printf("%s\n", errmsg);
272 if (mmc_get_env_addr(mmc, copy, &offset))
273 return CMD_RET_FAILURE;
275 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
277 #ifdef CONFIG_ENV_OFFSET_REDUND
280 if (mmc_get_env_addr(mmc, copy, &offset))
281 return CMD_RET_FAILURE;
283 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
288 #endif /* CONFIG_CMD_ERASEENV */
289 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
291 static inline int read_env(struct mmc *mmc, unsigned long size,
292 unsigned long offset, const void *buffer)
294 uint blk_start, blk_cnt, n;
295 struct blk_desc *desc = mmc_get_blk_desc(mmc);
297 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
298 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
300 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
302 return (n == blk_cnt) ? 0 : -1;
305 #ifdef CONFIG_ENV_OFFSET_REDUND
306 static int env_mmc_load(void)
308 #if !defined(ENV_IS_EMBEDDED)
310 u32 offset1, offset2;
311 int read1_fail = 0, read2_fail = 0;
313 int dev = mmc_get_env_dev();
314 const char *errmsg = NULL;
316 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
317 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
319 mmc_initialize(NULL);
321 mmc = find_mmc_device(dev);
323 errmsg = init_mmc_for_env(mmc);
329 if (mmc_get_env_addr(mmc, 0, &offset1) ||
330 mmc_get_env_addr(mmc, 1, &offset2)) {
335 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
336 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
338 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
342 fini_mmc_for_env(mmc);
345 env_set_default(errmsg, 0);
350 #else /* ! CONFIG_ENV_OFFSET_REDUND */
351 static int env_mmc_load(void)
353 #if !defined(ENV_IS_EMBEDDED)
354 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
358 int dev = mmc_get_env_dev();
361 mmc = find_mmc_device(dev);
363 errmsg = init_mmc_for_env(mmc);
369 if (mmc_get_env_addr(mmc, 0, &offset)) {
374 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
375 errmsg = "!read failed";
380 ret = env_import(buf, 1);
383 fini_mmc_for_env(mmc);
386 env_set_default(errmsg, 0);
390 #endif /* CONFIG_ENV_OFFSET_REDUND */
392 U_BOOT_ENV_LOCATION(mmc) = {
393 .location = ENVL_MMC,
395 .load = env_mmc_load,
396 #ifndef CONFIG_SPL_BUILD
397 .save = env_save_ptr(env_mmc_save),
398 #if defined(CONFIG_CMD_ERASEENV)
399 .erase = env_mmc_erase,