xref: /openbmc/u-boot/env/mmc.c (revision 157c74b6)
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 	puts("done\n");
237 	ret = 0;
238 
239 #ifdef CONFIG_ENV_OFFSET_REDUND
240 	gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
241 #endif
242 
243 fini:
244 	fini_mmc_for_env(mmc);
245 	return ret;
246 }
247 #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
248 
249 static inline int read_env(struct mmc *mmc, unsigned long size,
250 			   unsigned long offset, const void *buffer)
251 {
252 	uint blk_start, blk_cnt, n;
253 	struct blk_desc *desc = mmc_get_blk_desc(mmc);
254 
255 	blk_start	= ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
256 	blk_cnt		= ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
257 
258 	n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
259 
260 	return (n == blk_cnt) ? 0 : -1;
261 }
262 
263 #ifdef CONFIG_ENV_OFFSET_REDUND
264 static int env_mmc_load(void)
265 {
266 #if !defined(ENV_IS_EMBEDDED)
267 	struct mmc *mmc;
268 	u32 offset1, offset2;
269 	int read1_fail = 0, read2_fail = 0;
270 	int ret;
271 	int dev = mmc_get_env_dev();
272 	const char *errmsg = NULL;
273 
274 	ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
275 	ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
276 
277 	mmc = find_mmc_device(dev);
278 
279 	errmsg = init_mmc_for_env(mmc);
280 	if (errmsg) {
281 		ret = -EIO;
282 		goto err;
283 	}
284 
285 	if (mmc_get_env_addr(mmc, 0, &offset1) ||
286 	    mmc_get_env_addr(mmc, 1, &offset2)) {
287 		ret = -EIO;
288 		goto fini;
289 	}
290 
291 	read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
292 	read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
293 
294 	if (read1_fail && read2_fail)
295 		puts("*** Error - No Valid Environment Area found\n");
296 	else if (read1_fail || read2_fail)
297 		puts("*** Warning - some problems detected "
298 		     "reading environment; recovered successfully\n");
299 
300 	if (read1_fail && read2_fail) {
301 		errmsg = "!bad CRC";
302 		ret = -EIO;
303 		goto fini;
304 	} else if (!read1_fail && read2_fail) {
305 		gd->env_valid = ENV_VALID;
306 		env_import((char *)tmp_env1, 1);
307 	} else if (read1_fail && !read2_fail) {
308 		gd->env_valid = ENV_REDUND;
309 		env_import((char *)tmp_env2, 1);
310 	} else {
311 		env_import_redund((char *)tmp_env1, (char *)tmp_env2);
312 	}
313 
314 	ret = 0;
315 
316 fini:
317 	fini_mmc_for_env(mmc);
318 err:
319 	if (ret)
320 		set_default_env(errmsg);
321 
322 #endif
323 	return ret;
324 }
325 #else /* ! CONFIG_ENV_OFFSET_REDUND */
326 static int env_mmc_load(void)
327 {
328 #if !defined(ENV_IS_EMBEDDED)
329 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
330 	struct mmc *mmc;
331 	u32 offset;
332 	int ret;
333 	int dev = mmc_get_env_dev();
334 	const char *errmsg;
335 
336 	mmc = find_mmc_device(dev);
337 
338 	errmsg = init_mmc_for_env(mmc);
339 	if (errmsg) {
340 		ret = -EIO;
341 		goto err;
342 	}
343 
344 	if (mmc_get_env_addr(mmc, 0, &offset)) {
345 		ret = -EIO;
346 		goto fini;
347 	}
348 
349 	if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
350 		errmsg = "!read failed";
351 		ret = -EIO;
352 		goto fini;
353 	}
354 
355 	env_import(buf, 1);
356 	ret = 0;
357 
358 fini:
359 	fini_mmc_for_env(mmc);
360 err:
361 	if (ret)
362 		set_default_env(errmsg);
363 #endif
364 	return ret;
365 }
366 #endif /* CONFIG_ENV_OFFSET_REDUND */
367 
368 U_BOOT_ENV_LOCATION(mmc) = {
369 	.location	= ENVL_MMC,
370 	ENV_NAME("MMC")
371 	.load		= env_mmc_load,
372 #ifndef CONFIG_SPL_BUILD
373 	.save		= env_save_ptr(env_mmc_save),
374 #endif
375 };
376