Merge branch 'master' of git://git.denx.de/u-boot-socfpga
[oweals/u-boot.git] / tools / env / fw_env.c
index 600fe5dce0098de500d7c1cf0ca7fa1114e045e8..cfada0ee1157a9e8acdb612cb7a74281720d3af8 100644 (file)
@@ -1,11 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000-2010
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
  * (C) Copyright 2008
  * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #define _GNU_SOURCE
@@ -14,6 +13,7 @@
 #include <errno.h>
 #include <env_flags.h>
 #include <fcntl.h>
+#include <libgen.h>
 #include <linux/fs.h>
 #include <linux/stringify.h>
 #include <ctype.h>
@@ -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)
 {
@@ -1225,9 +1225,48 @@ static int flash_read(int fd)
        return 0;
 }
 
+static int flash_open_tempfile(const char **dname, const char **target_temp)
+{
+       char *dup_name = strdup(DEVNAME(dev_current));
+       char *temp_name = NULL;
+       int rc = -1;
+
+       if (!dup_name)
+               return -1;
+
+       *dname = dirname(dup_name);
+       if (!*dname)
+               goto err;
+
+       rc = asprintf(&temp_name, "%s/XXXXXX", *dname);
+       if (rc == -1)
+               goto err;
+
+       rc = mkstemp(temp_name);
+       if (rc == -1) {
+               /* fall back to in place write */
+               fprintf(stderr,
+                       "Can't create %s: %s\n", temp_name, strerror(errno));
+               free(temp_name);
+       } else {
+               *target_temp = temp_name;
+               /* deliberately leak dup_name as dname /might/ point into
+                * it and we need it for our caller
+                */
+               dup_name = NULL;
+       }
+
+err:
+       if (dup_name)
+               free(dup_name);
+
+       return rc;
+}
+
 static int flash_io_write(int fd_current)
 {
-       int fd_target, rc, dev_target;
+       int fd_target = -1, rc, dev_target;
+       const char *dname, *target_temp = NULL;
 
        if (have_redund_env) {
                /* switch to next partition for writing */
@@ -1242,8 +1281,17 @@ static int flash_io_write(int fd_current)
                        goto exit;
                }
        } else {
+               struct stat sb;
+
+               if (fstat(fd_current, &sb) == 0 && S_ISREG(sb.st_mode)) {
+                       /* if any part of flash_open_tempfile() fails we fall
+                        * back to in-place writes
+                        */
+                       fd_target = flash_open_tempfile(&dname, &target_temp);
+               }
                dev_target = dev_current;
-               fd_target = fd_current;
+               if (fd_target == -1)
+                       fd_target = fd_current;
        }
 
        rc = flash_write(fd_current, fd_target, dev_target);
@@ -1254,7 +1302,7 @@ static int flash_io_write(int fd_current)
                        DEVNAME(dev_current), strerror(errno));
        }
 
-       if (have_redund_env) {
+       if (fd_current != fd_target) {
                if (fsync(fd_target) &&
                    !(errno == EINVAL || errno == EROFS)) {
                        fprintf(stderr,
@@ -1268,6 +1316,34 @@ static int flash_io_write(int fd_current)
                                DEVNAME(dev_target), strerror(errno));
                        rc = -1;
                }
+
+               if (target_temp) {
+                       int dir_fd;
+
+                       dir_fd = open(dname, O_DIRECTORY | O_RDONLY);
+                       if (dir_fd == -1)
+                               fprintf(stderr,
+                                       "Can't open %s: %s\n",
+                                       dname, strerror(errno));
+
+                       if (rename(target_temp, DEVNAME(dev_target))) {
+                               fprintf(stderr,
+                                       "rename failed %s => %s: %s\n",
+                                       target_temp, DEVNAME(dev_target),
+                                       strerror(errno));
+                               rc = -1;
+                       }
+
+                       if (dir_fd != -1 && fsync(dir_fd))
+                               fprintf(stderr,
+                                       "fsync failed on %s: %s\n",
+                                       dname, strerror(errno));
+
+                       if (dir_fd != -1 && close(dir_fd))
+                               fprintf(stderr,
+                                       "I/O error on %s: %s\n",
+                                       dname, strerror(errno));
+               }
        }
  exit:
        return rc;
@@ -1490,7 +1566,7 @@ int fw_env_open(struct env_opts *opts)
                free(addr0);
 
        if (addr1)
-               free(addr0);
+               free(addr1);
 
        return ret;
 }
@@ -1666,7 +1742,7 @@ static int parse_config(struct env_opts *opts)
 
                if (ENVSIZE(0) != ENVSIZE(1)) {
                        fprintf(stderr,
-                               "Redundant environments have unequal size");
+                               "Redundant environments have unequal size\n");
                        return -1;
                }
        }
@@ -1684,19 +1760,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));
@@ -1712,6 +1789,7 @@ static int get_config(char *fname)
 
                i++;
        }
+       free(line);
        fclose(fp);
 
        have_redund_env = i - 1;