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