]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - env/mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
11 #include <environment.h>
13 #include <linux/stddef.h>
22 #define STR(X) __STR(X)
24 #if defined(CONFIG_ENV_SIZE_REDUND) && \
25 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
26 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
29 DECLARE_GLOBAL_DATA_PTR
;
31 #if !defined(CONFIG_ENV_OFFSET)
32 #define CONFIG_ENV_OFFSET 0
35 #if CONFIG_IS_ENABLED(OF_CONTROL)
36 static inline int mmc_offset_try_partition(const char *str
, s64
*val
)
38 disk_partition_t info
;
39 struct blk_desc
*desc
;
42 ret
= blk_get_device_by_str("mmc", STR(CONFIG_SYS_MMC_ENV_DEV
), &desc
);
47 ret
= part_get_info(desc
, i
, &info
);
51 if (!strncmp((const char *)info
.name
, str
, sizeof(str
)))
55 /* round up to info.blksz */
56 len
= (CONFIG_ENV_SIZE
+ info
.blksz
- 1) & ~(info
.blksz
- 1);
58 /* use the top of the partion for the environment */
59 *val
= (info
.start
+ info
.size
- 1) - len
/ info
.blksz
;
64 static inline s64
mmc_offset(int copy
)
67 const char *offset_redund
;
68 const char *partition
;
71 .offset_redund
= "u-boot,mmc-env-offset-redundant",
72 .partition
= "u-boot,mmc-env-partition",
73 .offset
= "u-boot,mmc-env-offset",
75 s64 val
= 0, defvalue
;
80 /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
81 str
= fdtdec_get_config_string(gd
->fdt_blob
, dt_prop
.partition
);
83 /* try to place the environment at end of the partition */
84 err
= mmc_offset_try_partition(str
, &val
);
89 defvalue
= CONFIG_ENV_OFFSET
;
90 propname
= dt_prop
.offset
;
92 #if defined(CONFIG_ENV_OFFSET_REDUND)
94 defvalue
= CONFIG_ENV_OFFSET_REDUND
;
95 propname
= dt_prop
.offset_redund
;
98 return fdtdec_get_config_int(gd
->fdt_blob
, propname
, defvalue
);
101 static inline s64
mmc_offset(int copy
)
103 s64 offset
= CONFIG_ENV_OFFSET
;
105 #if defined(CONFIG_ENV_OFFSET_REDUND)
107 offset
= CONFIG_ENV_OFFSET_REDUND
;
113 __weak
int mmc_get_env_addr(struct mmc
*mmc
, int copy
, u32
*env_addr
)
115 s64 offset
= mmc_offset(copy
);
118 offset
+= mmc
->capacity
;
125 __weak
int mmc_get_env_dev(void)
127 return CONFIG_SYS_MMC_ENV_DEV
;
130 #ifdef CONFIG_SYS_MMC_ENV_PART
131 __weak uint
mmc_get_env_part(struct mmc
*mmc
)
133 return CONFIG_SYS_MMC_ENV_PART
;
136 static unsigned char env_mmc_orig_hwpart
;
138 static int mmc_set_env_part(struct mmc
*mmc
)
140 uint part
= mmc_get_env_part(mmc
);
141 int dev
= mmc_get_env_dev();
144 env_mmc_orig_hwpart
= mmc_get_blk_desc(mmc
)->hwpart
;
145 ret
= blk_select_hwpart_devnum(IF_TYPE_MMC
, dev
, part
);
147 puts("MMC partition switch failed\n");
152 static inline int mmc_set_env_part(struct mmc
*mmc
) {return 0; };
155 static const char *init_mmc_for_env(struct mmc
*mmc
)
158 return "No MMC card found";
160 #if CONFIG_IS_ENABLED(BLK)
163 if (blk_get_from_parent(mmc
->dev
, &dev
))
164 return "No block device";
167 return "MMC init failed";
169 if (mmc_set_env_part(mmc
))
170 return "MMC partition switch failed";
175 static void fini_mmc_for_env(struct mmc
*mmc
)
177 #ifdef CONFIG_SYS_MMC_ENV_PART
178 int dev
= mmc_get_env_dev();
180 blk_select_hwpart_devnum(IF_TYPE_MMC
, dev
, env_mmc_orig_hwpart
);
184 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
185 static inline int write_env(struct mmc
*mmc
, unsigned long size
,
186 unsigned long offset
, const void *buffer
)
188 uint blk_start
, blk_cnt
, n
;
189 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
191 blk_start
= ALIGN(offset
, mmc
->write_bl_len
) / mmc
->write_bl_len
;
192 blk_cnt
= ALIGN(size
, mmc
->write_bl_len
) / mmc
->write_bl_len
;
194 n
= blk_dwrite(desc
, blk_start
, blk_cnt
, (u_char
*)buffer
);
196 return (n
== blk_cnt
) ? 0 : -1;
199 static int env_mmc_save(void)
201 ALLOC_CACHE_ALIGN_BUFFER(env_t
, env_new
, 1);
202 int dev
= mmc_get_env_dev();
203 struct mmc
*mmc
= find_mmc_device(dev
);
208 errmsg
= init_mmc_for_env(mmc
);
210 printf("%s\n", errmsg
);
214 ret
= env_export(env_new
);
218 #ifdef CONFIG_ENV_OFFSET_REDUND
219 if (gd
->env_valid
== ENV_VALID
)
223 if (mmc_get_env_addr(mmc
, copy
, &offset
)) {
228 printf("Writing to %sMMC(%d)... ", copy
? "redundant " : "", dev
);
229 if (write_env(mmc
, CONFIG_ENV_SIZE
, offset
, (u_char
*)env_new
)) {
237 #ifdef CONFIG_ENV_OFFSET_REDUND
238 gd
->env_valid
= gd
->env_valid
== ENV_REDUND
? ENV_VALID
: ENV_REDUND
;
242 fini_mmc_for_env(mmc
);
245 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
247 static inline int read_env(struct mmc
*mmc
, unsigned long size
,
248 unsigned long offset
, const void *buffer
)
250 uint blk_start
, blk_cnt
, n
;
251 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
253 blk_start
= ALIGN(offset
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
254 blk_cnt
= ALIGN(size
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
256 n
= blk_dread(desc
, blk_start
, blk_cnt
, (uchar
*)buffer
);
258 return (n
== blk_cnt
) ? 0 : -1;
261 #ifdef CONFIG_ENV_OFFSET_REDUND
262 static int env_mmc_load(void)
264 #if !defined(ENV_IS_EMBEDDED)
266 u32 offset1
, offset2
;
267 int read1_fail
= 0, read2_fail
= 0;
269 int dev
= mmc_get_env_dev();
270 const char *errmsg
= NULL
;
272 ALLOC_CACHE_ALIGN_BUFFER(env_t
, tmp_env1
, 1);
273 ALLOC_CACHE_ALIGN_BUFFER(env_t
, tmp_env2
, 1);
275 mmc_initialize(NULL
);
277 mmc
= find_mmc_device(dev
);
279 errmsg
= init_mmc_for_env(mmc
);
285 if (mmc_get_env_addr(mmc
, 0, &offset1
) ||
286 mmc_get_env_addr(mmc
, 1, &offset2
)) {
291 read1_fail
= read_env(mmc
, CONFIG_ENV_SIZE
, offset1
, tmp_env1
);
292 read2_fail
= read_env(mmc
, CONFIG_ENV_SIZE
, offset2
, tmp_env2
);
294 ret
= env_import_redund((char *)tmp_env1
, read1_fail
, (char *)tmp_env2
,
298 fini_mmc_for_env(mmc
);
301 set_default_env(errmsg
, 0);
306 #else /* ! CONFIG_ENV_OFFSET_REDUND */
307 static int env_mmc_load(void)
309 #if !defined(ENV_IS_EMBEDDED)
310 ALLOC_CACHE_ALIGN_BUFFER(char, buf
, CONFIG_ENV_SIZE
);
314 int dev
= mmc_get_env_dev();
317 mmc
= find_mmc_device(dev
);
319 errmsg
= init_mmc_for_env(mmc
);
325 if (mmc_get_env_addr(mmc
, 0, &offset
)) {
330 if (read_env(mmc
, CONFIG_ENV_SIZE
, offset
, buf
)) {
331 errmsg
= "!read failed";
336 ret
= env_import(buf
, 1);
339 fini_mmc_for_env(mmc
);
342 set_default_env(errmsg
, 0);
346 #endif /* CONFIG_ENV_OFFSET_REDUND */
348 U_BOOT_ENV_LOCATION(mmc
) = {
349 .location
= ENVL_MMC
,
351 .load
= env_mmc_load
,
352 #ifndef CONFIG_SPL_BUILD
353 .save
= env_save_ptr(env_mmc_save
),