1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
9 #include <asm/global_data.h>
13 #include <env_internal.h>
15 #include <linux/stddef.h>
22 #include <dm/ofnode.h>
25 #define STR(X) __STR(X)
27 DECLARE_GLOBAL_DATA_PTR;
30 * In case the environment is redundant, stored in eMMC hardware boot
31 * partition and the environment and redundant environment offsets are
32 * identical, store the environment and redundant environment in both
33 * eMMC boot partitions, one copy in each.
35 #if (defined(CONFIG_SYS_REDUNDAND_ENVIRONMENT) && \
36 (CONFIG_SYS_MMC_ENV_PART == 1) && \
37 (CONFIG_ENV_OFFSET == CONFIG_ENV_OFFSET_REDUND))
38 #define ENV_MMC_HWPART_REDUND
41 #if CONFIG_IS_ENABLED(OF_CONTROL)
42 static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val)
44 struct disk_partition info;
45 struct blk_desc *desc;
49 snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev());
50 ret = blk_get_device_by_str("mmc", dev_str, &desc);
55 ret = part_get_info(desc, i, &info);
59 if (!strncmp((const char *)info.name, str, sizeof(info.name)))
63 /* round up to info.blksz */
64 len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz);
66 /* use the top of the partion for the environment */
67 *val = (info.start + info.size - (1 + copy) * len) * info.blksz;
72 static inline s64 mmc_offset(int copy)
75 const char *offset_redund;
76 const char *partition;
79 .offset_redund = "u-boot,mmc-env-offset-redundant",
80 .partition = "u-boot,mmc-env-partition",
81 .offset = "u-boot,mmc-env-offset",
83 s64 val = 0, defvalue;
88 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
89 str = ofnode_conf_read_str(dt_prop.partition);
91 /* try to place the environment at end of the partition */
92 err = mmc_offset_try_partition(str, copy, &val);
97 defvalue = CONFIG_ENV_OFFSET;
98 propname = dt_prop.offset;
100 #if defined(CONFIG_ENV_OFFSET_REDUND)
102 defvalue = CONFIG_ENV_OFFSET_REDUND;
103 propname = dt_prop.offset_redund;
106 return ofnode_conf_read_int(propname, defvalue);
109 static inline s64 mmc_offset(int copy)
111 s64 offset = CONFIG_ENV_OFFSET;
113 #if defined(CONFIG_ENV_OFFSET_REDUND)
115 offset = CONFIG_ENV_OFFSET_REDUND;
121 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
123 s64 offset = mmc_offset(copy);
126 offset += mmc->capacity;
133 #ifdef CONFIG_SYS_MMC_ENV_PART
134 __weak uint mmc_get_env_part(struct mmc *mmc)
136 return CONFIG_SYS_MMC_ENV_PART;
139 static unsigned char env_mmc_orig_hwpart;
141 static int mmc_set_env_part(struct mmc *mmc, uint part)
143 int dev = mmc_get_env_dev();
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, uint part) {return 0; };
156 static const char *init_mmc_for_env(struct mmc *mmc)
159 return "No MMC card found";
161 #if CONFIG_IS_ENABLED(BLK)
164 if (blk_get_from_parent(mmc->dev, &dev))
165 return "No block device";
168 return "MMC init failed";
170 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
171 if (mmc_set_env_part(mmc, mmc_get_env_part(mmc)))
172 return "MMC partition switch failed";
177 static void fini_mmc_for_env(struct mmc *mmc)
179 #ifdef CONFIG_SYS_MMC_ENV_PART
180 int dev = mmc_get_env_dev();
182 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
186 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
187 static inline int write_env(struct mmc *mmc, unsigned long size,
188 unsigned long offset, const void *buffer)
190 uint blk_start, blk_cnt, n;
191 struct blk_desc *desc = mmc_get_blk_desc(mmc);
193 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
194 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
196 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
198 return (n == blk_cnt) ? 0 : -1;
201 static int env_mmc_save(void)
203 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
204 int dev = mmc_get_env_dev();
205 struct mmc *mmc = find_mmc_device(dev);
210 errmsg = init_mmc_for_env(mmc);
212 printf("%s\n", errmsg);
216 ret = env_export(env_new);
220 #ifdef CONFIG_ENV_OFFSET_REDUND
221 if (gd->env_valid == ENV_VALID)
224 #ifdef ENV_MMC_HWPART_REDUND
225 ret = mmc_set_env_part(mmc, copy + 1);
232 if (mmc_get_env_addr(mmc, copy, &offset)) {
237 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
238 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
246 #ifdef CONFIG_ENV_OFFSET_REDUND
247 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
251 fini_mmc_for_env(mmc);
255 static inline int erase_env(struct mmc *mmc, unsigned long size,
256 unsigned long offset)
258 uint blk_start, blk_cnt, n;
259 struct blk_desc *desc = mmc_get_blk_desc(mmc);
261 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
262 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
264 n = blk_derase(desc, blk_start, blk_cnt);
265 printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR");
267 return (n == blk_cnt) ? 0 : 1;
270 static int env_mmc_erase(void)
272 int dev = mmc_get_env_dev();
273 struct mmc *mmc = find_mmc_device(dev);
278 errmsg = init_mmc_for_env(mmc);
280 printf("%s\n", errmsg);
284 if (mmc_get_env_addr(mmc, copy, &offset)) {
285 ret = CMD_RET_FAILURE;
289 ret = erase_env(mmc, CONFIG_ENV_SIZE, offset);
291 #ifdef CONFIG_ENV_OFFSET_REDUND
294 #ifdef ENV_MMC_HWPART_REDUND
295 ret = mmc_set_env_part(mmc, copy + 1);
300 if (mmc_get_env_addr(mmc, copy, &offset)) {
301 ret = CMD_RET_FAILURE;
305 ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset);
309 fini_mmc_for_env(mmc);
312 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
314 static inline int read_env(struct mmc *mmc, unsigned long size,
315 unsigned long offset, const void *buffer)
317 uint blk_start, blk_cnt, n;
318 struct blk_desc *desc = mmc_get_blk_desc(mmc);
320 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
321 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
323 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
325 return (n == blk_cnt) ? 0 : -1;
328 #ifdef CONFIG_ENV_OFFSET_REDUND
329 static int env_mmc_load(void)
331 #if !defined(ENV_IS_EMBEDDED)
333 u32 offset1, offset2;
334 int read1_fail = 0, read2_fail = 0;
336 int dev = mmc_get_env_dev();
337 const char *errmsg = NULL;
339 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
340 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
342 mmc_initialize(NULL);
344 mmc = find_mmc_device(dev);
346 errmsg = init_mmc_for_env(mmc);
352 if (mmc_get_env_addr(mmc, 0, &offset1) ||
353 mmc_get_env_addr(mmc, 1, &offset2)) {
358 #ifdef ENV_MMC_HWPART_REDUND
359 ret = mmc_set_env_part(mmc, 1);
364 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
366 #ifdef ENV_MMC_HWPART_REDUND
367 ret = mmc_set_env_part(mmc, 2);
372 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
374 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
375 read2_fail, H_EXTERNAL);
378 fini_mmc_for_env(mmc);
381 env_set_default(errmsg, 0);
386 #else /* ! CONFIG_ENV_OFFSET_REDUND */
387 static int env_mmc_load(void)
389 #if !defined(ENV_IS_EMBEDDED)
390 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
394 int dev = mmc_get_env_dev();
398 mmc = find_mmc_device(dev);
400 errmsg = init_mmc_for_env(mmc);
406 if (mmc_get_env_addr(mmc, 0, &offset)) {
411 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
412 errmsg = "!read failed";
417 ret = env_import(buf, 1, H_EXTERNAL);
420 gd->env_addr = (ulong)&ep->data;
424 fini_mmc_for_env(mmc);
427 env_set_default(errmsg, 0);
431 #endif /* CONFIG_ENV_OFFSET_REDUND */
433 U_BOOT_ENV_LOCATION(mmc) = {
434 .location = ENVL_MMC,
436 .load = env_mmc_load,
437 #ifndef CONFIG_SPL_BUILD
438 .save = env_save_ptr(env_mmc_save),
439 .erase = ENV_ERASE_PTR(env_mmc_erase)