Commit cca2011e62289063066faee3cf8e3d74685be8fc
Committed by
Tom Rini
1 parent
47a4bea6af
Exists in
master
and in
53 other branches
env: dataflash: fix env_init issue
As the SPI controller is not initialized before env_init(), it causes reading env in dataflash failed. So, although saveenv() successfully, it shows warning information when reboot the system as following: *** Warning - bad CRC, using default environment Let the env_relocate() to check env CRC and import it. Signed-off-by: Bo Shen <voice.shen@atmel.com>
Showing 1 changed file with 18 additions and 32 deletions Side-by-side Diff
common/env_dataflash.c
... | ... | @@ -29,11 +29,25 @@ |
29 | 29 | |
30 | 30 | void env_relocate_spec(void) |
31 | 31 | { |
32 | + ulong crc, new = 0; | |
33 | + unsigned off; | |
32 | 34 | char buf[CONFIG_ENV_SIZE]; |
33 | 35 | |
36 | + /* Read old CRC */ | |
37 | + read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), | |
38 | + sizeof(ulong), (char *)&crc); | |
39 | + | |
40 | + /* Read whole environment */ | |
34 | 41 | read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); |
35 | 42 | |
36 | - env_import(buf, 1); | |
43 | + /* Calculate the CRC */ | |
44 | + off = offsetof(env_t, data); | |
45 | + new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE); | |
46 | + | |
47 | + if (crc == new) | |
48 | + env_import(buf, 1); | |
49 | + else | |
50 | + set_default_env("!bad CRC"); | |
37 | 51 | } |
38 | 52 | |
39 | 53 | #ifdef CONFIG_ENV_OFFSET_REDUND |
... | ... | @@ -67,37 +81,9 @@ |
67 | 81 | */ |
68 | 82 | int env_init(void) |
69 | 83 | { |
70 | - ulong crc, len = ENV_SIZE, new = 0; | |
71 | - unsigned off; | |
72 | - uchar buf[64]; | |
73 | - | |
74 | - if (gd->env_valid) | |
75 | - return 0; | |
76 | - | |
77 | - AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ | |
78 | - | |
79 | - /* read old CRC */ | |
80 | - read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), | |
81 | - sizeof(ulong), (char *)&crc); | |
82 | - | |
83 | - off = offsetof(env_t, data); | |
84 | - while (len > 0) { | |
85 | - int n = (len > sizeof(buf)) ? sizeof(buf) : len; | |
86 | - | |
87 | - read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); | |
88 | - | |
89 | - new = crc32(new, buf, n); | |
90 | - len -= n; | |
91 | - off += n; | |
92 | - } | |
93 | - | |
94 | - if (crc == new) { | |
95 | - gd->env_addr = offsetof(env_t, data); | |
96 | - gd->env_valid = 1; | |
97 | - } else { | |
98 | - gd->env_addr = (ulong)&default_environment[0]; | |
99 | - gd->env_valid = 0; | |
100 | - } | |
84 | + /* use default */ | |
85 | + gd->env_addr = (ulong)&default_environment[0]; | |
86 | + gd->env_valid = 1; | |
101 | 87 | |
102 | 88 | return 0; |
103 | 89 | } |