xref: /openbmc/u-boot/env/mmc.c (revision a35332fa)
1 /*
2  * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 /* #define DEBUG */
8 
9 #include <common.h>
10 
11 #include <command.h>
12 #include <environment.h>
13 #include <fdtdec.h>
14 #include <linux/stddef.h>
15 #include <malloc.h>
16 #include <memalign.h>
17 #include <mmc.h>
18 #include <part.h>
19 #include <search.h>
20 #include <errno.h>
21 
22 #define __STR(X) #X
23 #define STR(X) __STR(X)
24 
25 #if defined(CONFIG_ENV_SIZE_REDUND) &&  \
26 	(CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
27 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
28 #endif
29 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 #if !defined(CONFIG_ENV_OFFSET)
33 #define CONFIG_ENV_OFFSET 0
34 #endif
35 
36 #if CONFIG_IS_ENABLED(OF_CONTROL)
37 static inline int mmc_offset_try_partition(const char *str, s64 *val)
38 {
39 	disk_partition_t info;
40 	struct blk_desc *desc;
41 	int len, i, ret;
42 
43 	ret = blk_get_device_by_str("mmc", STR(CONFIG_SYS_MMC_ENV_DEV), &desc);
44 	if (ret < 0)
45 		return (ret);
46 
47 	for (i = 1;;i++) {
48 		ret = part_get_info(desc, i, &info);
49 		if (ret < 0)
50 			return ret;
51 
52 		if (!strncmp((const char *)info.name, str, sizeof(str)))
53 			break;
54 	}
55 
56 	/* round up to info.blksz */
57 	len = (CONFIG_ENV_SIZE + info.blksz - 1) & ~(info.blksz - 1);
58 
59 	/* use the top of the partion for the environment */
60 	*val = (info.start + info.size - 1) - len / info.blksz;
61 
62 	return 0;
63 }
64 
65 static inline s64 mmc_offset(int copy)
66 {
67 	const struct {
68 		const char *offset_redund;
69 		const char *partition;
70 		const char *offset;
71 	} dt_prop = {
72 		.offset_redund = "u-boot,mmc-env-offset-redundant",
73 		.partition = "u-boot,mmc-env-partition",
74 		.offset = "u-boot,mmc-env-offset",
75 	};
76 	s64 val = 0, defvalue;
77 	const char *propname;
78 	const char *str;
79 	int err;
80 
81 	/* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */
82 	str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition);
83 	if (str) {
84 		/* try to place the environment at end of the partition */
85 		err = mmc_offset_try_partition(str, &val);
86 		if (!err)
87 			return val;
88 	}
89 
90 	defvalue = CONFIG_ENV_OFFSET;
91 	propname = dt_prop.offset;
92 
93 #if defined(CONFIG_ENV_OFFSET_REDUND)
94 	if (copy) {
95 		defvalue = CONFIG_ENV_OFFSET_REDUND;
96 		propname = dt_prop.offset_redund;
97 	}
98 #endif
99 	return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
100 }
101 #else
102 static inline s64 mmc_offset(int copy)
103 {
104 	s64 offset = CONFIG_ENV_OFFSET;
105 
106 #if defined(CONFIG_ENV_OFFSET_REDUND)
107 	if (copy)
108 		offset = CONFIG_ENV_OFFSET_REDUND;
109 #endif
110 	return offset;
111 }
112 #endif
113 
114 __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
115 {
116 	s64 offset = mmc_offset(copy);
117 
118 	if (offset < 0)
119 		offset += mmc->capacity;
120 
121 	*env_addr = offset;
122 
123 	return 0;
124 }
125 
126 __weak int mmc_get_env_dev(void)
127 {
128 	return CONFIG_SYS_MMC_ENV_DEV;
129 }
130 
131 #ifdef CONFIG_SYS_MMC_ENV_PART
132 __weak uint mmc_get_env_part(struct mmc *mmc)
133 {
134 	return CONFIG_SYS_MMC_ENV_PART;
135 }
136 
137 static unsigned char env_mmc_orig_hwpart;
138 
139 static int mmc_set_env_part(struct mmc *mmc)
140 {
141 	uint part = mmc_get_env_part(mmc);
142 	int dev = mmc_get_env_dev();
143 	int ret = 0;
144 
145 	env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
146 	ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
147 	if (ret)
148 		puts("MMC partition switch failed\n");
149 
150 	return ret;
151 }
152 #else
153 static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
154 #endif
155 
156 static const char *init_mmc_for_env(struct mmc *mmc)
157 {
158 	if (!mmc)
159 		return "!No MMC card found";
160 
161 #ifdef CONFIG_BLK
162 	struct udevice *dev;
163 
164 	if (blk_get_from_parent(mmc->dev, &dev))
165 		return "!No block device";
166 #else
167 	if (mmc_init(mmc))
168 		return "!MMC init failed";
169 #endif
170 	if (mmc_set_env_part(mmc))
171 		return "!MMC partition switch failed";
172 
173 	return NULL;
174 }
175 
176 static void fini_mmc_for_env(struct mmc *mmc)
177 {
178 #ifdef CONFIG_SYS_MMC_ENV_PART
179 	int dev = mmc_get_env_dev();
180 
181 	blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
182 #endif
183 }
184 
185 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
186 static inline int write_env(struct mmc *mmc, unsigned long size,
187 			    unsigned long offset, const void *buffer)
188 {
189 	uint blk_start, blk_cnt, n;
190 	struct blk_desc *desc = mmc_get_blk_desc(mmc);
191 
192 	blk_start	= ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
193 	blk_cnt		= ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
194 
195 	n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
196 
197 	return (n == blk_cnt) ? 0 : -1;
198 }
199 
200 static int env_mmc_save(void)
201 {
202 	ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
203 	int dev = mmc_get_env_dev();
204 	struct mmc *mmc = find_mmc_device(dev);
205 	u32	offset;
206 	int	ret, copy = 0;
207 	const char *errmsg;
208 
209 	errmsg = init_mmc_for_env(mmc);
210 	if (errmsg) {
211 		printf("%s\n", errmsg);
212 		return 1;
213 	}
214 
215 	ret = env_export(env_new);
216 	if (ret)
217 		goto fini;
218 
219 #ifdef CONFIG_ENV_OFFSET_REDUND
220 	if (gd->env_valid == ENV_VALID)
221 		copy = 1;
222 #endif
223 
224 	if (mmc_get_env_addr(mmc, copy, &offset)) {
225 		ret = 1;
226 		goto fini;
227 	}
228 
229 	printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
230 	if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
231 		puts("failed\n");
232 		ret = 1;
233 		goto fini;
234 	}
235 
236 	ret = 0;
237 
238 #ifdef CONFIG_ENV_OFFSET_REDUND
239 	gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
240 #endif
241 
242 fini:
243 	fini_mmc_for_env(mmc);
244 	return ret;
245 }
246 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
247 
248 static inline int read_env(struct mmc *mmc, unsigned long size,
249 			   unsigned long offset, const void *buffer)
250 {
251 	uint blk_start, blk_cnt, n;
252 	struct blk_desc *desc = mmc_get_blk_desc(mmc);
253 
254 	blk_start	= ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
255 	blk_cnt		= ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
256 
257 	n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
258 
259 	return (n == blk_cnt) ? 0 : -1;
260 }
261 
262 #ifdef CONFIG_ENV_OFFSET_REDUND
263 static int env_mmc_load(void)
264 {
265 #if !defined(ENV_IS_EMBEDDED)
266 	struct mmc *mmc;
267 	u32 offset1, offset2;
268 	int read1_fail = 0, read2_fail = 0;
269 	int ret;
270 	int dev = mmc_get_env_dev();
271 	const char *errmsg = NULL;
272 
273 	ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
274 	ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
275 
276 	mmc = find_mmc_device(dev);
277 
278 	errmsg = init_mmc_for_env(mmc);
279 	if (errmsg) {
280 		ret = -EIO;
281 		goto err;
282 	}
283 
284 	if (mmc_get_env_addr(mmc, 0, &offset1) ||
285 	    mmc_get_env_addr(mmc, 1, &offset2)) {
286 		ret = -EIO;
287 		goto fini;
288 	}
289 
290 	read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
291 	read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
292 
293 	ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
294 				read2_fail);
295 
296 fini:
297 	fini_mmc_for_env(mmc);
298 err:
299 	if (ret)
300 		set_default_env(errmsg);
301 
302 #endif
303 	return ret;
304 }
305 #else /* ! CONFIG_ENV_OFFSET_REDUND */
306 static int env_mmc_load(void)
307 {
308 #if !defined(ENV_IS_EMBEDDED)
309 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
310 	struct mmc *mmc;
311 	u32 offset;
312 	int ret;
313 	int dev = mmc_get_env_dev();
314 	const char *errmsg;
315 
316 	mmc = find_mmc_device(dev);
317 
318 	errmsg = init_mmc_for_env(mmc);
319 	if (errmsg) {
320 		ret = -EIO;
321 		goto err;
322 	}
323 
324 	if (mmc_get_env_addr(mmc, 0, &offset)) {
325 		ret = -EIO;
326 		goto fini;
327 	}
328 
329 	if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
330 		errmsg = "!read failed";
331 		ret = -EIO;
332 		goto fini;
333 	}
334 
335 	ret = env_import(buf, 1);
336 
337 fini:
338 	fini_mmc_for_env(mmc);
339 err:
340 	if (ret)
341 		set_default_env(errmsg);
342 #endif
343 	return ret;
344 }
345 #endif /* CONFIG_ENV_OFFSET_REDUND */
346 
347 U_BOOT_ENV_LOCATION(mmc) = {
348 	.location	= ENVL_MMC,
349 	ENV_NAME("MMC")
350 	.load		= env_mmc_load,
351 #ifndef CONFIG_SPL_BUILD
352 	.save		= env_save_ptr(env_mmc_save),
353 #endif
354 };
355