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