]> git.sur5r.net Git - u-boot/blobdiff - tools/env/fw_env.c
fw_printenv: Don't bail out directly after one env read error
[u-boot] / tools / env / fw_env.c
index 4b2caf696041b3fc428691db2ca12269001b8a78..3a5ad026f01732442cefd05e772724ac8298ccd3 100644 (file)
@@ -737,7 +737,8 @@ int fw_env_set(int argc, char *argv[], struct env_opts *opts)
 int fw_parse_script(char *fname, struct env_opts *opts)
 {
        FILE *fp;
-       char dump[1024];        /* Maximum line length in the file */
+       char *line = NULL;
+       size_t linesize = 0;
        char *name;
        char *val;
        int lineno = 0;
@@ -763,36 +764,34 @@ int fw_parse_script(char *fname, struct env_opts *opts)
                }
        }
 
-       while (fgets(dump, sizeof(dump), fp)) {
+       while ((len = getline(&line, &linesize, fp)) != -1) {
                lineno++;
-               len = strlen(dump);
 
                /*
-                * Read a whole line from the file. If the line is too long
-                * or is not terminated, reports an error and exit.
+                * Read a whole line from the file. If the line is not
+                * terminated, reports an error and exit.
                 */
-               if (dump[len - 1] != '\n') {
+               if (line[len - 1] != '\n') {
                        fprintf(stderr,
-                               "Line %d not corrected terminated or too long\n",
+                               "Line %d not correctly terminated\n",
                                lineno);
                        ret = -1;
                        break;
                }
 
                /* Drop ending line feed / carriage return */
-               dump[--len] = '\0';
-               if (len && dump[len - 1] == '\r')
-                       dump[--len] = '\0';
+               line[--len] = '\0';
+               if (len && line[len - 1] == '\r')
+                       line[--len] = '\0';
 
                /* Skip comment or empty lines */
-               if (len == 0 || dump[0] == '#')
+               if (len == 0 || line[0] == '#')
                        continue;
 
                /*
-                * Search for variable's name,
-                * remove leading whitespaces
+                * Search for variable's name remove leading whitespaces
                 */
-               name = skip_blanks(dump);
+               name = skip_blanks(line);
                if (!name)
                        continue;
 
@@ -829,6 +828,7 @@ int fw_parse_script(char *fname, struct env_opts *opts)
                }
 
        }
+       free(line);
 
        /* Close file if not stdin */
        if (strcmp(fname, "-") != 0)
@@ -842,10 +842,10 @@ int fw_parse_script(char *fname, struct env_opts *opts)
 }
 
 /**
- * environment_end() - compute offset of first byte right after environemnt
+ * environment_end() - compute offset of first byte right after environment
  * @dev - index of enviroment buffer
  * Return:
- *  device offset of first byte right after environemnt
+ *  device offset of first byte right after environment
  */
 off_t environment_end(int dev)
 {
@@ -1427,14 +1427,21 @@ int fw_env_open(struct env_opts *opts)
        }
 
        dev_current = 0;
-       if (flash_io(O_RDONLY)) {
+
+       if (!flash_io(O_RDONLY)) {
+               crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE);
+               crc0_ok = (crc0 == *environment.crc);
+       } else if (have_redund_env) {
+               /*
+                * to give the redundant env a chance, maybe it's good:
+                * mark env crc0 invalid then test below if crc1 is ok
+                */
+               crc0_ok = 0;
+       } else {
                ret = -EIO;
                goto open_cleanup;
        }
 
-       crc0 = crc32(0, (uint8_t *)environment.data, ENV_SIZE);
-
-       crc0_ok = (crc0 == *environment.crc);
        if (!have_redund_env) {
                if (!crc0_ok) {
                        fprintf(stderr,
@@ -1462,8 +1469,10 @@ int fw_env_open(struct env_opts *opts)
                 */
                environment.image = addr1;
                if (flash_io(O_RDONLY)) {
-                       ret = -EIO;
-                       goto open_cleanup;
+                       crc1_ok = 0;
+               } else {
+                       crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE);
+                       crc1_ok = (crc1 == redundant->crc);
                }
 
                /* Check flag scheme compatibility */
@@ -1489,9 +1498,6 @@ int fw_env_open(struct env_opts *opts)
                        goto open_cleanup;
                }
 
-               crc1 = crc32(0, (uint8_t *)redundant->data, ENV_SIZE);
-
-               crc1_ok = (crc1 == redundant->crc);
                flag1 = redundant->flags;
 
                if (crc0_ok && !crc1_ok) {
@@ -1760,19 +1766,20 @@ static int get_config(char *fname)
        FILE *fp;
        int i = 0;
        int rc;
-       char dump[128];
+       char *line = NULL;
+       size_t linesize = 0;
        char *devname;
 
        fp = fopen(fname, "r");
        if (fp == NULL)
                return -1;
 
-       while (i < 2 && fgets(dump, sizeof(dump), fp)) {
-               /* Skip incomplete conversions and comment strings */
-               if (dump[0] == '#')
+       while (i < 2 && getline(&line, &linesize, fp) != -1) {
+               /* Skip comment strings */
+               if (line[0] == '#')
                        continue;
 
-               rc = sscanf(dump, "%ms %lli %lx %lx %lx",
+               rc = sscanf(line, "%ms %lli %lx %lx %lx",
                            &devname,
                            &DEVOFFSET(i),
                            &ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
@@ -1788,6 +1795,7 @@ static int get_config(char *fname)
 
                i++;
        }
+       free(line);
        fclose(fp);
 
        have_redund_env = i - 1;