]> git.sur5r.net Git - u-boot/blobdiff - tools/env/fw_env.c
Merge branch 'master' of git://git.denx.de/u-boot-i2c
[u-boot] / tools / env / fw_env.c
index 1654ac0e46bc54a31990fcb80d2f380c4fa999b5..6b0dcaa943ef9607bbda7d77f6d450dafefc4da8 100644 (file)
@@ -14,6 +14,7 @@
 #include <errno.h>
 #include <env_flags.h>
 #include <fcntl.h>
+#include <linux/fs.h>
 #include <linux/stringify.h>
 #include <ctype.h>
 #include <stdio.h>
 
 #include "fw_env.h"
 
-struct common_args common_args;
-struct printenv_args printenv_args;
-struct setenv_args setenv_args;
+struct env_opts default_opts = {
+#ifdef CONFIG_FILE
+       .config_file = CONFIG_FILE
+#endif
+};
 
 #define DIV_ROUND_UP(n, d)     (((n) + (d) - 1) / (d))
 
@@ -49,7 +52,7 @@ struct setenv_args setenv_args;
 
 struct envdev_s {
        const char *devname;            /* Device name */
-       ulong devoff;                   /* Device offset */
+       long long devoff;               /* Device offset */
        ulong env_size;                 /* environment size */
        ulong erase_size;               /* device erase size */
        ulong env_sectors;              /* number of environment sectors */
@@ -119,8 +122,7 @@ static unsigned char obsolete_flag = 0;
 #include <env_default.h>
 
 static int flash_io (int mode);
-static char *envmatch (char * s1, char * s2);
-static int parse_config (void);
+static int parse_config(struct env_opts *opts);
 
 #if defined(CONFIG_FILE)
 static int get_config (char *);
@@ -145,6 +147,24 @@ static char *skip_blanks(char *s)
 }
 
 /*
+ * s1 is either a simple 'name', or a 'name=value' pair.
+ * s2 is a 'name=value' pair.
+ * If the names match, return the value of s2, else NULL.
+ */
+static char *envmatch(char *s1, char *s2)
+{
+       if (s1 == NULL || s2 == NULL)
+               return NULL;
+
+       while (*s1 == *s2++)
+               if (*s1++ == '=')
+                       return s2;
+       if (*s1 == '\0' && *(s2 - 1) == '=')
+               return s2;
+       return NULL;
+}
+
+/**
  * Search the environment for a variable.
  * Return the value, if found, or NULL, if not found.
  */
@@ -228,15 +248,24 @@ int parse_aes_key(char *key, uint8_t *bin_key)
  * Print the current definition of one, or more, or all
  * environment variables
  */
-int fw_printenv(int argc, char *argv[], int value_only)
+int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 {
-       char *env, *nxt;
        int i, rc = 0;
 
-       if (fw_env_open())
+       if (value_only && argc != 1) {
+               fprintf(stderr,
+                       "## Error: `-n' option requires exactly one argument\n");
+               return -1;
+       }
+
+       if (!opts)
+               opts = &default_opts;
+
+       if (fw_env_open(opts))
                return -1;
 
        if (argc == 0) {                /* Print all env variables  */
+               char *env, *nxt;
                for (env = environment.data; *env; env = nxt + 1) {
                        for (nxt = env; *nxt; ++nxt) {
                                if (nxt >= &environment.data[ENV_SIZE]) {
@@ -251,50 +280,38 @@ int fw_printenv(int argc, char *argv[], int value_only)
                return 0;
        }
 
-       if (value_only && argc != 1) {
-               fprintf(stderr,
-                       "## Error: `-n' option requires exactly one argument\n");
-               return -1;
-       }
-
-       for (i = 0; i < argc; ++i) {    /* print single env variables   */
+       for (i = 0; i < argc; ++i) {    /* print a subset of env variables */
                char *name = argv[i];
                char *val = NULL;
 
-               for (env = environment.data; *env; env = nxt + 1) {
-
-                       for (nxt = env; *nxt; ++nxt) {
-                               if (nxt >= &environment.data[ENV_SIZE]) {
-                                       fprintf (stderr, "## Error: "
-                                               "environment not terminated\n");
-                                       return -1;
-                               }
-                       }
-                       val = envmatch (name, env);
-                       if (val) {
-                               if (!value_only) {
-                                       fputs (name, stdout);
-                                       putc ('=', stdout);
-                               }
-                               puts (val);
-                               break;
-                       }
-               }
+               val = fw_getenv(name);
                if (!val) {
                        fprintf (stderr, "## Error: \"%s\" not defined\n", name);
                        rc = -1;
+                       continue;
                }
+
+               if (value_only) {
+                       puts(val);
+                       break;
+               }
+
+               printf("%s=%s\n", name, val);
        }
 
        return rc;
 }
 
-int fw_env_close(void)
+int fw_env_close(struct env_opts *opts)
 {
        int ret;
-       if (common_args.aes_flag) {
+
+       if (!opts)
+               opts = &default_opts;
+
+       if (opts->aes_flag) {
                ret = env_aes_cbc_crypt(environment.data, 1,
-                                       common_args.aes_key);
+                                       opts->aes_key);
                if (ret) {
                        fprintf(stderr,
                                "Error: can't encrypt env for flash\n");
@@ -447,7 +464,7 @@ int fw_env_write(char *name, char *value)
  *         modified or deleted
  *
  */
-int fw_setenv(int argc, char *argv[])
+int fw_setenv(int argc, char *argv[], struct env_opts *opts)
 {
        int i;
        size_t len;
@@ -455,13 +472,16 @@ int fw_setenv(int argc, char *argv[])
        char *value = NULL;
        int valc;
 
+       if (!opts)
+               opts = &default_opts;
+
        if (argc < 1) {
                fprintf(stderr, "## Error: variable name missing\n");
                errno = EINVAL;
                return -1;
        }
 
-       if (fw_env_open()) {
+       if (fw_env_open(opts)) {
                fprintf(stderr, "Error: environment not initialized\n");
                return -1;
        }
@@ -471,7 +491,7 @@ int fw_setenv(int argc, char *argv[])
        valc = argc - 1;
 
        if (env_flags_validate_env_set_params(name, valv, valc) < 0)
-               return 1;
+               return -1;
 
        len = 0;
        for (i = 0; i < valc; ++i) {
@@ -497,7 +517,7 @@ int fw_setenv(int argc, char *argv[])
 
        free(value);
 
-       return fw_env_close();
+       return fw_env_close(opts);
 }
 
 /*
@@ -517,7 +537,7 @@ int fw_setenv(int argc, char *argv[])
  * 0     - OK
  * -1     - Error
  */
-int fw_parse_script(char *fname)
+int fw_parse_script(char *fname, struct env_opts *opts)
 {
        FILE *fp;
        char dump[1024];        /* Maximum line length in the file */
@@ -527,7 +547,10 @@ int fw_parse_script(char *fname)
        int len;
        int ret = 0;
 
-       if (fw_env_open()) {
+       if (!opts)
+               opts = &default_opts;
+
+       if (fw_env_open(opts)) {
                fprintf(stderr, "Error: environment not initialized\n");
                return -1;
        }
@@ -615,10 +638,9 @@ int fw_parse_script(char *fname)
        if (strcmp(fname, "-") != 0)
                fclose(fp);
 
-       ret |= fw_env_close();
+       ret |= fw_env_close(opts);
 
        return ret;
-
 }
 
 /*
@@ -639,8 +661,8 @@ static int flash_bad_block (int fd, uint8_t mtd_type, loff_t *blockstart)
 
                if (badblock) {
 #ifdef DEBUG
-                       fprintf (stderr, "Bad block at 0x%llx, "
-                                "skipping\n", *blockstart);
+                       fprintf (stderr, "Bad block at 0x%llx, skipping\n",
+                               (unsigned long long) *blockstart);
 #endif
                        return badblock;
                }
@@ -727,7 +749,8 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
                }
 #ifdef DEBUG
                fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
-                        rc, blockstart + block_seek, DEVNAME(dev));
+                       rc, (unsigned long long) blockstart + block_seek,
+                       DEVNAME(dev));
 #endif
                processed += readlen;
                readlen = min (blocklen, count - processed);
@@ -825,8 +848,9 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                if (block_seek + count != write_total) {
                        if (block_seek != 0)
                                fprintf(stderr, " and ");
-                       fprintf(stderr, "0x%lx - 0x%x",
-                               block_seek + count, write_total - 1);
+                       fprintf(stderr, "0x%lx - 0x%lx",
+                               (unsigned long) block_seek + count,
+                               (unsigned long) write_total - 1);
                }
                fprintf(stderr, "\n");
 #endif
@@ -889,8 +913,9 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                }
 
 #ifdef DEBUG
-               fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
-                       blockstart);
+               fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
+                       (unsigned long long) erasesize,
+                       (unsigned long long) blockstart);
 #endif
                if (write (fd, data + processed, erasesize) != erasesize) {
                        fprintf (stderr, "Write error on %s: %s\n",
@@ -980,7 +1005,7 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
        }
 
 #ifdef DEBUG
-       fprintf(stderr, "Writing new environment at 0x%lx on %s\n",
+       fprintf(stderr, "Writing new environment at 0x%llx on %s\n",
                DEVOFFSET (dev_target), DEVNAME (dev_target));
 #endif
 
@@ -996,7 +1021,7 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
                        offsetof (struct env_image_redundant, flags);
 #ifdef DEBUG
                fprintf(stderr,
-                       "Setting obsolete flag in environment at 0x%lx on %s\n",
+                       "Setting obsolete flag in environment at 0x%llx on %s\n",
                        DEVOFFSET (dev_current), DEVNAME (dev_current));
 #endif
                flash_flag_obsolete (dev_current, fd_current, offset);
@@ -1007,41 +1032,10 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
 
 static int flash_read (int fd)
 {
-       struct mtd_info_user mtdinfo;
-       struct stat st;
        int rc;
 
-       rc = fstat(fd, &st);
-       if (rc < 0) {
-               fprintf(stderr, "Cannot stat the file %s\n",
-                       DEVNAME(dev_current));
-               return -1;
-       }
-
-       if (S_ISCHR(st.st_mode)) {
-               rc = ioctl(fd, MEMGETINFO, &mtdinfo);
-               if (rc < 0) {
-                       fprintf(stderr, "Cannot get MTD information for %s\n",
-                               DEVNAME(dev_current));
-                       return -1;
-               }
-               if (mtdinfo.type != MTD_NORFLASH &&
-                   mtdinfo.type != MTD_NANDFLASH &&
-                   mtdinfo.type != MTD_DATAFLASH &&
-                   mtdinfo.type != MTD_UBIVOLUME) {
-                       fprintf (stderr, "Unsupported flash type %u on %s\n",
-                                mtdinfo.type, DEVNAME(dev_current));
-                       return -1;
-               }
-       } else {
-               memset(&mtdinfo, 0, sizeof(mtdinfo));
-               mtdinfo.type = MTD_ABSENT;
-       }
-
-       DEVTYPE(dev_current) = mtdinfo.type;
-
        rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
-                            DEVOFFSET (dev_current), mtdinfo.type);
+                           DEVOFFSET(dev_current), DEVTYPE(dev_current));
        if (rc != CUR_ENVSIZE)
                return -1;
 
@@ -1106,29 +1100,10 @@ exit:
        return rc;
 }
 
-/*
- * s1 is either a simple 'name', or a 'name=value' pair.
- * s2 is a 'name=value' pair.
- * If the names match, return the value of s2, else NULL.
- */
-
-static char *envmatch (char * s1, char * s2)
-{
-       if (s1 == NULL || s2 == NULL)
-               return NULL;
-
-       while (*s1 == *s2++)
-               if (*s1++ == '=')
-                       return s2;
-       if (*s1 == '\0' && *(s2 - 1) == '=')
-               return s2;
-       return NULL;
-}
-
 /*
  * Prevent confusion if running from erased flash memory
  */
-int fw_env_open(void)
+int fw_env_open(struct env_opts *opts)
 {
        int crc0, crc0_ok;
        unsigned char flag0;
@@ -1143,7 +1118,10 @@ int fw_env_open(void)
        struct env_image_single *single;
        struct env_image_redundant *redundant;
 
-       if (parse_config ())            /* should fill envdevices */
+       if (!opts)
+               opts = &default_opts;
+
+       if (parse_config(opts))         /* should fill envdevices */
                return -1;
 
        addr0 = calloc(1, CUR_ENVSIZE);
@@ -1175,9 +1153,9 @@ int fw_env_open(void)
 
        crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
 
-       if (common_args.aes_flag) {
+       if (opts->aes_flag) {
                ret = env_aes_cbc_crypt(environment.data, 0,
-                                       common_args.aes_key);
+                                       opts->aes_key);
                if (ret)
                        return ret;
        }
@@ -1233,9 +1211,9 @@ int fw_env_open(void)
 
                crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
 
-               if (common_args.aes_flag) {
+               if (opts->aes_flag) {
                        ret = env_aes_cbc_crypt(redundant->data, 0,
-                                               common_args.aes_key);
+                                               opts->aes_key);
                        if (ret)
                                return ret;
                }
@@ -1311,19 +1289,84 @@ int fw_env_open(void)
        return 0;
 }
 
-
-static int parse_config ()
+static int check_device_config(int dev)
 {
        struct stat st;
+       int fd, rc = 0;
 
-#if defined(CONFIG_FILE)
-       if (!common_args.config_file)
-               common_args.config_file = CONFIG_FILE;
+       fd = open(DEVNAME(dev), O_RDONLY);
+       if (fd < 0) {
+               fprintf(stderr,
+                       "Cannot open %s: %s\n",
+                       DEVNAME(dev), strerror(errno));
+               return -1;
+       }
 
+       rc = fstat(fd, &st);
+       if (rc < 0) {
+               fprintf(stderr, "Cannot stat the file %s\n",
+                       DEVNAME(dev));
+               goto err;
+       }
+
+       if (S_ISCHR(st.st_mode)) {
+               struct mtd_info_user mtdinfo;
+               rc = ioctl(fd, MEMGETINFO, &mtdinfo);
+               if (rc < 0) {
+                       fprintf(stderr, "Cannot get MTD information for %s\n",
+                               DEVNAME(dev));
+                       goto err;
+               }
+               if (mtdinfo.type != MTD_NORFLASH &&
+                   mtdinfo.type != MTD_NANDFLASH &&
+                   mtdinfo.type != MTD_DATAFLASH &&
+                   mtdinfo.type != MTD_UBIVOLUME) {
+                       fprintf(stderr, "Unsupported flash type %u on %s\n",
+                               mtdinfo.type, DEVNAME(dev));
+                       goto err;
+               }
+               DEVTYPE(dev) = mtdinfo.type;
+       } else {
+               uint64_t size;
+               DEVTYPE(dev) = MTD_ABSENT;
+
+               /*
+                * Check for negative offsets, treat it as backwards offset
+                * from the end of the block device
+                */
+               if (DEVOFFSET(dev) < 0) {
+                       rc = ioctl(fd, BLKGETSIZE64, &size);
+                       if (rc < 0) {
+                               fprintf(stderr, "Could not get block device size on %s\n",
+                                       DEVNAME(dev));
+                               goto err;
+                       }
+
+                       DEVOFFSET(dev) = DEVOFFSET(dev) + size;
+#ifdef DEBUG
+                       fprintf(stderr, "Calculated device offset 0x%llx on %s\n",
+                               DEVOFFSET(dev), DEVNAME(dev));
+#endif
+               }
+       }
+
+err:
+       close(fd);
+       return rc;
+}
+
+static int parse_config(struct env_opts *opts)
+{
+       int rc;
+
+       if (!opts)
+               opts = &default_opts;
+
+#if defined(CONFIG_FILE)
        /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
-       if (get_config(common_args.config_file)) {
+       if (get_config(opts->config_file)) {
                fprintf(stderr, "Cannot parse config file '%s': %m\n",
-                       common_args.config_file);
+                       opts->config_file);
                return -1;
        }
 #else
@@ -1358,32 +1401,28 @@ static int parse_config ()
        HaveRedundEnv = 1;
 #endif
 #endif
-       if (stat (DEVNAME (0), &st)) {
-               fprintf (stderr,
-                       "Cannot access MTD device %s: %s\n",
-                       DEVNAME (0), strerror (errno));
-               return -1;
-       }
+       rc = check_device_config(0);
+       if (rc < 0)
+               return rc;
 
-       if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
-               fprintf (stderr,
-                       "Cannot access MTD device %s: %s\n",
-                       DEVNAME (1), strerror (errno));
-               return -1;
-       }
+       if (HaveRedundEnv) {
+               rc = check_device_config(1);
+               if (rc < 0)
+                       return rc;
 
-       if (HaveRedundEnv && ENVSIZE(0) != ENVSIZE(1)) {
-               ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
-               fprintf(stderr,
-                       "Redundant environments have inequal size, set to 0x%08lx\n",
-                       ENVSIZE(1));
+               if (ENVSIZE(0) != ENVSIZE(1)) {
+                       ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
+                       fprintf(stderr,
+                               "Redundant environments have inequal size, set to 0x%08lx\n",
+                               ENVSIZE(1));
+               }
        }
 
        usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
        if (HaveRedundEnv)
                usable_envsize -= sizeof(char);
 
-       if (common_args.aes_flag)
+       if (opts->aes_flag)
                usable_envsize &= ~(AES_KEY_LENGTH - 1);
 
        return 0;
@@ -1407,12 +1446,12 @@ static int get_config (char *fname)
                if (dump[0] == '#')
                        continue;
 
-               rc = sscanf (dump, "%ms %lx %lx %lx %lx",
-                            &devname,
-                            &DEVOFFSET (i),
-                            &ENVSIZE (i),
-                            &DEVESIZE (i),
-                            &ENVSECTORS (i));
+               rc = sscanf(dump, "%ms %lli %lx %lx %lx",
+                           &devname,
+                           &DEVOFFSET(i),
+                           &ENVSIZE(i),
+                           &DEVESIZE(i),
+                           &ENVSECTORS(i));
 
                if (rc < 3)
                        continue;