]> git.sur5r.net Git - u-boot/blobdiff - tools/env/fw_env.c
tools/env: ensure environment starts at erase block boundary
[u-boot] / tools / env / fw_env.c
index 01fc1d4e5741f2eb052f9da7aa0d18c49127e54d..d2b167deb9c1aa27198b7a4e7a659e4e4d656caa 100644 (file)
@@ -5,29 +5,18 @@
  * (C) Copyright 2008
  * Guennadi Liakhovetski, DENX Software Engineering, lg@denx.de.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
+#define _GNU_SOURCE
+
+#include <compiler.h>
 #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 <stdlib.h>
 #include <stddef.h>
 
 #include "fw_env.h"
 
-#define WHITESPACE(c) ((c == '\t') || (c == ' '))
+struct env_opts default_opts = {
+#ifdef CONFIG_FILE
+       .config_file = CONFIG_FILE
+#endif
+};
+
+#define DIV_ROUND_UP(n, d)     (((n) + (d) - 1) / (d))
 
 #define min(x, y) ({                           \
        typeof(x) _min1 = (x);                  \
@@ -56,8 +51,8 @@
        _min1 < _min2 ? _min1 : _min2; })
 
 struct envdev_s {
-       char devname[16];               /* Device name */
-       ulong devoff;                   /* Device offset */
+       const char *devname;            /* Device name */
+       long long devoff;               /* Device offset */
        ulong env_size;                 /* environment size */
        ulong erase_size;               /* device erase size */
        ulong env_sectors;              /* number of environment sectors */
@@ -83,7 +78,8 @@ static int dev_current;
 
 #define CUR_ENVSIZE ENVSIZE(dev_current)
 
-#define ENV_SIZE      getenvsize()
+static unsigned long usable_envsize;
+#define ENV_SIZE      usable_envsize
 
 struct env_image_single {
        uint32_t        crc;    /* CRC32 over data bytes    */
@@ -114,6 +110,8 @@ static struct environment environment = {
        .flag_scheme = FLAG_NONE,
 };
 
+static int env_aes_cbc_crypt(char *data, const int enc, uint8_t *key);
+
 static int HaveRedundEnv = 0;
 
 static unsigned char active_flag = 1;
@@ -124,38 +122,49 @@ 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 *);
 #endif
-static inline ulong getenvsize (void)
-{
-       ulong rc = CUR_ENVSIZE - sizeof(long);
 
-       if (HaveRedundEnv)
-               rc -= sizeof (char);
-       return rc;
+static char *skip_chars(char *s)
+{
+       for (; *s != '\0'; s++) {
+               if (isblank(*s))
+                       return s;
+       }
+       return NULL;
 }
 
-static char *fw_string_blank(char *s, int noblank)
+static char *skip_blanks(char *s)
 {
-       int i;
-       int len = strlen(s);
-
-       for (i = 0; i < len; i++, s++) {
-               if ((noblank && !WHITESPACE(*s)) ||
-                       (!noblank && WHITESPACE(*s)))
-                       break;
+       for (; *s != '\0'; s++) {
+               if (!isblank(*s))
+                       return s;
        }
-       if (i == len)
+       return NULL;
+}
+
+/*
+ * 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;
 
-       return s;
+       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.
  */
@@ -207,20 +216,56 @@ char *fw_getdefenv(char *name)
        return NULL;
 }
 
+int parse_aes_key(char *key, uint8_t *bin_key)
+{
+       char tmp[5] = { '0', 'x', 0, 0, 0 };
+       unsigned long ul;
+       int i;
+
+       if (strnlen(key, 64) != 32) {
+               fprintf(stderr,
+                       "## Error: '-a' option requires 16-byte AES key\n");
+               return -1;
+       }
+
+       for (i = 0; i < 16; i++) {
+               tmp[2] = key[0];
+               tmp[3] = key[1];
+               errno = 0;
+               ul = strtoul(tmp, NULL, 16);
+               if (errno) {
+                       fprintf(stderr,
+                               "## Error: '-a' option requires valid AES key\n");
+                       return -1;
+               }
+               bin_key[i] = ul & 0xff;
+               key += 2;
+       }
+       return 0;
+}
+
 /*
  * Print the current definition of one, or more, or all
  * environment variables
  */
-int fw_printenv (int argc, char *argv[])
+int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 {
-       char *env, *nxt;
-       int i, n_flag;
-       int rc = 0;
+       int i, rc = 0;
+
+       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())
+       if (fw_env_open(opts))
                return -1;
 
-       if (argc == 1) {                /* Print all env variables  */
+       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]) {
@@ -235,53 +280,45 @@ int fw_printenv (int argc, char *argv[])
                return 0;
        }
 
-       if (strcmp (argv[1], "-n") == 0) {
-               n_flag = 1;
-               ++argv;
-               --argc;
-               if (argc != 2) {
-                       fprintf (stderr, "## Error: "
-                               "`-n' option requires exactly one argument\n");
-                       return -1;
-               }
-       } else {
-               n_flag = 0;
-       }
-
-       for (i = 1; 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 (!n_flag) {
-                                       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 (!opts)
+               opts = &default_opts;
+
+       if (opts->aes_flag) {
+               ret = env_aes_cbc_crypt(environment.data, 1,
+                                       opts->aes_key);
+               if (ret) {
+                       fprintf(stderr,
+                               "Error: can't encrypt env for flash\n");
+                       return ret;
+               }
+       }
+
        /*
         * Update CRC
         */
@@ -427,31 +464,38 @@ 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;
-       char *name;
+       char *name, **valv;
        char *value = NULL;
+       int valc;
+
+       if (!opts)
+               opts = &default_opts;
 
-       if (argc < 2) {
+       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;
        }
 
-       name = argv[1];
+       name = argv[0];
+       valv = argv + 1;
+       valc = argc - 1;
 
-       if (env_flags_validate_env_set_params(argc, argv) < 0)
-               return 1;
+       if (env_flags_validate_env_set_params(name, valv, valc) < 0)
+               return -1;
 
        len = 0;
-       for (i = 2; i < argc; ++i) {
-               char *val = argv[i];
+       for (i = 0; i < valc; ++i) {
+               char *val = valv[i];
                size_t val_len = strlen(val);
 
                if (value)
@@ -473,7 +517,7 @@ int fw_setenv(int argc, char *argv[])
 
        free(value);
 
-       return fw_env_close();
+       return fw_env_close(opts);
 }
 
 /*
@@ -493,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 */
@@ -503,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;
        }
@@ -536,31 +583,29 @@ int fw_parse_script(char *fname)
                }
 
                /* Drop ending line feed / carriage return */
-               while (len > 0 && (dump[len - 1] == '\n' ||
-                               dump[len - 1] == '\r')) {
-                       dump[len - 1] = '\0';
-                       len--;
-               }
+               dump[--len] = '\0';
+               if (len && dump[len - 1] == '\r')
+                       dump[--len] = '\0';
 
                /* Skip comment or empty lines */
-               if ((len == 0) || dump[0] == '#')
+               if (len == 0 || dump[0] == '#')
                        continue;
 
                /*
                 * Search for variable's name,
                 * remove leading whitespaces
                 */
-               name = fw_string_blank(dump, 1);
+               name = skip_blanks(dump);
                if (!name)
                        continue;
 
                /* The first white space is the end of variable name */
-               val = fw_string_blank(name, 0);
+               val = skip_chars(name);
                len = strlen(name);
                if (val) {
                        *val++ = '\0';
                        if ((val - name) < len)
-                               val = fw_string_blank(val, 1);
+                               val = skip_blanks(val);
                        else
                                val = NULL;
                }
@@ -593,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;
-
 }
 
 /*
@@ -617,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;
                }
@@ -705,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);
@@ -743,27 +788,39 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                                   MEMGETBADBLOCK needs 64 bits */
        int rc;
 
-       blocklen = DEVESIZE (dev);
+       /*
+        * For mtd devices only offset and size of the environment do matter
+        */
+       if (mtd_type == MTD_ABSENT) {
+               blocklen = count;
+               top_of_range = offset + count;
+               erase_len = blocklen;
+               blockstart = offset;
+               block_seek = 0;
+               write_total = blocklen;
+       } else {
+               blocklen = DEVESIZE(dev);
 
-       top_of_range = ((DEVOFFSET(dev) / blocklen) +
-                                       ENVSECTORS (dev)) * blocklen;
+               top_of_range = ((DEVOFFSET(dev) / blocklen) +
+                                       ENVSECTORS(dev)) * blocklen;
 
-       erase_offset = (offset / blocklen) * blocklen;
+               erase_offset = (offset / blocklen) * blocklen;
 
-       /* Maximum area we may use */
-       erase_len = top_of_range - erase_offset;
+               /* Maximum area we may use */
+               erase_len = top_of_range - erase_offset;
 
-       blockstart = erase_offset;
-       /* Offset inside a block */
-       block_seek = offset - erase_offset;
+               blockstart = erase_offset;
+               /* Offset inside a block */
+               block_seek = offset - erase_offset;
 
-       /*
-        * Data size we actually have to write: from the start of the block
-        * to the start of the data, then count bytes of data, and to the
-        * end of the block
-        */
-       write_total = ((block_seek + count + blocklen - 1) /
-                                               blocklen) * blocklen;
+               /*
+                * Data size we actually write: from the start of the block
+                * to the start of the data, then count bytes of data, and
+                * to the end of the block
+                */
+               write_total = ((block_seek + count + blocklen - 1) /
+                                                       blocklen) * blocklen;
+       }
 
        /*
         * Support data anywhere within erase sectors: read out the complete
@@ -791,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
@@ -834,17 +892,18 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                        continue;
                }
 
-               erase.start = blockstart;
-               ioctl (fd, MEMUNLOCK, &erase);
-               /* These do not need an explicit erase cycle */
-               if (mtd_type != MTD_ABSENT &&
-                   mtd_type != MTD_DATAFLASH)
-                       if (ioctl (fd, MEMERASE, &erase) != 0) {
-                               fprintf (stderr, "MTD erase error on %s: %s\n",
-                                        DEVNAME (dev),
-                                        strerror (errno));
-                               return -1;
-                       }
+               if (mtd_type != MTD_ABSENT) {
+                       erase.start = blockstart;
+                       ioctl(fd, MEMUNLOCK, &erase);
+                       /* These do not need an explicit erase cycle */
+                       if (mtd_type != MTD_DATAFLASH)
+                               if (ioctl(fd, MEMERASE, &erase) != 0) {
+                                       fprintf(stderr,
+                                               "MTD erase error on %s: %s\n",
+                                               DEVNAME(dev), strerror(errno));
+                                       return -1;
+                               }
+               }
 
                if (lseek (fd, blockstart, SEEK_SET) == -1) {
                        fprintf (stderr,
@@ -854,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",
@@ -863,11 +923,12 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                        return -1;
                }
 
-               ioctl (fd, MEMLOCK, &erase);
+               if (mtd_type != MTD_ABSENT)
+                       ioctl(fd, MEMLOCK, &erase);
 
-               processed  += blocklen;
+               processed  += erasesize;
                block_seek = 0;
-               blockstart += blocklen;
+               blockstart += erasesize;
        }
 
        if (write_total > count)
@@ -902,6 +963,28 @@ static int flash_flag_obsolete (int dev, int fd, off_t offset)
        return rc;
 }
 
+/* Encrypt or decrypt the environment before writing or reading it. */
+static int env_aes_cbc_crypt(char *payload, const int enc, uint8_t *key)
+{
+       uint8_t *data = (uint8_t *)payload;
+       const int len = usable_envsize;
+       uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
+       uint32_t aes_blocks;
+
+       /* First we expand the key. */
+       aes_expand_key(key, key_exp);
+
+       /* Calculate the number of AES blocks to encrypt. */
+       aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
+
+       if (enc)
+               aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
+       else
+               aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
+
+       return 0;
+}
+
 static int flash_write (int fd_current, int fd_target, int dev_target)
 {
        int rc;
@@ -922,9 +1005,10 @@ 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
+
        rc = flash_write_buf(dev_target, fd_target, environment.image,
                              CUR_ENVSIZE, DEVOFFSET(dev_target),
                              DEVTYPE(dev_target));
@@ -937,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);
@@ -948,43 +1032,14 @@ 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;
 
-       return (rc != CUR_ENVSIZE) ? -1 : 0;
+       return 0;
 }
 
 static int flash_io (int mode)
@@ -1045,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;
@@ -1077,10 +1113,15 @@ int fw_env_open(void)
        unsigned char flag1;
        void *addr1;
 
+       int ret;
+
        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);
@@ -1111,6 +1152,14 @@ int fw_env_open(void)
                return -1;
 
        crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
+
+       if (opts->aes_flag) {
+               ret = env_aes_cbc_crypt(environment.data, 0,
+                                       opts->aes_key);
+               if (ret)
+                       return ret;
+       }
+
        crc0_ok = (crc0 == *environment.crc);
        if (!HaveRedundEnv) {
                if (!crc0_ok) {
@@ -1152,12 +1201,23 @@ int fw_env_open(void)
                } else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
                           DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
                        environment.flag_scheme = FLAG_INCREMENTAL;
+               } else if (DEVTYPE(dev_current) == MTD_ABSENT &&
+                          DEVTYPE(!dev_current) == MTD_ABSENT) {
+                       environment.flag_scheme = FLAG_INCREMENTAL;
                } else {
                        fprintf (stderr, "Incompatible flash types!\n");
                        return -1;
                }
 
                crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
+
+               if (opts->aes_flag) {
+                       ret = env_aes_cbc_crypt(redundant->data, 0,
+                                               opts->aes_key);
+                       if (ret)
+                               return ret;
+               }
+
                crc1_ok = (crc1 == redundant->crc);
                flag1 = redundant->flags;
 
@@ -1229,25 +1289,106 @@ 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 (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
+               fprintf(stderr, "Environment does not start on erase block boundary\n");
+               errno = EINVAL;
+               return -1;
+       }
+
+       if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
+               fprintf(stderr, "Environment does not fit into available sectors\n");
+               errno = EINVAL;
+               return -1;
+       }
+
+       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 (CONFIG_FILE)) {
-               fprintf (stderr,
-                       "Cannot parse config file: %s\n", strerror (errno));
+       if (get_config(opts->config_file)) {
+               fprintf(stderr, "Cannot parse config file '%s': %m\n",
+                       opts->config_file);
                return -1;
        }
 #else
-       strcpy (DEVNAME (0), DEVICE1_NAME);
+       DEVNAME (0) = DEVICE1_NAME;
        DEVOFFSET (0) = DEVICE1_OFFSET;
        ENVSIZE (0) = ENV1_SIZE;
-       /* Default values are: erase-size=env-size, #sectors=1 */
+       /* Default values are: erase-size=env-size */
        DEVESIZE (0) = ENVSIZE (0);
-       ENVSECTORS (0) = 1;
+       /* #sectors=env-size/erase-size (rounded up) */
+       ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
 #ifdef DEVICE1_ESIZE
        DEVESIZE (0) = DEVICE1_ESIZE;
 #endif
@@ -1256,12 +1397,13 @@ static int parse_config ()
 #endif
 
 #ifdef HAVE_REDUND
-       strcpy (DEVNAME (1), DEVICE2_NAME);
+       DEVNAME (1) = DEVICE2_NAME;
        DEVOFFSET (1) = DEVICE2_OFFSET;
        ENVSIZE (1) = ENV2_SIZE;
-       /* Default values are: erase-size=env-size, #sectors=1 */
+       /* Default values are: erase-size=env-size */
        DEVESIZE (1) = ENVSIZE (1);
-       ENVSECTORS (1) = 1;
+       /* #sectors=env-size/erase-size (rounded up) */
+       ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
 #ifdef DEVICE2_ESIZE
        DEVESIZE (1) = DEVICE2_ESIZE;
 #endif
@@ -1271,19 +1413,30 @@ 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 (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 (opts->aes_flag)
+               usable_envsize &= ~(AES_KEY_LENGTH - 1);
+
        return 0;
 }
 
@@ -1294,6 +1447,7 @@ static int get_config (char *fname)
        int i = 0;
        int rc;
        char dump[128];
+       char *devname;
 
        fp = fopen (fname, "r");
        if (fp == NULL)
@@ -1304,23 +1458,25 @@ static int get_config (char *fname)
                if (dump[0] == '#')
                        continue;
 
-               rc = sscanf (dump, "%s %lx %lx %lx %lx",
-                            DEVNAME (i),
-                            &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;
 
+               DEVNAME(i) = devname;
+
                if (rc < 4)
                        /* Assume the erase size is the same as the env-size */
                        DEVESIZE(i) = ENVSIZE(i);
 
                if (rc < 5)
-                       /* Default - 1 sector */
-                       ENVSECTORS (i) = 1;
+                       /* Assume enough env sectors to cover the environment */
+                       ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
 
                i++;
        }