Commit 31f044bd91df58bed6bb8cfadfc187eedac1442e

Authored by Simon Goldschmidt
Committed by Tom Rini
1 parent 42a1820bbc

env: move more common code to env_import_redund

There is more common code in mmc, nand and ubi env drivers that
can be shared by moving to env_import_redund.

For this, a status/error value whether the buffers were loaded
are passed as additional parameters to env_import_redund.
Ideally, these are already returned to the env driver by the
storage driver. This is the case for mmc, nand and ubi, so for
this change, code deduplicated.

Signed-off-by: Simon Goldschmidt <sgoldschmidt@de.pepperl-fuchs.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>

Showing 5 changed files with 36 additions and 51 deletions Side-by-side Diff

... ... @@ -138,13 +138,32 @@
138 138 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
139 139 static unsigned char env_flags;
140 140  
141   -int env_import_redund(const char *buf1, const char *buf2)
  141 +int env_import_redund(const char *buf1, int buf1_read_fail,
  142 + const char *buf2, int buf2_read_fail)
142 143 {
143 144 int crc1_ok, crc2_ok;
144 145 env_t *ep, *tmp_env1, *tmp_env2;
145 146  
146 147 tmp_env1 = (env_t *)buf1;
147 148 tmp_env2 = (env_t *)buf2;
  149 +
  150 + if (buf1_read_fail && buf2_read_fail) {
  151 + puts("*** Error - No Valid Environment Area found\n");
  152 + } else if (buf1_read_fail || buf2_read_fail) {
  153 + puts("*** Warning - some problems detected ");
  154 + puts("reading environment; recovered successfully\n");
  155 + }
  156 +
  157 + if (buf1_read_fail && buf2_read_fail) {
  158 + set_default_env("!bad env area");
  159 + return -EIO;
  160 + } else if (!buf1_read_fail && buf2_read_fail) {
  161 + gd->env_valid = ENV_VALID;
  162 + return env_import((char *)tmp_env1, 1);
  163 + } else if (buf1_read_fail && !buf2_read_fail) {
  164 + gd->env_valid = ENV_REDUND;
  165 + return env_import((char *)tmp_env2, 1);
  166 + }
148 167  
149 168 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
150 169 tmp_env1->crc;
... ... @@ -290,27 +290,8 @@
290 290 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
291 291 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
292 292  
293   - if (read1_fail && read2_fail)
294   - puts("*** Error - No Valid Environment Area found\n");
295   - else if (read1_fail || read2_fail)
296   - puts("*** Warning - some problems detected "
297   - "reading environment; recovered successfully\n");
298   -
299   - if (read1_fail && read2_fail) {
300   - errmsg = "!bad CRC";
301   - ret = -EIO;
302   - goto fini;
303   - } else if (!read1_fail && read2_fail) {
304   - gd->env_valid = ENV_VALID;
305   - env_import((char *)tmp_env1, 1);
306   - } else if (read1_fail && !read2_fail) {
307   - gd->env_valid = ENV_REDUND;
308   - env_import((char *)tmp_env2, 1);
309   - } else {
310   - env_import_redund((char *)tmp_env1, (char *)tmp_env2);
311   - }
312   -
313   - ret = 0;
  293 + ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
  294 + read2_fail);
314 295  
315 296 fini:
316 297 fini_mmc_for_env(mmc);
... ... @@ -320,7 +320,7 @@
320 320 #if defined(ENV_IS_EMBEDDED)
321 321 return 0;
322 322 #else
323   - int read1_fail = 0, read2_fail = 0;
  323 + int read1_fail, read2_fail;
324 324 env_t *tmp_env1, *tmp_env2;
325 325 int ret = 0;
326 326  
... ... @@ -336,24 +336,8 @@
336 336 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
337 337 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
338 338  
339   - if (read1_fail && read2_fail)
340   - puts("*** Error - No Valid Environment Area found\n");
341   - else if (read1_fail || read2_fail)
342   - puts("*** Warning - some problems detected "
343   - "reading environment; recovered successfully\n");
344   -
345   - if (read1_fail && read2_fail) {
346   - set_default_env("!bad env area");
347   - goto done;
348   - } else if (!read1_fail && read2_fail) {
349   - gd->env_valid = ENV_VALID;
350   - env_import((char *)tmp_env1, 1);
351   - } else if (read1_fail && !read2_fail) {
352   - gd->env_valid = ENV_REDUND;
353   - env_import((char *)tmp_env2, 1);
354   - } else {
355   - env_import_redund((char *)tmp_env1, (char *)tmp_env2);
356   - }
  339 + ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
  340 + read2_fail);
357 341  
358 342 done:
359 343 free(tmp_env1);
... ... @@ -95,6 +95,7 @@
95 95 {
96 96 ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
97 97 ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  98 + int read1_fail, read2_fail;
98 99 env_t *tmp_env1, *tmp_env2;
99 100  
100 101 /*
101 102  
102 103  
103 104  
104 105  
... ... @@ -118,21 +119,20 @@
118 119 return -EIO;
119 120 }
120 121  
121   - if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
122   - CONFIG_ENV_SIZE)) {
  122 + read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  123 + CONFIG_ENV_SIZE);
  124 + if (read1_fail)
123 125 printf("\n** Unable to read env from %s:%s **\n",
124 126 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
125   - }
126 127  
127   - if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
128   - CONFIG_ENV_SIZE)) {
  128 + read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
  129 + (void *)tmp_env2, CONFIG_ENV_SIZE);
  130 + if (read2_fail)
129 131 printf("\n** Unable to read redundant env from %s:%s **\n",
130 132 CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
131   - }
132 133  
133   - env_import_redund((char *)tmp_env1, (char *)tmp_env2);
134   -
135   - return 0;
  134 + return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
  135 + read2_fail);
136 136 }
137 137 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
138 138 static int env_ubi_load(void)
include/environment.h
... ... @@ -297,7 +297,8 @@
297 297  
298 298 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
299 299 /* Select and import one of two redundant environments */
300   -int env_import_redund(const char *buf1, const char *buf2);
  300 +int env_import_redund(const char *buf1, int buf1_status,
  301 + const char *buf2, int buf2_status);
301 302 #endif
302 303  
303 304 /**