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