]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
a8060359 | 2 | /* |
97039ab9 | 3 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
a8060359 TL |
4 | */ |
5 | ||
6 | /* #define DEBUG */ | |
7 | ||
8 | #include <common.h> | |
9 | ||
10 | #include <command.h> | |
0ac7d722 | 11 | #include <env.h> |
f3998fdc | 12 | #include <env_internal.h> |
f8b8a554 | 13 | #include <fdtdec.h> |
a8060359 TL |
14 | #include <linux/stddef.h> |
15 | #include <malloc.h> | |
cf92e05c | 16 | #include <memalign.h> |
a8060359 | 17 | #include <mmc.h> |
c9e87ba6 | 18 | #include <part.h> |
6d1d51b3 | 19 | #include <search.h> |
e79f4839 | 20 | #include <errno.h> |
a8060359 | 21 | |
c9e87ba6 JRO |
22 | #define __STR(X) #X |
23 | #define STR(X) __STR(X) | |
24 | ||
a8060359 TL |
25 | DECLARE_GLOBAL_DATA_PTR; |
26 | ||
2b2f7275 PD |
27 | __weak int mmc_get_env_dev(void) |
28 | { | |
29 | return CONFIG_SYS_MMC_ENV_DEV; | |
30 | } | |
31 | ||
f8b8a554 | 32 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
5d4f7b4e | 33 | static inline int mmc_offset_try_partition(const char *str, int copy, s64 *val) |
c9e87ba6 | 34 | { |
0528979f | 35 | struct disk_partition info; |
c9e87ba6 JRO |
36 | struct blk_desc *desc; |
37 | int len, i, ret; | |
2b2f7275 | 38 | char dev_str[4]; |
c9e87ba6 | 39 | |
2b2f7275 PD |
40 | snprintf(dev_str, sizeof(dev_str), "%d", mmc_get_env_dev()); |
41 | ret = blk_get_device_by_str("mmc", dev_str, &desc); | |
c9e87ba6 JRO |
42 | if (ret < 0) |
43 | return (ret); | |
44 | ||
45 | for (i = 1;;i++) { | |
46 | ret = part_get_info(desc, i, &info); | |
47 | if (ret < 0) | |
48 | return ret; | |
49 | ||
50 | if (!strncmp((const char *)info.name, str, sizeof(str))) | |
51 | break; | |
52 | } | |
53 | ||
54 | /* round up to info.blksz */ | |
76b640c3 | 55 | len = DIV_ROUND_UP(CONFIG_ENV_SIZE, info.blksz); |
c9e87ba6 JRO |
56 | |
57 | /* use the top of the partion for the environment */ | |
5d4f7b4e | 58 | *val = (info.start + info.size - (1 + copy) * len) * info.blksz; |
c9e87ba6 JRO |
59 | |
60 | return 0; | |
61 | } | |
62 | ||
f8b8a554 PT |
63 | static inline s64 mmc_offset(int copy) |
64 | { | |
c9e87ba6 JRO |
65 | const struct { |
66 | const char *offset_redund; | |
67 | const char *partition; | |
68 | const char *offset; | |
69 | } dt_prop = { | |
70 | .offset_redund = "u-boot,mmc-env-offset-redundant", | |
71 | .partition = "u-boot,mmc-env-partition", | |
72 | .offset = "u-boot,mmc-env-offset", | |
73 | }; | |
fd374665 | 74 | s64 val = 0, defvalue; |
c9e87ba6 JRO |
75 | const char *propname; |
76 | const char *str; | |
77 | int err; | |
78 | ||
79 | /* look for the partition in mmc CONFIG_SYS_MMC_ENV_DEV */ | |
80 | str = fdtdec_get_config_string(gd->fdt_blob, dt_prop.partition); | |
81 | if (str) { | |
82 | /* try to place the environment at end of the partition */ | |
5d4f7b4e | 83 | err = mmc_offset_try_partition(str, copy, &val); |
c9e87ba6 JRO |
84 | if (!err) |
85 | return val; | |
86 | } | |
87 | ||
88 | defvalue = CONFIG_ENV_OFFSET; | |
89 | propname = dt_prop.offset; | |
f8b8a554 PT |
90 | |
91 | #if defined(CONFIG_ENV_OFFSET_REDUND) | |
92 | if (copy) { | |
f8b8a554 | 93 | defvalue = CONFIG_ENV_OFFSET_REDUND; |
c9e87ba6 | 94 | propname = dt_prop.offset_redund; |
f8b8a554 PT |
95 | } |
96 | #endif | |
f8b8a554 PT |
97 | return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue); |
98 | } | |
99 | #else | |
100 | static inline s64 mmc_offset(int copy) | |
97039ab9 | 101 | { |
f8b8a554 | 102 | s64 offset = CONFIG_ENV_OFFSET; |
5c088ee8 | 103 | |
f8b8a554 | 104 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
d196bd88 | 105 | if (copy) |
5c088ee8 | 106 | offset = CONFIG_ENV_OFFSET_REDUND; |
d196bd88 | 107 | #endif |
f8b8a554 PT |
108 | return offset; |
109 | } | |
110 | #endif | |
111 | ||
112 | __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) | |
113 | { | |
114 | s64 offset = mmc_offset(copy); | |
5c088ee8 SW |
115 | |
116 | if (offset < 0) | |
117 | offset += mmc->capacity; | |
118 | ||
119 | *env_addr = offset; | |
120 | ||
97039ab9 MH |
121 | return 0; |
122 | } | |
97039ab9 | 123 | |
b9c8ccab | 124 | #ifdef CONFIG_SYS_MMC_ENV_PART |
6e7b7df4 DL |
125 | __weak uint mmc_get_env_part(struct mmc *mmc) |
126 | { | |
127 | return CONFIG_SYS_MMC_ENV_PART; | |
128 | } | |
129 | ||
873cc1d7 SW |
130 | static unsigned char env_mmc_orig_hwpart; |
131 | ||
6e7b7df4 DL |
132 | static int mmc_set_env_part(struct mmc *mmc) |
133 | { | |
134 | uint part = mmc_get_env_part(mmc); | |
e92029c0 | 135 | int dev = mmc_get_env_dev(); |
6e7b7df4 | 136 | int ret = 0; |
b9c8ccab | 137 | |
69f45cd5 SG |
138 | env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart; |
139 | ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part); | |
873cc1d7 SW |
140 | if (ret) |
141 | puts("MMC partition switch failed\n"); | |
6e7b7df4 DL |
142 | |
143 | return ret; | |
144 | } | |
145 | #else | |
146 | static inline int mmc_set_env_part(struct mmc *mmc) {return 0; }; | |
b9c8ccab TR |
147 | #endif |
148 | ||
c75648d7 | 149 | static const char *init_mmc_for_env(struct mmc *mmc) |
6e7b7df4 | 150 | { |
c75648d7 | 151 | if (!mmc) |
c5d548a9 | 152 | return "No MMC card found"; |
a8060359 | 153 | |
d48b8d11 | 154 | #if CONFIG_IS_ENABLED(BLK) |
01b73fe6 SG |
155 | struct udevice *dev; |
156 | ||
157 | if (blk_get_from_parent(mmc->dev, &dev)) | |
c5d548a9 | 158 | return "No block device"; |
01b73fe6 | 159 | #else |
c75648d7 | 160 | if (mmc_init(mmc)) |
c5d548a9 | 161 | return "MMC init failed"; |
e7017a3c | 162 | #endif |
c75648d7 | 163 | if (mmc_set_env_part(mmc)) |
c5d548a9 | 164 | return "MMC partition switch failed"; |
a8060359 | 165 | |
c75648d7 | 166 | return NULL; |
a8060359 TL |
167 | } |
168 | ||
9404a5fc SW |
169 | static void fini_mmc_for_env(struct mmc *mmc) |
170 | { | |
171 | #ifdef CONFIG_SYS_MMC_ENV_PART | |
e92029c0 | 172 | int dev = mmc_get_env_dev(); |
b9c8ccab | 173 | |
69f45cd5 | 174 | blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart); |
9404a5fc SW |
175 | #endif |
176 | } | |
177 | ||
e5bce247 | 178 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD) |
e8db8f71 IG |
179 | static inline int write_env(struct mmc *mmc, unsigned long size, |
180 | unsigned long offset, const void *buffer) | |
a8060359 TL |
181 | { |
182 | uint blk_start, blk_cnt, n; | |
5461acba | 183 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
a8060359 | 184 | |
e8db8f71 IG |
185 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
186 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; | |
a8060359 | 187 | |
5461acba | 188 | n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer); |
a8060359 TL |
189 | |
190 | return (n == blk_cnt) ? 0 : -1; | |
191 | } | |
192 | ||
e5bce247 | 193 | static int env_mmc_save(void) |
a8060359 | 194 | { |
cd0f4fa1 | 195 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
e92029c0 CG |
196 | int dev = mmc_get_env_dev(); |
197 | struct mmc *mmc = find_mmc_device(dev); | |
e8db8f71 | 198 | u32 offset; |
d196bd88 | 199 | int ret, copy = 0; |
c75648d7 | 200 | const char *errmsg; |
a8060359 | 201 | |
c75648d7 TH |
202 | errmsg = init_mmc_for_env(mmc); |
203 | if (errmsg) { | |
204 | printf("%s\n", errmsg); | |
97039ab9 | 205 | return 1; |
c75648d7 | 206 | } |
97039ab9 | 207 | |
7ce1526e MV |
208 | ret = env_export(env_new); |
209 | if (ret) | |
9404a5fc | 210 | goto fini; |
d196bd88 MH |
211 | |
212 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
203e94f6 | 213 | if (gd->env_valid == ENV_VALID) |
d196bd88 MH |
214 | copy = 1; |
215 | #endif | |
216 | ||
217 | if (mmc_get_env_addr(mmc, copy, &offset)) { | |
218 | ret = 1; | |
219 | goto fini; | |
220 | } | |
221 | ||
e92029c0 | 222 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev); |
4036b630 | 223 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
a8060359 | 224 | puts("failed\n"); |
9404a5fc SW |
225 | ret = 1; |
226 | goto fini; | |
a8060359 TL |
227 | } |
228 | ||
9404a5fc SW |
229 | ret = 0; |
230 | ||
d196bd88 | 231 | #ifdef CONFIG_ENV_OFFSET_REDUND |
203e94f6 | 232 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
d196bd88 MH |
233 | #endif |
234 | ||
9404a5fc SW |
235 | fini: |
236 | fini_mmc_for_env(mmc); | |
237 | return ret; | |
a8060359 | 238 | } |
34853925 FW |
239 | |
240 | #if defined(CONFIG_CMD_ERASEENV) | |
241 | static inline int erase_env(struct mmc *mmc, unsigned long size, | |
242 | unsigned long offset) | |
243 | { | |
244 | uint blk_start, blk_cnt, n; | |
245 | struct blk_desc *desc = mmc_get_blk_desc(mmc); | |
246 | ||
247 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; | |
248 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; | |
249 | ||
250 | n = blk_derase(desc, blk_start, blk_cnt); | |
251 | printf("%d blocks erased: %s\n", n, (n == blk_cnt) ? "OK" : "ERROR"); | |
252 | ||
253 | return (n == blk_cnt) ? 0 : 1; | |
254 | } | |
255 | ||
256 | static int env_mmc_erase(void) | |
257 | { | |
258 | int dev = mmc_get_env_dev(); | |
259 | struct mmc *mmc = find_mmc_device(dev); | |
260 | int ret, copy = 0; | |
261 | u32 offset; | |
262 | const char *errmsg; | |
263 | ||
264 | errmsg = init_mmc_for_env(mmc); | |
265 | if (errmsg) { | |
266 | printf("%s\n", errmsg); | |
267 | return 1; | |
268 | } | |
269 | ||
270 | if (mmc_get_env_addr(mmc, copy, &offset)) | |
271 | return CMD_RET_FAILURE; | |
272 | ||
273 | ret = erase_env(mmc, CONFIG_ENV_SIZE, offset); | |
274 | ||
275 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
276 | copy = 1; | |
277 | ||
278 | if (mmc_get_env_addr(mmc, copy, &offset)) | |
279 | return CMD_RET_FAILURE; | |
280 | ||
281 | ret |= erase_env(mmc, CONFIG_ENV_SIZE, offset); | |
282 | #endif | |
283 | ||
284 | return ret; | |
285 | } | |
286 | #endif /* CONFIG_CMD_ERASEENV */ | |
e5bce247 | 287 | #endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */ |
a8060359 | 288 | |
e8db8f71 IG |
289 | static inline int read_env(struct mmc *mmc, unsigned long size, |
290 | unsigned long offset, const void *buffer) | |
a8060359 TL |
291 | { |
292 | uint blk_start, blk_cnt, n; | |
5461acba | 293 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
a8060359 | 294 | |
e8db8f71 IG |
295 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
296 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; | |
a8060359 | 297 | |
5461acba | 298 | n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer); |
a8060359 TL |
299 | |
300 | return (n == blk_cnt) ? 0 : -1; | |
301 | } | |
302 | ||
d196bd88 | 303 | #ifdef CONFIG_ENV_OFFSET_REDUND |
c5951991 | 304 | static int env_mmc_load(void) |
d196bd88 MH |
305 | { |
306 | #if !defined(ENV_IS_EMBEDDED) | |
b9c8ccab | 307 | struct mmc *mmc; |
d196bd88 MH |
308 | u32 offset1, offset2; |
309 | int read1_fail = 0, read2_fail = 0; | |
d196bd88 | 310 | int ret; |
e92029c0 | 311 | int dev = mmc_get_env_dev(); |
c75648d7 | 312 | const char *errmsg = NULL; |
d196bd88 | 313 | |
452a2722 MN |
314 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1); |
315 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1); | |
316 | ||
26862b4a FA |
317 | mmc_initialize(NULL); |
318 | ||
b9c8ccab TR |
319 | mmc = find_mmc_device(dev); |
320 | ||
c75648d7 TH |
321 | errmsg = init_mmc_for_env(mmc); |
322 | if (errmsg) { | |
c5951991 | 323 | ret = -EIO; |
d196bd88 MH |
324 | goto err; |
325 | } | |
326 | ||
327 | if (mmc_get_env_addr(mmc, 0, &offset1) || | |
328 | mmc_get_env_addr(mmc, 1, &offset2)) { | |
c5951991 | 329 | ret = -EIO; |
d196bd88 MH |
330 | goto fini; |
331 | } | |
332 | ||
333 | read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1); | |
334 | read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); | |
335 | ||
31f044bd | 336 | ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
890feeca | 337 | read2_fail, H_EXTERNAL); |
d196bd88 MH |
338 | |
339 | fini: | |
340 | fini_mmc_for_env(mmc); | |
341 | err: | |
342 | if (ret) | |
0ac7d722 | 343 | env_set_default(errmsg, 0); |
c5951991 | 344 | |
d196bd88 | 345 | #endif |
c5951991 | 346 | return ret; |
d196bd88 MH |
347 | } |
348 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ | |
c5951991 | 349 | static int env_mmc_load(void) |
a8060359 TL |
350 | { |
351 | #if !defined(ENV_IS_EMBEDDED) | |
cd0f4fa1 | 352 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
b9c8ccab | 353 | struct mmc *mmc; |
97039ab9 | 354 | u32 offset; |
9404a5fc | 355 | int ret; |
e92029c0 | 356 | int dev = mmc_get_env_dev(); |
c75648d7 | 357 | const char *errmsg; |
0536b440 | 358 | env_t *ep = NULL; |
b9c8ccab | 359 | |
b9c8ccab | 360 | mmc = find_mmc_device(dev); |
a8060359 | 361 | |
c75648d7 TH |
362 | errmsg = init_mmc_for_env(mmc); |
363 | if (errmsg) { | |
c5951991 | 364 | ret = -EIO; |
9404a5fc SW |
365 | goto err; |
366 | } | |
a8060359 | 367 | |
d196bd88 | 368 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
c5951991 | 369 | ret = -EIO; |
9404a5fc SW |
370 | goto fini; |
371 | } | |
372 | ||
cd0f4fa1 | 373 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
c75648d7 | 374 | errmsg = "!read failed"; |
c5951991 | 375 | ret = -EIO; |
9404a5fc SW |
376 | goto fini; |
377 | } | |
a8060359 | 378 | |
890feeca | 379 | ret = env_import(buf, 1, H_EXTERNAL); |
0536b440 PG |
380 | if (!ret) { |
381 | ep = (env_t *)buf; | |
382 | gd->env_addr = (ulong)&ep->data; | |
383 | } | |
9404a5fc SW |
384 | |
385 | fini: | |
386 | fini_mmc_for_env(mmc); | |
387 | err: | |
388 | if (ret) | |
0ac7d722 | 389 | env_set_default(errmsg, 0); |
a8060359 | 390 | #endif |
c5951991 | 391 | return ret; |
a8060359 | 392 | } |
d196bd88 | 393 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
4415f1d1 SG |
394 | |
395 | U_BOOT_ENV_LOCATION(mmc) = { | |
396 | .location = ENVL_MMC, | |
ac358beb | 397 | ENV_NAME("MMC") |
e5bce247 | 398 | .load = env_mmc_load, |
4415f1d1 | 399 | #ifndef CONFIG_SPL_BUILD |
e5bce247 | 400 | .save = env_save_ptr(env_mmc_save), |
34853925 FW |
401 | #if defined(CONFIG_CMD_ERASEENV) |
402 | .erase = env_mmc_erase, | |
403 | #endif | |
4415f1d1 | 404 | #endif |
4415f1d1 | 405 | }; |