Commit 2dc55d9ede62cd2af2a6d813115373a462c2b3dc

Authored by thomas.langer@lantiq.com
Committed by Wolfgang Denk
1 parent f38536f913

fix redundant environment for serial flash

This patch fixes problems in the handling of redundant environment in env_sf.c

The major problem are double calls of free() on the allocated buffers,
which damages the internal data of malloc and crashes on next call.

In addition, the selection of the active environment had errors and compiler
warnings, which are corrected by this patch.

Signed-off-by: Thomas Langer <thomas.langer@lantiq.com>

Showing 1 changed file with 12 additions and 27 deletions Side-by-side Diff

... ... @@ -59,7 +59,6 @@
59 59 extern uchar default_environment[];
60 60  
61 61 char * env_name_spec = "SPI Flash";
62   -env_t *env_ptr;
63 62  
64 63 static struct spi_flash *env_flash;
65 64  
... ... @@ -79,7 +78,7 @@
79 78 char *saved_buffer = NULL;
80 79 u32 sector = 1;
81 80 int ret;
82   - char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
  81 + char flag = OBSOLETE_FLAG;
83 82  
84 83 if (!env_flash) {
85 84 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
... ... @@ -159,7 +158,7 @@
159 158  
160 159 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
161 160  
162   - printf("Valid environment: %d\n", gd->env_valid);
  161 + printf("Valid environment: %d\n", (int)gd->env_valid);
163 162  
164 163 done:
165 164 if (saved_buffer)
166 165  
167 166  
168 167  
... ... @@ -174,25 +173,20 @@
174 173 env_t *tmp_env1 = NULL;
175 174 env_t *tmp_env2 = NULL;
176 175 env_t *ep = NULL;
177   - uchar flag1, flag2;
178   - /* current_env is set only in case both areas are valid! */
179   - int current_env = 0;
180 176  
181 177 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
182 178 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
183 179  
184 180 if (!tmp_env1 || !tmp_env2) {
185   - free(tmp_env1);
186   - free(tmp_env2);
187 181 set_default_env("!malloc() failed");
188   - return;
  182 + goto out;
189 183 }
190 184  
191 185 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
192 186 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
193 187 if (!env_flash) {
194 188 set_default_env("!spi_flash_probe() failed");
195   - return;
  189 + goto out;
196 190 }
197 191  
198 192 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
199 193  
200 194  
201 195  
202 196  
203 197  
204 198  
205 199  
206 200  
... ... @@ -204,33 +198,30 @@
204 198  
205 199 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
206 200 crc1_ok = 1;
207   - flag1 = tmp_env1->flags;
208 201  
209 202 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
210 203 CONFIG_ENV_SIZE, tmp_env2);
211 204 if (!ret) {
212 205 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
213 206 crc2_ok = 1;
214   - flag2 = tmp_env2->flags;
215 207 }
216 208  
217 209 if (!crc1_ok && !crc2_ok) {
218   - free(tmp_env1);
219   - free(tmp_env2);
220 210 set_default_env("!bad CRC");
221   - return;
  211 + goto err_read;
222 212 } else if (crc1_ok && !crc2_ok) {
223 213 gd->env_valid = 1;
224   - ep = tmp_env1;
225 214 } else if (!crc1_ok && crc2_ok) {
  215 + gd->env_valid = 2;
  216 + } else if (tmp_env1->flags == ACTIVE_FLAG &&
  217 + tmp_env2->flags == OBSOLETE_FLAG) {
226 218 gd->env_valid = 1;
227   - } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
228   - gd->env_valid = 1;
229   - } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
  219 + } else if (tmp_env1->flags == OBSOLETE_FLAG &&
  220 + tmp_env2->flags == ACTIVE_FLAG) {
230 221 gd->env_valid = 2;
231   - } else if (flag1 == flag2) {
  222 + } else if (tmp_env1->flags == tmp_env2->flags) {
232 223 gd->env_valid = 2;
233   - } else if (flag1 == 0xFF) {
  224 + } else if (tmp_env1->flags == 0xFF) {
234 225 gd->env_valid = 2;
235 226 } else {
236 227 /*
... ... @@ -240,8 +231,6 @@
240 231 gd->env_valid = 2;
241 232 }
242 233  
243   - free(env_ptr);
244   -
245 234 if (gd->env_valid == 1)
246 235 ep = tmp_env1;
247 236 else
... ... @@ -257,10 +246,6 @@
257 246 spi_flash_free(env_flash);
258 247 env_flash = NULL;
259 248 out:
260   - if (tmp_env1)
261   - free(tmp_env1);
262   - if (tmp_env2)
263   - free(tmp_env2);
264 249 free(tmp_env1);
265 250 free(tmp_env2);
266 251