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