]>
Commit | Line | Data |
---|---|---|
a8060359 | 1 | /* |
97039ab9 | 2 | * (C) Copyright 2008-2011 Freescale Semiconductor, Inc. |
a8060359 | 3 | * |
3765b3e7 | 4 | * SPDX-License-Identifier: GPL-2.0+ |
a8060359 TL |
5 | */ |
6 | ||
7 | /* #define DEBUG */ | |
8 | ||
9 | #include <common.h> | |
10 | ||
11 | #include <command.h> | |
12 | #include <environment.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> |
6d1d51b3 | 18 | #include <search.h> |
e79f4839 | 19 | #include <errno.h> |
a8060359 | 20 | |
d196bd88 MH |
21 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
22 | (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) | |
23 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE | |
24 | #endif | |
25 | ||
a8060359 TL |
26 | char *env_name_spec = "MMC"; |
27 | ||
28 | #ifdef ENV_IS_EMBEDDED | |
994bc671 | 29 | env_t *env_ptr = &environment; |
a8060359 | 30 | #else /* ! ENV_IS_EMBEDDED */ |
e8db8f71 | 31 | env_t *env_ptr; |
a8060359 TL |
32 | #endif /* ENV_IS_EMBEDDED */ |
33 | ||
a8060359 TL |
34 | DECLARE_GLOBAL_DATA_PTR; |
35 | ||
97039ab9 MH |
36 | #if !defined(CONFIG_ENV_OFFSET) |
37 | #define CONFIG_ENV_OFFSET 0 | |
38 | #endif | |
39 | ||
f8b8a554 PT |
40 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
41 | static inline s64 mmc_offset(int copy) | |
42 | { | |
43 | const char *propname = "u-boot,mmc-env-offset"; | |
44 | s64 defvalue = CONFIG_ENV_OFFSET; | |
45 | ||
46 | #if defined(CONFIG_ENV_OFFSET_REDUND) | |
47 | if (copy) { | |
48 | propname = "u-boot,mmc-env-offset-redundant"; | |
49 | defvalue = CONFIG_ENV_OFFSET_REDUND; | |
50 | } | |
51 | #endif | |
52 | ||
53 | return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue); | |
54 | } | |
55 | #else | |
56 | static inline s64 mmc_offset(int copy) | |
97039ab9 | 57 | { |
f8b8a554 | 58 | s64 offset = CONFIG_ENV_OFFSET; |
5c088ee8 | 59 | |
f8b8a554 | 60 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
d196bd88 | 61 | if (copy) |
5c088ee8 | 62 | offset = CONFIG_ENV_OFFSET_REDUND; |
d196bd88 | 63 | #endif |
f8b8a554 PT |
64 | return offset; |
65 | } | |
66 | #endif | |
67 | ||
68 | __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) | |
69 | { | |
70 | s64 offset = mmc_offset(copy); | |
5c088ee8 SW |
71 | |
72 | if (offset < 0) | |
73 | offset += mmc->capacity; | |
74 | ||
75 | *env_addr = offset; | |
76 | ||
97039ab9 MH |
77 | return 0; |
78 | } | |
97039ab9 | 79 | |
e92029c0 CG |
80 | __weak int mmc_get_env_dev(void) |
81 | { | |
82 | return CONFIG_SYS_MMC_ENV_DEV; | |
83 | } | |
84 | ||
a8060359 TL |
85 | int env_init(void) |
86 | { | |
87 | /* use default */ | |
e8db8f71 | 88 | gd->env_addr = (ulong)&default_environment[0]; |
203e94f6 | 89 | gd->env_valid = ENV_VALID; |
a8060359 TL |
90 | |
91 | return 0; | |
92 | } | |
93 | ||
b9c8ccab | 94 | #ifdef CONFIG_SYS_MMC_ENV_PART |
6e7b7df4 DL |
95 | __weak uint mmc_get_env_part(struct mmc *mmc) |
96 | { | |
97 | return CONFIG_SYS_MMC_ENV_PART; | |
98 | } | |
99 | ||
873cc1d7 SW |
100 | static unsigned char env_mmc_orig_hwpart; |
101 | ||
6e7b7df4 DL |
102 | static int mmc_set_env_part(struct mmc *mmc) |
103 | { | |
104 | uint part = mmc_get_env_part(mmc); | |
e92029c0 | 105 | int dev = mmc_get_env_dev(); |
6e7b7df4 | 106 | int ret = 0; |
b9c8ccab | 107 | |
69f45cd5 SG |
108 | env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart; |
109 | ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part); | |
873cc1d7 SW |
110 | if (ret) |
111 | puts("MMC partition switch failed\n"); | |
6e7b7df4 DL |
112 | |
113 | return ret; | |
114 | } | |
115 | #else | |
116 | static inline int mmc_set_env_part(struct mmc *mmc) {return 0; }; | |
b9c8ccab TR |
117 | #endif |
118 | ||
c75648d7 | 119 | static const char *init_mmc_for_env(struct mmc *mmc) |
6e7b7df4 | 120 | { |
c75648d7 | 121 | if (!mmc) |
a85da21f | 122 | return "!No MMC card found"; |
a8060359 | 123 | |
01b73fe6 SG |
124 | #ifdef CONFIG_BLK |
125 | struct udevice *dev; | |
126 | ||
127 | if (blk_get_from_parent(mmc->dev, &dev)) | |
128 | return "!No block device"; | |
129 | #else | |
c75648d7 | 130 | if (mmc_init(mmc)) |
a85da21f | 131 | return "!MMC init failed"; |
e7017a3c | 132 | #endif |
c75648d7 | 133 | if (mmc_set_env_part(mmc)) |
a85da21f | 134 | return "!MMC partition switch failed"; |
a8060359 | 135 | |
c75648d7 | 136 | return NULL; |
a8060359 TL |
137 | } |
138 | ||
9404a5fc SW |
139 | static void fini_mmc_for_env(struct mmc *mmc) |
140 | { | |
141 | #ifdef CONFIG_SYS_MMC_ENV_PART | |
e92029c0 | 142 | int dev = mmc_get_env_dev(); |
b9c8ccab | 143 | |
69f45cd5 | 144 | blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart); |
9404a5fc SW |
145 | #endif |
146 | } | |
147 | ||
a8060359 | 148 | #ifdef CONFIG_CMD_SAVEENV |
e8db8f71 IG |
149 | static inline int write_env(struct mmc *mmc, unsigned long size, |
150 | unsigned long offset, const void *buffer) | |
a8060359 TL |
151 | { |
152 | uint blk_start, blk_cnt, n; | |
5461acba | 153 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
a8060359 | 154 | |
e8db8f71 IG |
155 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; |
156 | blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len; | |
a8060359 | 157 | |
5461acba | 158 | n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer); |
a8060359 TL |
159 | |
160 | return (n == blk_cnt) ? 0 : -1; | |
161 | } | |
162 | ||
163 | int saveenv(void) | |
164 | { | |
cd0f4fa1 | 165 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
e92029c0 CG |
166 | int dev = mmc_get_env_dev(); |
167 | struct mmc *mmc = find_mmc_device(dev); | |
e8db8f71 | 168 | u32 offset; |
d196bd88 | 169 | int ret, copy = 0; |
c75648d7 | 170 | const char *errmsg; |
a8060359 | 171 | |
c75648d7 TH |
172 | errmsg = init_mmc_for_env(mmc); |
173 | if (errmsg) { | |
174 | printf("%s\n", errmsg); | |
97039ab9 | 175 | return 1; |
c75648d7 | 176 | } |
97039ab9 | 177 | |
7ce1526e MV |
178 | ret = env_export(env_new); |
179 | if (ret) | |
9404a5fc | 180 | goto fini; |
d196bd88 MH |
181 | |
182 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
203e94f6 | 183 | if (gd->env_valid == ENV_VALID) |
d196bd88 MH |
184 | copy = 1; |
185 | #endif | |
186 | ||
187 | if (mmc_get_env_addr(mmc, copy, &offset)) { | |
188 | ret = 1; | |
189 | goto fini; | |
190 | } | |
191 | ||
e92029c0 | 192 | printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev); |
4036b630 | 193 | if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) { |
a8060359 | 194 | puts("failed\n"); |
9404a5fc SW |
195 | ret = 1; |
196 | goto fini; | |
a8060359 TL |
197 | } |
198 | ||
199 | puts("done\n"); | |
9404a5fc SW |
200 | ret = 0; |
201 | ||
d196bd88 | 202 | #ifdef CONFIG_ENV_OFFSET_REDUND |
203e94f6 | 203 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
d196bd88 MH |
204 | #endif |
205 | ||
9404a5fc SW |
206 | fini: |
207 | fini_mmc_for_env(mmc); | |
208 | return ret; | |
a8060359 TL |
209 | } |
210 | #endif /* CONFIG_CMD_SAVEENV */ | |
211 | ||
e8db8f71 IG |
212 | static inline int read_env(struct mmc *mmc, unsigned long size, |
213 | unsigned long offset, const void *buffer) | |
a8060359 TL |
214 | { |
215 | uint blk_start, blk_cnt, n; | |
5461acba | 216 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
a8060359 | 217 | |
e8db8f71 IG |
218 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
219 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; | |
a8060359 | 220 | |
5461acba | 221 | n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer); |
a8060359 TL |
222 | |
223 | return (n == blk_cnt) ? 0 : -1; | |
224 | } | |
225 | ||
d196bd88 MH |
226 | #ifdef CONFIG_ENV_OFFSET_REDUND |
227 | void env_relocate_spec(void) | |
228 | { | |
229 | #if !defined(ENV_IS_EMBEDDED) | |
b9c8ccab | 230 | struct mmc *mmc; |
d196bd88 MH |
231 | u32 offset1, offset2; |
232 | int read1_fail = 0, read2_fail = 0; | |
d196bd88 | 233 | int ret; |
e92029c0 | 234 | int dev = mmc_get_env_dev(); |
c75648d7 | 235 | const char *errmsg = NULL; |
d196bd88 | 236 | |
452a2722 MN |
237 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1); |
238 | ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1); | |
239 | ||
b9c8ccab TR |
240 | mmc = find_mmc_device(dev); |
241 | ||
c75648d7 TH |
242 | errmsg = init_mmc_for_env(mmc); |
243 | if (errmsg) { | |
d196bd88 MH |
244 | ret = 1; |
245 | goto err; | |
246 | } | |
247 | ||
248 | if (mmc_get_env_addr(mmc, 0, &offset1) || | |
249 | mmc_get_env_addr(mmc, 1, &offset2)) { | |
250 | ret = 1; | |
251 | goto fini; | |
252 | } | |
253 | ||
254 | read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1); | |
255 | read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); | |
256 | ||
257 | if (read1_fail && read2_fail) | |
258 | puts("*** Error - No Valid Environment Area found\n"); | |
259 | else if (read1_fail || read2_fail) | |
260 | puts("*** Warning - some problems detected " | |
261 | "reading environment; recovered successfully\n"); | |
262 | ||
9d364af2 | 263 | if (read1_fail && read2_fail) { |
c75648d7 | 264 | errmsg = "!bad CRC"; |
d196bd88 MH |
265 | ret = 1; |
266 | goto fini; | |
9d364af2 | 267 | } else if (!read1_fail && read2_fail) { |
203e94f6 | 268 | gd->env_valid = ENV_VALID; |
9d364af2 FA |
269 | env_import((char *)tmp_env1, 1); |
270 | } else if (read1_fail && !read2_fail) { | |
203e94f6 | 271 | gd->env_valid = ENV_REDUND; |
9d364af2 | 272 | env_import((char *)tmp_env2, 1); |
d196bd88 | 273 | } else { |
9d364af2 | 274 | env_import_redund((char *)tmp_env1, (char *)tmp_env2); |
d196bd88 MH |
275 | } |
276 | ||
d196bd88 MH |
277 | ret = 0; |
278 | ||
279 | fini: | |
280 | fini_mmc_for_env(mmc); | |
281 | err: | |
282 | if (ret) | |
c75648d7 | 283 | set_default_env(errmsg); |
d196bd88 MH |
284 | #endif |
285 | } | |
286 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ | |
a8060359 TL |
287 | void env_relocate_spec(void) |
288 | { | |
289 | #if !defined(ENV_IS_EMBEDDED) | |
cd0f4fa1 | 290 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
b9c8ccab | 291 | struct mmc *mmc; |
97039ab9 | 292 | u32 offset; |
9404a5fc | 293 | int ret; |
e92029c0 | 294 | int dev = mmc_get_env_dev(); |
c75648d7 | 295 | const char *errmsg; |
b9c8ccab | 296 | |
b9c8ccab | 297 | mmc = find_mmc_device(dev); |
a8060359 | 298 | |
c75648d7 TH |
299 | errmsg = init_mmc_for_env(mmc); |
300 | if (errmsg) { | |
9404a5fc SW |
301 | ret = 1; |
302 | goto err; | |
303 | } | |
a8060359 | 304 | |
d196bd88 | 305 | if (mmc_get_env_addr(mmc, 0, &offset)) { |
9404a5fc SW |
306 | ret = 1; |
307 | goto fini; | |
308 | } | |
309 | ||
cd0f4fa1 | 310 | if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) { |
c75648d7 | 311 | errmsg = "!read failed"; |
9404a5fc SW |
312 | ret = 1; |
313 | goto fini; | |
314 | } | |
a8060359 | 315 | |
cd0f4fa1 | 316 | env_import(buf, 1); |
9404a5fc SW |
317 | ret = 0; |
318 | ||
319 | fini: | |
320 | fini_mmc_for_env(mmc); | |
321 | err: | |
322 | if (ret) | |
c75648d7 | 323 | set_default_env(errmsg); |
a8060359 TL |
324 | #endif |
325 | } | |
d196bd88 | 326 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
4415f1d1 SG |
327 | |
328 | U_BOOT_ENV_LOCATION(mmc) = { | |
329 | .location = ENVL_MMC, | |
330 | .get_char = env_get_char_spec, | |
331 | .load = env_relocate_spec, | |
332 | #ifndef CONFIG_SPL_BUILD | |
333 | .save = env_save_ptr(saveenv), | |
334 | #endif | |
335 | .init = env_init, | |
336 | }; |