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