+ int32_t lnum = 0;
+ int fd, rc = 0;
+
+ /* Fills in IS_UBI(), converts DEVNAME() with ubi volume name */
+ ubi_check_dev(dev);
+
+ 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 (IS_UBI(dev)) {
+ rc = ioctl(fd, UBI_IOCEBISMAP, &lnum);
+ if (rc < 0) {
+ fprintf(stderr, "Cannot get UBI information for %s\n",
+ DEVNAME(dev));
+ goto err;
+ }
+ } else 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;
+ if (DEVESIZE(dev) == 0)
+ /* Assume the erase size is the same as the env-size */
+ DEVESIZE(dev) = ENVSIZE(dev);
+ } else {
+ uint64_t size;
+ DEVTYPE(dev) = MTD_ABSENT;
+ if (DEVESIZE(dev) == 0)
+ /* Assume the erase size to be 512 bytes */
+ DEVESIZE(dev) = 0x200;
+
+ /*
+ * 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
+ }
+ }
+
+ if (ENVSECTORS(dev) == 0)
+ /* Assume enough sectors to cover the environment */
+ ENVSECTORS(dev) = DIV_ROUND_UP(ENVSIZE(dev), DEVESIZE(dev));
+
+ 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;
+ }
+
+ err:
+ close(fd);
+ return rc;
+}
+
+static int parse_config(struct env_opts *opts)
+{
+ int rc;
+
+ if (!opts)
+ opts = &default_opts;