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