]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c609719b | 2 | /* |
ea882baf | 3 | * (C) Copyright 2000-2010 |
c609719b WD |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
5 | * | |
6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
7 | * Andreas Heppel <[email protected]> | |
c609719b WD |
8 | */ |
9 | ||
10 | /* #define DEBUG */ | |
11 | ||
12 | #include <common.h> | |
c609719b | 13 | #include <command.h> |
db197010 | 14 | #include <env.h> |
f3998fdc | 15 | #include <env_internal.h> |
0ee48252 | 16 | #include <flash.h> |
f7ae49fc | 17 | #include <log.h> |
c609719b | 18 | #include <linux/stddef.h> |
47cd00fa | 19 | #include <malloc.h> |
ea882baf WD |
20 | #include <search.h> |
21 | #include <errno.h> | |
3db71108 | 22 | #include <u-boot/crc.h> |
c609719b | 23 | |
d87080b7 WD |
24 | DECLARE_GLOBAL_DATA_PTR; |
25 | ||
4415f1d1 SG |
26 | #ifndef CONFIG_SPL_BUILD |
27 | # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH) | |
28 | # define CMD_SAVEENV | |
29 | # elif defined(CONFIG_ENV_ADDR_REDUND) | |
30 | # error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH | |
31 | # endif | |
c609719b WD |
32 | #endif |
33 | ||
4415f1d1 SG |
34 | /* TODO([email protected]): Figure out all these special cases */ |
35 | #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \ | |
36 | !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \ | |
37 | !defined(CONFIG_TARGET_EDMINIV2)) || \ | |
38 | !defined(CONFIG_SPL_BUILD) | |
39 | #define LOADENV | |
40 | #endif | |
41 | ||
42 | #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD) | |
43 | #define INITENV | |
44 | #endif | |
45 | ||
b6cba297 RB |
46 | #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \ |
47 | !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV) | |
c609719b | 48 | #ifdef ENV_IS_EMBEDDED |
be54ec16 | 49 | static env_t *env_ptr = &embedded_environment; |
c609719b WD |
50 | #else /* ! ENV_IS_EMBEDDED */ |
51 | ||
b6cba297 | 52 | static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
c609719b | 53 | #endif /* ENV_IS_EMBEDDED */ |
b6cba297 RB |
54 | #endif |
55 | static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR; | |
c609719b | 56 | |
ea882baf | 57 | /* CONFIG_ENV_ADDR is supposed to be on sector boundary */ |
e5bce247 SG |
58 | static ulong __maybe_unused end_addr = |
59 | CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1; | |
ea882baf | 60 | |
0e8d1586 | 61 | #ifdef CONFIG_ENV_ADDR_REDUND |
e5bce247 SG |
62 | |
63 | static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND; | |
c609719b | 64 | |
ea882baf | 65 | /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */ |
e5bce247 SG |
66 | static ulong __maybe_unused end_addr_new = |
67 | CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1; | |
0e8d1586 | 68 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
c609719b | 69 | |
0e8d1586 | 70 | #ifdef CONFIG_ENV_ADDR_REDUND |
4415f1d1 | 71 | #ifdef INITENV |
e5bce247 | 72 | static int env_flash_init(void) |
c609719b | 73 | { |
8ed96046 | 74 | int crc1_ok = 0, crc2_ok = 0; |
c609719b WD |
75 | |
76 | uchar flag1 = flash_addr->flags; | |
77 | uchar flag2 = flash_addr_new->flags; | |
78 | ||
79 | ulong addr_default = (ulong)&default_environment[0]; | |
80 | ulong addr1 = (ulong)&(flash_addr->data); | |
81 | ulong addr2 = (ulong)&(flash_addr_new->data); | |
82 | ||
82b54b97 IG |
83 | crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc; |
84 | crc2_ok = | |
85 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc; | |
86 | ||
87 | if (crc1_ok && !crc2_ok) { | |
88 | gd->env_addr = addr1; | |
203e94f6 | 89 | gd->env_valid = ENV_VALID; |
82b54b97 IG |
90 | } else if (!crc1_ok && crc2_ok) { |
91 | gd->env_addr = addr2; | |
203e94f6 | 92 | gd->env_valid = ENV_VALID; |
82b54b97 IG |
93 | } else if (!crc1_ok && !crc2_ok) { |
94 | gd->env_addr = addr_default; | |
2d7cb5b4 | 95 | gd->env_valid = ENV_INVALID; |
d3716dd6 SG |
96 | } else if (flag1 == ENV_REDUND_ACTIVE && |
97 | flag2 == ENV_REDUND_OBSOLETE) { | |
82b54b97 | 98 | gd->env_addr = addr1; |
203e94f6 | 99 | gd->env_valid = ENV_VALID; |
d3716dd6 SG |
100 | } else if (flag1 == ENV_REDUND_OBSOLETE && |
101 | flag2 == ENV_REDUND_ACTIVE) { | |
82b54b97 | 102 | gd->env_addr = addr2; |
203e94f6 | 103 | gd->env_valid = ENV_VALID; |
c3f9d493 | 104 | } else if (flag1 == flag2) { |
82b54b97 | 105 | gd->env_addr = addr1; |
203e94f6 | 106 | gd->env_valid = ENV_REDUND; |
c3f9d493 | 107 | } else if (flag1 == 0xFF) { |
82b54b97 | 108 | gd->env_addr = addr1; |
203e94f6 | 109 | gd->env_valid = ENV_REDUND; |
c3f9d493 | 110 | } else if (flag2 == 0xFF) { |
82b54b97 | 111 | gd->env_addr = addr2; |
203e94f6 | 112 | gd->env_valid = ENV_REDUND; |
c609719b WD |
113 | } |
114 | ||
ea882baf | 115 | return 0; |
c609719b | 116 | } |
4415f1d1 | 117 | #endif |
c609719b WD |
118 | |
119 | #ifdef CMD_SAVEENV | |
e5bce247 | 120 | static int env_flash_save(void) |
c609719b | 121 | { |
ea882baf | 122 | env_t env_new; |
7ce1526e | 123 | char *saved_data = NULL; |
d3716dd6 | 124 | char flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE; |
82b54b97 | 125 | int rc = 1; |
0e8d1586 | 126 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
ea882baf | 127 | ulong up_data = 0; |
500545cc | 128 | #endif |
c609719b | 129 | |
82b54b97 | 130 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
c609719b | 131 | |
82b54b97 | 132 | if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) |
ea882baf | 133 | goto done; |
c609719b | 134 | |
ea882baf | 135 | debug("Protect off %08lX ... %08lX\n", |
c609719b WD |
136 | (ulong)flash_addr_new, end_addr_new); |
137 | ||
82b54b97 | 138 | if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) |
ea882baf | 139 | goto done; |
ea882baf | 140 | |
7ce1526e MV |
141 | rc = env_export(&env_new); |
142 | if (rc) | |
143 | return rc; | |
82b54b97 | 144 | env_new.flags = new_flag; |
c609719b | 145 | |
0e8d1586 | 146 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
82b54b97 | 147 | up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE); |
ea882baf | 148 | debug("Data to save 0x%lX\n", up_data); |
47cd00fa | 149 | if (up_data) { |
82b54b97 IG |
150 | saved_data = malloc(up_data); |
151 | if (saved_data == NULL) { | |
8bde7f77 | 152 | printf("Unable to save the rest of sector (%ld)\n", |
47cd00fa | 153 | up_data); |
ea882baf | 154 | goto done; |
47cd00fa | 155 | } |
8bde7f77 | 156 | memcpy(saved_data, |
82b54b97 IG |
157 | (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), |
158 | up_data); | |
ea882baf WD |
159 | debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n", |
160 | (long)flash_addr_new + CONFIG_ENV_SIZE, | |
161 | up_data, saved_data); | |
47cd00fa WD |
162 | } |
163 | #endif | |
ea882baf | 164 | puts("Erasing Flash..."); |
82b54b97 | 165 | debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new); |
c609719b | 166 | |
82b54b97 | 167 | if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) |
ea882baf | 168 | goto done; |
c609719b | 169 | |
ea882baf WD |
170 | puts("Writing to Flash... "); |
171 | debug(" %08lX ... %08lX ...", | |
c609719b | 172 | (ulong)&(flash_addr_new->data), |
82b54b97 IG |
173 | sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data)); |
174 | rc = flash_write((char *)&env_new, (ulong)flash_addr_new, | |
175 | sizeof(env_new)); | |
176 | if (rc) | |
177 | goto perror; | |
178 | ||
179 | rc = flash_write(&flag, (ulong)&(flash_addr->flags), | |
180 | sizeof(flash_addr->flags)); | |
181 | if (rc) | |
182 | goto perror; | |
c609719b | 183 | |
0e8d1586 | 184 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
47cd00fa | 185 | if (up_data) { /* restore the rest of sector */ |
ea882baf WD |
186 | debug("Restoring the rest of data to 0x%lX len 0x%lX\n", |
187 | (long)flash_addr_new + CONFIG_ENV_SIZE, up_data); | |
8bde7f77 | 188 | if (flash_write(saved_data, |
0e8d1586 | 189 | (long)flash_addr_new + CONFIG_ENV_SIZE, |
82b54b97 IG |
190 | up_data)) |
191 | goto perror; | |
47cd00fa WD |
192 | } |
193 | #endif | |
ea882baf WD |
194 | puts("done\n"); |
195 | ||
c609719b | 196 | { |
82b54b97 | 197 | env_t *etmp = flash_addr; |
c609719b WD |
198 | ulong ltmp = end_addr; |
199 | ||
200 | flash_addr = flash_addr_new; | |
201 | flash_addr_new = etmp; | |
202 | ||
203 | end_addr = end_addr_new; | |
204 | end_addr_new = ltmp; | |
205 | } | |
206 | ||
207 | rc = 0; | |
82b54b97 IG |
208 | goto done; |
209 | perror: | |
210 | flash_perror(rc); | |
ea882baf | 211 | done: |
47cd00fa | 212 | if (saved_data) |
ea882baf | 213 | free(saved_data); |
c609719b | 214 | /* try to re-protect */ |
82b54b97 IG |
215 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
216 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); | |
c609719b WD |
217 | |
218 | return rc; | |
219 | } | |
220 | #endif /* CMD_SAVEENV */ | |
221 | ||
0e8d1586 | 222 | #else /* ! CONFIG_ENV_ADDR_REDUND */ |
c609719b | 223 | |
4415f1d1 | 224 | #ifdef INITENV |
e5bce247 | 225 | static int env_flash_init(void) |
c609719b | 226 | { |
c609719b | 227 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
82b54b97 | 228 | gd->env_addr = (ulong)&(env_ptr->data); |
203e94f6 | 229 | gd->env_valid = ENV_VALID; |
82b54b97 | 230 | return 0; |
c609719b | 231 | } |
9bb8b209 | 232 | |
82b54b97 | 233 | gd->env_addr = (ulong)&default_environment[0]; |
2d7cb5b4 | 234 | gd->env_valid = ENV_INVALID; |
ea882baf | 235 | return 0; |
c609719b | 236 | } |
4415f1d1 | 237 | #endif |
c609719b WD |
238 | |
239 | #ifdef CMD_SAVEENV | |
e5bce247 | 240 | static int env_flash_save(void) |
c609719b | 241 | { |
ea882baf | 242 | env_t env_new; |
ea882baf | 243 | int rc = 1; |
3fa1981e | 244 | char *saved_data = NULL; |
ea882baf WD |
245 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
246 | ulong up_data = 0; | |
c609719b | 247 | |
82b54b97 | 248 | up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE); |
ea882baf WD |
249 | debug("Data to save 0x%lx\n", up_data); |
250 | if (up_data) { | |
82b54b97 IG |
251 | saved_data = malloc(up_data); |
252 | if (saved_data == NULL) { | |
ea882baf WD |
253 | printf("Unable to save the rest of sector (%ld)\n", |
254 | up_data); | |
255 | goto done; | |
256 | } | |
257 | memcpy(saved_data, | |
258 | (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data); | |
259 | debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n", | |
260 | (ulong)flash_addr + CONFIG_ENV_SIZE, | |
261 | up_data, | |
262 | (ulong)saved_data); | |
263 | } | |
0e8d1586 | 264 | #endif /* CONFIG_ENV_SECT_SIZE */ |
c609719b | 265 | |
82b54b97 | 266 | debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr); |
c609719b | 267 | |
ea882baf WD |
268 | if (flash_sect_protect(0, (long)flash_addr, end_addr)) |
269 | goto done; | |
c609719b | 270 | |
7ce1526e MV |
271 | rc = env_export(&env_new); |
272 | if (rc) | |
ea882baf | 273 | goto done; |
c609719b | 274 | |
ea882baf WD |
275 | puts("Erasing Flash..."); |
276 | if (flash_sect_erase((long)flash_addr, end_addr)) | |
277 | goto done; | |
c609719b | 278 | |
ea882baf WD |
279 | puts("Writing to Flash... "); |
280 | rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE); | |
82b54b97 IG |
281 | if (rc != 0) |
282 | goto perror; | |
283 | ||
ea882baf WD |
284 | #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE |
285 | if (up_data) { /* restore the rest of sector */ | |
286 | debug("Restoring the rest of data to 0x%lx len 0x%lx\n", | |
287 | (ulong)flash_addr + CONFIG_ENV_SIZE, up_data); | |
288 | if (flash_write(saved_data, | |
289 | (long)flash_addr + CONFIG_ENV_SIZE, | |
82b54b97 IG |
290 | up_data)) |
291 | goto perror; | |
ea882baf WD |
292 | } |
293 | #endif | |
294 | puts("done\n"); | |
295 | rc = 0; | |
82b54b97 IG |
296 | goto done; |
297 | perror: | |
298 | flash_perror(rc); | |
ea882baf WD |
299 | done: |
300 | if (saved_data) | |
301 | free(saved_data); | |
c609719b | 302 | /* try to re-protect */ |
82b54b97 | 303 | flash_sect_protect(1, (long)flash_addr, end_addr); |
ea882baf | 304 | return rc; |
c609719b | 305 | } |
c609719b WD |
306 | #endif /* CMD_SAVEENV */ |
307 | ||
0e8d1586 | 308 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
c609719b | 309 | |
e5bce247 | 310 | #ifdef LOADENV |
c5951991 | 311 | static int env_flash_load(void) |
c609719b | 312 | { |
0e8d1586 | 313 | #ifdef CONFIG_ENV_ADDR_REDUND |
c3f9d493 | 314 | if (gd->env_addr != (ulong)&(flash_addr->data)) { |
ea882baf | 315 | env_t *etmp = flash_addr; |
c609719b WD |
316 | ulong ltmp = end_addr; |
317 | ||
318 | flash_addr = flash_addr_new; | |
319 | flash_addr_new = etmp; | |
320 | ||
321 | end_addr = end_addr_new; | |
322 | end_addr_new = ltmp; | |
323 | } | |
324 | ||
d3716dd6 | 325 | if (flash_addr_new->flags != ENV_REDUND_OBSOLETE && |
82b54b97 | 326 | crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) { |
d3716dd6 | 327 | char flag = ENV_REDUND_OBSOLETE; |
c3f9d493 | 328 | |
203e94f6 | 329 | gd->env_valid = ENV_REDUND; |
ea882baf | 330 | flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new); |
c3f9d493 | 331 | flash_write(&flag, |
8bde7f77 WD |
332 | (ulong)&(flash_addr_new->flags), |
333 | sizeof(flash_addr_new->flags)); | |
ea882baf | 334 | flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new); |
c609719b WD |
335 | } |
336 | ||
d3716dd6 SG |
337 | if (flash_addr->flags != ENV_REDUND_ACTIVE && |
338 | (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) { | |
339 | char flag = ENV_REDUND_ACTIVE; | |
c3f9d493 | 340 | |
203e94f6 | 341 | gd->env_valid = ENV_REDUND; |
ea882baf | 342 | flash_sect_protect(0, (ulong)flash_addr, end_addr); |
c3f9d493 | 343 | flash_write(&flag, |
8bde7f77 WD |
344 | (ulong)&(flash_addr->flags), |
345 | sizeof(flash_addr->flags)); | |
ea882baf | 346 | flash_sect_protect(1, (ulong)flash_addr, end_addr); |
c609719b WD |
347 | } |
348 | ||
203e94f6 | 349 | if (gd->env_valid == ENV_REDUND) |
82b54b97 IG |
350 | puts("*** Warning - some problems detected " |
351 | "reading environment; recovered successfully\n\n"); | |
0e8d1586 | 352 | #endif /* CONFIG_ENV_ADDR_REDUND */ |
ea882baf | 353 | |
2166ebf7 | 354 | return env_import((char *)flash_addr, 1); |
c609719b | 355 | } |
e5bce247 | 356 | #endif /* LOADENV */ |
4415f1d1 SG |
357 | |
358 | U_BOOT_ENV_LOCATION(flash) = { | |
359 | .location = ENVL_FLASH, | |
ac358beb | 360 | ENV_NAME("Flash") |
4415f1d1 | 361 | #ifdef LOADENV |
e5bce247 | 362 | .load = env_flash_load, |
4415f1d1 SG |
363 | #endif |
364 | #ifdef CMD_SAVEENV | |
e5bce247 | 365 | .save = env_save_ptr(env_flash_save), |
4415f1d1 SG |
366 | #endif |
367 | #ifdef INITENV | |
e5bce247 | 368 | .init = env_flash_init, |
4415f1d1 SG |
369 | #endif |
370 | }; |