]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/env_mmc.c
2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
14 #include <linux/stddef.h>
21 #if defined(CONFIG_ENV_SIZE_REDUND) && \
22 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
23 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
26 char *env_name_spec
= "MMC";
28 #ifdef ENV_IS_EMBEDDED
29 env_t
*env_ptr
= &environment
;
30 #else /* ! ENV_IS_EMBEDDED */
32 #endif /* ENV_IS_EMBEDDED */
34 DECLARE_GLOBAL_DATA_PTR
;
36 #if !defined(CONFIG_ENV_OFFSET)
37 #define CONFIG_ENV_OFFSET 0
40 #if CONFIG_IS_ENABLED(OF_CONTROL)
41 static inline s64
mmc_offset(int copy
)
43 const char *propname
= "u-boot,mmc-env-offset";
44 s64 defvalue
= CONFIG_ENV_OFFSET
;
46 #if defined(CONFIG_ENV_OFFSET_REDUND)
48 propname
= "u-boot,mmc-env-offset-redundant";
49 defvalue
= CONFIG_ENV_OFFSET_REDUND
;
53 return fdtdec_get_config_int(gd
->fdt_blob
, propname
, defvalue
);
56 static inline s64
mmc_offset(int copy
)
58 s64 offset
= CONFIG_ENV_OFFSET
;
60 #if defined(CONFIG_ENV_OFFSET_REDUND)
62 offset
= CONFIG_ENV_OFFSET_REDUND
;
68 __weak
int mmc_get_env_addr(struct mmc
*mmc
, int copy
, u32
*env_addr
)
70 s64 offset
= mmc_offset(copy
);
73 offset
+= mmc
->capacity
;
80 __weak
int mmc_get_env_dev(void)
82 return CONFIG_SYS_MMC_ENV_DEV
;
88 gd
->env_addr
= (ulong
)&default_environment
[0];
94 #ifdef CONFIG_SYS_MMC_ENV_PART
95 __weak uint
mmc_get_env_part(struct mmc
*mmc
)
97 return CONFIG_SYS_MMC_ENV_PART
;
100 static unsigned char env_mmc_orig_hwpart
;
102 static int mmc_set_env_part(struct mmc
*mmc
)
104 uint part
= mmc_get_env_part(mmc
);
105 int dev
= mmc_get_env_dev();
108 env_mmc_orig_hwpart
= mmc_get_blk_desc(mmc
)->hwpart
;
109 ret
= blk_select_hwpart_devnum(IF_TYPE_MMC
, dev
, part
);
111 puts("MMC partition switch failed\n");
116 static inline int mmc_set_env_part(struct mmc
*mmc
) {return 0; };
119 static const char *init_mmc_for_env(struct mmc
*mmc
)
122 return "!No MMC card found";
127 if (blk_get_from_parent(mmc
->dev
, &dev
))
128 return "!No block device";
131 return "!MMC init failed";
133 if (mmc_set_env_part(mmc
))
134 return "!MMC partition switch failed";
139 static void fini_mmc_for_env(struct mmc
*mmc
)
141 #ifdef CONFIG_SYS_MMC_ENV_PART
142 int dev
= mmc_get_env_dev();
144 blk_select_hwpart_devnum(IF_TYPE_MMC
, dev
, env_mmc_orig_hwpart
);
148 #ifdef CONFIG_CMD_SAVEENV
149 static inline int write_env(struct mmc
*mmc
, unsigned long size
,
150 unsigned long offset
, const void *buffer
)
152 uint blk_start
, blk_cnt
, n
;
153 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
155 blk_start
= ALIGN(offset
, mmc
->write_bl_len
) / mmc
->write_bl_len
;
156 blk_cnt
= ALIGN(size
, mmc
->write_bl_len
) / mmc
->write_bl_len
;
158 n
= blk_dwrite(desc
, blk_start
, blk_cnt
, (u_char
*)buffer
);
160 return (n
== blk_cnt
) ? 0 : -1;
165 ALLOC_CACHE_ALIGN_BUFFER(env_t
, env_new
, 1);
166 int dev
= mmc_get_env_dev();
167 struct mmc
*mmc
= find_mmc_device(dev
);
172 errmsg
= init_mmc_for_env(mmc
);
174 printf("%s\n", errmsg
);
178 ret
= env_export(env_new
);
182 #ifdef CONFIG_ENV_OFFSET_REDUND
183 if (gd
->env_valid
== 1)
187 if (mmc_get_env_addr(mmc
, copy
, &offset
)) {
192 printf("Writing to %sMMC(%d)... ", copy
? "redundant " : "", dev
);
193 if (write_env(mmc
, CONFIG_ENV_SIZE
, offset
, (u_char
*)env_new
)) {
202 #ifdef CONFIG_ENV_OFFSET_REDUND
203 gd
->env_valid
= gd
->env_valid
== 2 ? 1 : 2;
207 fini_mmc_for_env(mmc
);
210 #endif /* CONFIG_CMD_SAVEENV */
212 static inline int read_env(struct mmc
*mmc
, unsigned long size
,
213 unsigned long offset
, const void *buffer
)
215 uint blk_start
, blk_cnt
, n
;
216 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
218 blk_start
= ALIGN(offset
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
219 blk_cnt
= ALIGN(size
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
221 n
= blk_dread(desc
, blk_start
, blk_cnt
, (uchar
*)buffer
);
223 return (n
== blk_cnt
) ? 0 : -1;
226 #ifdef CONFIG_ENV_OFFSET_REDUND
227 void env_relocate_spec(void)
229 #if !defined(ENV_IS_EMBEDDED)
231 u32 offset1
, offset2
;
232 int read1_fail
= 0, read2_fail
= 0;
234 int dev
= mmc_get_env_dev();
235 const char *errmsg
= NULL
;
237 ALLOC_CACHE_ALIGN_BUFFER(env_t
, tmp_env1
, 1);
238 ALLOC_CACHE_ALIGN_BUFFER(env_t
, tmp_env2
, 1);
240 mmc
= find_mmc_device(dev
);
242 errmsg
= init_mmc_for_env(mmc
);
248 if (mmc_get_env_addr(mmc
, 0, &offset1
) ||
249 mmc_get_env_addr(mmc
, 1, &offset2
)) {
254 read1_fail
= read_env(mmc
, CONFIG_ENV_SIZE
, offset1
, tmp_env1
);
255 read2_fail
= read_env(mmc
, CONFIG_ENV_SIZE
, offset2
, tmp_env2
);
257 if (read1_fail
&& read2_fail
)
258 puts("*** Error - No Valid Environment Area found\n");
259 else if (read1_fail
|| read2_fail
)
260 puts("*** Warning - some problems detected "
261 "reading environment; recovered successfully\n");
263 if (read1_fail
&& read2_fail
) {
267 } else if (!read1_fail
&& read2_fail
) {
269 env_import((char *)tmp_env1
, 1);
270 } else if (read1_fail
&& !read2_fail
) {
272 env_import((char *)tmp_env2
, 1);
274 env_import_redund((char *)tmp_env1
, (char *)tmp_env2
);
280 fini_mmc_for_env(mmc
);
283 set_default_env(errmsg
);
286 #else /* ! CONFIG_ENV_OFFSET_REDUND */
287 void env_relocate_spec(void)
289 #if !defined(ENV_IS_EMBEDDED)
290 ALLOC_CACHE_ALIGN_BUFFER(char, buf
, CONFIG_ENV_SIZE
);
294 int dev
= mmc_get_env_dev();
297 mmc
= find_mmc_device(dev
);
299 errmsg
= init_mmc_for_env(mmc
);
305 if (mmc_get_env_addr(mmc
, 0, &offset
)) {
310 if (read_env(mmc
, CONFIG_ENV_SIZE
, offset
, buf
)) {
311 errmsg
= "!read failed";
320 fini_mmc_for_env(mmc
);
323 set_default_env(errmsg
);
326 #endif /* CONFIG_ENV_OFFSET_REDUND */