]>
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>
13 #include <linux/stddef.h>
19 #if defined(CONFIG_ENV_SIZE_REDUND) && \
20 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
21 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
24 char *env_name_spec
= "MMC";
26 #ifdef ENV_IS_EMBEDDED
27 env_t
*env_ptr
= &environment
;
28 #else /* ! ENV_IS_EMBEDDED */
30 #endif /* ENV_IS_EMBEDDED */
32 DECLARE_GLOBAL_DATA_PTR
;
34 #if !defined(CONFIG_ENV_OFFSET)
35 #define CONFIG_ENV_OFFSET 0
38 __weak
int mmc_get_env_addr(struct mmc
*mmc
, int copy
, u32
*env_addr
)
42 offset
= CONFIG_ENV_OFFSET
;
43 #ifdef CONFIG_ENV_OFFSET_REDUND
45 offset
= CONFIG_ENV_OFFSET_REDUND
;
49 offset
+= mmc
->capacity
;
59 gd
->env_addr
= (ulong
)&default_environment
[0];
65 static int init_mmc_for_env(struct mmc
*mmc
)
67 #ifdef CONFIG_SYS_MMC_ENV_PART
68 int dev
= CONFIG_SYS_MMC_ENV_DEV
;
70 #ifdef CONFIG_SPL_BUILD
76 puts("No MMC card found\n");
81 puts("MMC init failed\n");
85 #ifdef CONFIG_SYS_MMC_ENV_PART
86 if (CONFIG_SYS_MMC_ENV_PART
!= mmc
->part_num
) {
87 if (mmc_switch_part(dev
, CONFIG_SYS_MMC_ENV_PART
)) {
88 puts("MMC partition switch failed\n");
97 static void fini_mmc_for_env(struct mmc
*mmc
)
99 #ifdef CONFIG_SYS_MMC_ENV_PART
100 int dev
= CONFIG_SYS_MMC_ENV_DEV
;
102 #ifdef CONFIG_SPL_BUILD
105 if (CONFIG_SYS_MMC_ENV_PART
!= mmc
->part_num
)
106 mmc_switch_part(dev
, mmc
->part_num
);
110 #ifdef CONFIG_CMD_SAVEENV
111 static inline int write_env(struct mmc
*mmc
, unsigned long size
,
112 unsigned long offset
, const void *buffer
)
114 uint blk_start
, blk_cnt
, n
;
116 blk_start
= ALIGN(offset
, mmc
->write_bl_len
) / mmc
->write_bl_len
;
117 blk_cnt
= ALIGN(size
, mmc
->write_bl_len
) / mmc
->write_bl_len
;
119 n
= mmc
->block_dev
.block_write(CONFIG_SYS_MMC_ENV_DEV
, blk_start
,
120 blk_cnt
, (u_char
*)buffer
);
122 return (n
== blk_cnt
) ? 0 : -1;
125 #ifdef CONFIG_ENV_OFFSET_REDUND
126 static unsigned char env_flags
;
131 ALLOC_CACHE_ALIGN_BUFFER(env_t
, env_new
, 1);
132 struct mmc
*mmc
= find_mmc_device(CONFIG_SYS_MMC_ENV_DEV
);
136 if (init_mmc_for_env(mmc
))
139 ret
= env_export(env_new
);
143 #ifdef CONFIG_ENV_OFFSET_REDUND
144 env_new
->flags
= ++env_flags
; /* increase the serial */
146 if (gd
->env_valid
== 1)
150 if (mmc_get_env_addr(mmc
, copy
, &offset
)) {
155 printf("Writing to %sMMC(%d)... ", copy
? "redundant " : "",
156 CONFIG_SYS_MMC_ENV_DEV
);
157 if (write_env(mmc
, CONFIG_ENV_SIZE
, offset
, (u_char
*)env_new
)) {
166 #ifdef CONFIG_ENV_OFFSET_REDUND
167 gd
->env_valid
= gd
->env_valid
== 2 ? 1 : 2;
171 fini_mmc_for_env(mmc
);
174 #endif /* CONFIG_CMD_SAVEENV */
176 static inline int read_env(struct mmc
*mmc
, unsigned long size
,
177 unsigned long offset
, const void *buffer
)
179 uint blk_start
, blk_cnt
, n
;
180 int dev
= CONFIG_SYS_MMC_ENV_DEV
;
182 #ifdef CONFIG_SPL_BUILD
186 blk_start
= ALIGN(offset
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
187 blk_cnt
= ALIGN(size
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
189 n
= mmc
->block_dev
.block_read(dev
, blk_start
, blk_cnt
, (uchar
*)buffer
);
191 return (n
== blk_cnt
) ? 0 : -1;
194 #ifdef CONFIG_ENV_OFFSET_REDUND
195 void env_relocate_spec(void)
197 #if !defined(ENV_IS_EMBEDDED)
199 u32 offset1
, offset2
;
200 int read1_fail
= 0, read2_fail
= 0;
201 int crc1_ok
= 0, crc2_ok
= 0;
204 int dev
= CONFIG_SYS_MMC_ENV_DEV
;
206 ALLOC_CACHE_ALIGN_BUFFER(env_t
, tmp_env1
, 1);
207 ALLOC_CACHE_ALIGN_BUFFER(env_t
, tmp_env2
, 1);
209 #ifdef CONFIG_SPL_BUILD
213 mmc
= find_mmc_device(dev
);
215 if (init_mmc_for_env(mmc
)) {
220 if (mmc_get_env_addr(mmc
, 0, &offset1
) ||
221 mmc_get_env_addr(mmc
, 1, &offset2
)) {
226 read1_fail
= read_env(mmc
, CONFIG_ENV_SIZE
, offset1
, tmp_env1
);
227 read2_fail
= read_env(mmc
, CONFIG_ENV_SIZE
, offset2
, tmp_env2
);
229 if (read1_fail
&& read2_fail
)
230 puts("*** Error - No Valid Environment Area found\n");
231 else if (read1_fail
|| read2_fail
)
232 puts("*** Warning - some problems detected "
233 "reading environment; recovered successfully\n");
235 crc1_ok
= !read1_fail
&&
236 (crc32(0, tmp_env1
->data
, ENV_SIZE
) == tmp_env1
->crc
);
237 crc2_ok
= !read2_fail
&&
238 (crc32(0, tmp_env2
->data
, ENV_SIZE
) == tmp_env2
->crc
);
240 if (!crc1_ok
&& !crc2_ok
) {
243 } else if (crc1_ok
&& !crc2_ok
) {
245 } else if (!crc1_ok
&& crc2_ok
) {
248 /* both ok - check serial */
249 if (tmp_env1
->flags
== 255 && tmp_env2
->flags
== 0)
251 else if (tmp_env2
->flags
== 255 && tmp_env1
->flags
== 0)
253 else if (tmp_env1
->flags
> tmp_env2
->flags
)
255 else if (tmp_env2
->flags
> tmp_env1
->flags
)
257 else /* flags are equal - almost impossible */
263 if (gd
->env_valid
== 1)
268 env_flags
= ep
->flags
;
269 env_import((char *)ep
, 0);
273 fini_mmc_for_env(mmc
);
276 set_default_env(NULL
);
280 #else /* ! CONFIG_ENV_OFFSET_REDUND */
281 void env_relocate_spec(void)
283 #if !defined(ENV_IS_EMBEDDED)
284 ALLOC_CACHE_ALIGN_BUFFER(char, buf
, CONFIG_ENV_SIZE
);
288 int dev
= CONFIG_SYS_MMC_ENV_DEV
;
290 #ifdef CONFIG_SPL_BUILD
294 mmc
= find_mmc_device(dev
);
296 if (init_mmc_for_env(mmc
)) {
301 if (mmc_get_env_addr(mmc
, 0, &offset
)) {
306 if (read_env(mmc
, CONFIG_ENV_SIZE
, offset
, buf
)) {
315 fini_mmc_for_env(mmc
);
318 set_default_env(NULL
);
321 #endif /* CONFIG_ENV_OFFSET_REDUND */